Skip to content

BM25Index

Contract for a BM25-ranked keyword index — the keyword tier of hybrid retrieval. Implementations: BM25Index (in-memory, rebuilt per process) and Fts5KeywordIndex (SQLite FTS5, persistent — survives Durable Object hibernation with zero rebuild).

All methods are synchronous: the in-memory index is pure computation and DO SQLite’s sql.exec is synchronous, so the contract stays sync to keep FusionRetriever / KnowledgeFs hot paths allocation-free.

new BM25Index(options?): BM25Index;
Parameter Type

options?

BM25IndexOptions

BM25Index

get size(): number;

Number of active (non-removed) documents in the index.

number

Number of documents in the index.

KeywordIndex.size

add(documents): void;

Add documents to the index. If a document with the same ID already exists, it is overwritten (old entry is logically removed first).

Parameter Type

documents

BM25Document[]

void

KeywordIndex.add


clear(): void;

Remove all documents and reset the index.

void

KeywordIndex.clear


remove(id): boolean;

Remove a document from the index by ID. Returns true if the document existed and was removed.

Parameter Type

id

string

boolean

KeywordIndex.remove


search(query, topK?): BM25SearchResult[];

Search the index for documents matching the query.

Parameter Type Default value Description

query

string

undefined

Natural language query (tokenized the same way as documents).

topK

number

10

Maximum number of results. Default: 10.

BM25SearchResult[]

Scored results sorted by BM25 score descending.

KeywordIndex.search