Creates a new BM25 index.
Optional config: BM25ConfigOptional BM25 tuning parameters and tokenizer.
// Use defaults (k1=1.2, b=0.75)
const index = new BM25Index();
// Custom parameters for short documents
const shortDocIndex = new BM25Index({ k1: 1.5, b: 0.5 });
Static fromJSONA saved index restored; give the configuration it was made with. Its k1 and b come from the saved index, and
its tokenizer from config, which must read text as the one the index was made with.
Optional config: BM25ConfigProtected recomputeSearches. With no options it answers exactly what BM25Index has always answered.
The query text, read with the index's tokenizer.
The most results answered.
match and prefix; see LexicalSearchOptions.
The matching documents, highest score first.
10
The index as plain JSON.
Protected restoreFills this index from a saved one, or throws an Error naming what keeps it from being one toJSON writes.
Protected k1Term saturation parameter (typical range: 1.2-2.0).
Protected bDocument length normalization (0 = none, 1 = full).
Protected documentsMap of document ID to internal document representation.
Protected invertedEach term's documents with the term's count in each: term -> { docId -> termFrequency }.
Protected idfEach term's IDF, recomputed after documents are added or removed.
Protected avgAverage document length across the index (in tokens).
Protected idfWhether the IDF cache needs recomputation.
Protected Readonly tokenizeThe tokenizer this index was made with.
BM25 sparse keyword index for hybrid retrieval.
Dense embeddings excel at semantic similarity but miss exact keyword matches (e.g., error codes, function names, product IDs). BM25 catches these by scoring documents based on term frequency, inverse document frequency, and document length normalization.
It is a LexicalIndex with AgentOS's tokenizer: the configured
tokenizeorpipeline, else the built-in one, whose stop words arenatural's English list when that package loads andENGLISH_STOP_WORDSotherwise, read on the first tokenization, not when the module is imported.toJSON()saves it and BM25Index.fromJSON restores it.Example: Basic usage
Example: Combined with HybridSearcher