annals.search#
How the catalog answers a query: the search strategies.
A strategy is a callable (query, rows, text_of) -> rows: given every listed row (the
catalog has already listed them and overlaid the owner’s tags) and a way to get an item’s
full text, it returns the rows that match, best first, each with a snippet of the
text around the match and, when it ranks, a score (higher is better). The catalog
takes one as its search keyword (annals.catalog.Catalog), so a smarter one
(semantic, hybrid, an agent that rewrites the query) plugs in at the same place.
Two ship here:
IndexedSearch, the default when the data root is on this machine: a SQLite FTS5 index kept on disk (cache/search/index.sqliteunder the data root). Each query brings it up to date first, re-reading only items whose stamp changed (their listed fields, and thestampa source gives, else their time and size), so the first query after a restart costs one pass over the texts and every later one a few milliseconds. Matching is by word prefix with English stemming (ratefinds “rates” and “rated”, not “separate”), every word required, ranked by BM25 with the title weighted most.scan_search(), the index-free fallback (a remote data root, tests): every word must appear as a substring of the title, tags, session, excerpt, id or full text; newest first, as listed.
Module Attributes
characters of context on each side of the first match, in a snippet |
|
tokens in an index snippet (FTS5's own window) |
|
the best rows that get a snippet (each costs ~5 ms); the rest show their excerpt. |
|
the most rows a ranked query returns; the page shows a few dozen at a time |
|
column weights for BM25, in the order of |
|
where index files live, under the data root ( |
|
an old index is rebuilt |
|
characters of the body kept around the match for rows past |
|
rows indexed per write transaction, so a first build never holds the lock for long |
|
bound variables per statement (SQLite's default limit is far above; this stays safe) |
Functions
|
The index |
|
A user's words as an FTS5 query: every word required, each a prefix. |
|
The words of a query, lowercased: |
|
Every word a substring of the row's fields or full text; rows kept in their order. |
|
The text around the first occurrence of any of |
Classes
|
A SQLite FTS5 index of every row's fields and text, synced per query by stamp. |
|
|
- annals.search.DFLT_WEIGHTS = (10.0, 5.0, 2.0, 1.0, 1.0)#
column weights for BM25, in the order of
_COLUMNS(the title matters most)
- annals.search.INDEX_DIR = 'cache/search'#
where index files live, under the data root (
<name>.sqlite)
- class annals.search.IndexedSearch(path=':memory:', *, weights=(10.0, 5.0, 2.0, 1.0, 1.0), max_results=500, snippet_tokens=24, snippet_rows=60)[source]#
Bases:
objectA SQLite FTS5 index of every row’s fields and text, synced per query by stamp.
pathis the index file (made on first use;":memory:"keeps it in memory).weightsare BM25’s per-column weights (title, tags, session/id/source, excerpt, text).max_resultscaps a ranked answer; the bestsnippet_rowscarry FTS5’s snippet, the rest a cheaper window of text around the query’s longest word. Safe across threads (one sync at a time: queries that arrive during a sync wait for it rather than redo it) and across processes sharing the file (two web workers): every write is one shortBEGIN IMMEDIATEtransaction. If the index fails (a locked or damaged file), a query falls back toscan_search()rather than fail.- sync(rows, text_of)[source]#
(Re)index the rows whose stamp changed; returns how many.
Nothing is dropped here: a query only ever answers with the rows it was given, so an item listed elsewhere (in the bin, behind a hiding store, or gone) simply does not come back.
forget()drops an item for good.- Return type:
- annals.search.MAX_RESULTS = 500#
the most rows a ranked query returns; the page shows a few dozen at a time
- annals.search.SCHEMA_VERSION = '3'#
an old index is rebuilt
- Type:
bumped when the schema or what is indexed changes
- annals.search.SNIPPET_CHARS = 90#
characters of context on each side of the first match, in a snippet
- annals.search.SNIPPET_ROWS = 60#
the best rows that get a snippet (each costs ~5 ms); the rest show their excerpt. One page of the list (the page’s
LIST_PAGE).
- annals.search.SNIPPET_TOKENS = 24#
tokens in an index snippet (FTS5’s own window)
- annals.search.SQL_VARS = 500#
bound variables per statement (SQLite’s default limit is far above; this stays safe)
- annals.search.SYNC_BATCH = 200#
rows indexed per write transaction, so a first build never holds the lock for long
- class annals.search.SearchStrategy(*args, **kwargs)[source]#
Bases:
Protocol(query, rows, text_of) -> matching rows, best first.rowsare what the reader may see (a strategy never answers with anything else);text_of(row)reads an item’s full text. A strategy that keeps state about items may also haveforget(key), which the catalog calls when an item is gone for good.
- annals.search.WINDOW_CHARS = 90#
characters of the body kept around the match for rows past
snippet_rows
- annals.search.default_search(data_root, *, name='index')[source]#
The index
<data root>/cache/search/<name>.sqlitewhen the data root is on this machine, else the scan.- Return type:
- annals.search.fts_query(q)[source]#
A user’s words as an FTS5 query: every word required, each a prefix.
Each word is quoted, so FTS5 syntax typed by the user (
AND,-,:, quotes) is searched for as text, never run as an operator. Control characters are dropped, and so is a word with no letter or digit (the tokenizer would drop it anyway, and an empty phrase matches nothing), so a query of only punctuation is no query.>>> fts_query('data rates') '"data"* "rates"*' >>> fts_query('a"b NOT') '"a""b"* "not"*' >>> fts_query(' ... - *') ''
- Return type:
- annals.search.query_words(q)[source]#
The words of a query, lowercased:
"Data Rates"->["data", "rates"].