ir.lexical_fts5

A persisted, incremental lexical backend on SQLite FTS5 (search(lexical="fts5")).

The default lexical leg (ir.retrieve._bm25_backend()) tokenizes every candidate document in memory the first time a process queries a corpus and matches whole tokens. This backend keeps the index in one SQLite file next to the corpus (standard library only), brings it up to date incrementally, and matches Porter-stemmed words, optionally with the last word as a prefix (search-as-you-type).

How it stays current: a query syncs the index whenever the corpus’s records are not the ones it last synced from (the first query on an open corpus, or after the store was rebuilt into). Each record’s stamp is the SHA-1 of its text; records whose stamp differs from the file’s are rewritten, records gone from the corpus are deleted, the rest are not touched. The diff and the writes are one BEGIN IMMEDIATE transaction, so processes sharing the file take turns and a killed sync leaves the previous index intact. Results are always cut to the current candidates. A store with no directory (an in-memory corpus) gets an in-memory index with the same behaviour. A file written with another schema or tokenizer is rebuilt.

Scores are SQLite’s bm25() negated (higher is better); they are on a different scale from the vd Okapi scores, so a floor calibrated for lexical="bm25" does not apply to lexical="fts5".

Options (search(..., lexical_options={...})): prefix (bool, default False: the last query word also matches longer words), match ("any", default, or "all": whether a document needs one or every query word).

Module Attributes

INDEX_FILENAME

The index file's name inside a corpus directory.

TOKENIZER

Unicode word characters, folded, stemmed.

SCHEMA_VERSION

Bumped when the table layout changes; a file with another version is rebuilt.

Functions

fts5_backend(corpus, ids, metas, candidates, ...)

A ir.retrieve.LexicalBackend over the persisted FTS5 index.

match_expression(query, *[, prefix, match])

The FTS5 MATCH text for query, or None when it has no words.

sync(db, ids, metas)

Make the index equal to (ids, metas); returns {added, changed, removed, kept}.

ir.lexical_fts5.INDEX_FILENAME = 'lexical_fts5.sqlite'

The index file’s name inside a corpus directory.

ir.lexical_fts5.SCHEMA_VERSION = 1

Bumped when the table layout changes; a file with another version is rebuilt.

ir.lexical_fts5.TOKENIZER = 'porter unicode61 remove_diacritics 2'

Unicode word characters, folded, stemmed.

Type:

The FTS5 tokenizer

ir.lexical_fts5.fts5_backend(corpus, ids, metas, candidates, query, *, fetch, cache_key=None, options=None)[source]

A ir.retrieve.LexicalBackend over the persisted FTS5 index.

Return type:

list[tuple[str, float]]

ir.lexical_fts5.match_expression(query, *, prefix=False, match='any')[source]

The FTS5 MATCH text for query, or None when it has no words.

Every word is double-quoted, so user text can never be read as FTS5 syntax (operators, column filters, NEAR). With prefix the last word gets *.

Return type:

str | None

ir.lexical_fts5.sync(db, ids, metas)[source]

Make the index equal to (ids, metas); returns {added, changed, removed, kept}.

The whole diff runs inside one BEGIN IMMEDIATE transaction, read included, so two processes syncing the same file take turns (the second finds nothing to do) and a killed sync leaves the previous index intact.

Return type:

dict