lmlucene guide

Lucene 10.5.2 object lifecycle, SAM TokenTransform and FieldWeight callbacks, facet and KNN search, and standard lmcode_std_v3 REST and CLI interfaces in lmlucene.

Overview

lmlucene embeds Apache Lucene 10.5.2 and the lmcode-java runtime into a single JVM or GraalVM native executable. LMScript programs construct and operate real Java Lucene directories, analyzers, index writers, near-real-time readers, and searchers inside type-checked _lmcode_ blocks.

Indexing, directories, and document fields

An indexing workflow starts by choosing a storage directory (memory_directory() for in-memory indexes or fs_directory(path) for persistent NIOFSDirectory storage) and an analyzer (standard_analyzer(), english_analyzer(), whitespace_analyzer(), keyword_analyzer(), or a custom rule_analyzer(name)).

The Document builder supports multi-modal field indexing on each document:

SAM callbacks: TokenTransform and FieldWeight

lmlucene exposes two SAM interfaces that hook directly into Java Lucene extension points using an isolated callback registry:

program = _lmcode_ cli="lmlucene";
@analysis
fn normalize(token: string) -> string implements TokenTransform {
  if (token == "automobile") { return "car"; }
  return token;
}

@similarity
fn title_weight(stats: CollectionStats) -> float implements FieldWeight {
  if (stats.field() == "title") { return 10.0; }
  return 1.0;
}

let dir = memory_directory();
let analyzer = rule_analyzer("normalize");
let w = index_writer(dir, writer_config(analyzer));
w.add(document().text("title", "automobile guide", true));
let s = index_searcher(w.reader());
s.bm25(1.2, 0.75).similarity_rule("title_weight");
s.search(parse_query("title", "car", analyzer), 5).emit("hits");
end _lmcode_

TokenTransform runs inside a custom TokenFilter chained after StandardAnalyzer, rewriting token terms while keeping token offsets and positions intact. FieldWeight runs inside Similarity.scorer, inspecting CollectionStats (field, doc_count, total_terms, doc_freq, boost) to return a nonnegative multiplier for the underlying BM25 scorer.

Search, facets, pagination, and explanations

IndexSearcher supports Boolean combinations (boolean_query().add(q, Occur.MUST).build()), numeric ranges (long_range), prefix and term queries, QueryParser strings (parse_query), score pagination (search_after), doc-value sorting (sorted_search), flat facet aggregation (facets and drill_down), filtered KNN search (knn_query), and scoring explanations (explain).

Standard REST and stateless CLI interfaces

Both the primary Host registry and the SAM callback registry include all 226 canonical lmcode_std_v3 signatures on current main, allowing RestGet..RestOptions, RestHandler, CliCommand, and CliOption handlers to build and query Lucene indexes via Program.callIsolated(). This describes current main (a source build); the latest published release at review time, v0.1.19, predates the std upgrade, so run lmlucene --lmcode-signatures to see whether an installed host ships the REST and CLI interfaces.

program = _lmcode_ cli="lmlucene";
@"/search/:term"
fn handle_search(x: RestExchange) -> void implements RestGet {
  let dir = memory_directory();
  let w = index_writer(dir, writer_config(standard_analyzer()));
  w.add(document().text("body", "apache lucene search", true));
  let hits = index_searcher(w.reader()).search(term_query("body", x.param("term")), 5);
  x.json("{\"total\":" + str(hits.total()) + "}");
}

print(rest_cli(str_split("/search/lucene", " ")));
end _lmcode_