sil 3.4.1 · download as pdf · source on github

sil reference

sil (Seecr Indexing Lib) is a search index for Python applications, on tantivy (a Rust search engine, like Lucene). It adds what web applications need on top: an index profile in Python, a forgiving query language, sorting on several fields, drilldown (counts per value), suggestions while typing, and commits on a timer. One Index is one directory; no server, no JVM.

Install with pip install seecr-sil (the package is sil); it needs Python 3.11 or newer and tantivy (installed with it).

An index

from sil.index import Index
from sil.indexfield import IndexField
from sil.indexprofile import IndexProfile

index = Index(
    path,                                         # a pathlib.Path; made when missing
    IndexProfile([
        IndexField.unqualified("all"),
        IndexField.text("title", stored=True),
        IndexField.untokenized("genre", drilldown=True),
        IndexField.int("year", sortable=True),
    ]),
    immediate_commit=True,                        # or a timer, see Commits
)
index.add_record("book-1", {"all": "Dune Herbert", "title": "Dune",
                            "genre": "sf", "year": 1965})
result = index.search_query("dune", sort=[{"field": "year", "reverse": True}])
result["count"], result["docs"][0]            # 1, {"__id__": "book-1", "title": "Dune"}
index.shutdown()                                  # commits and closes

Index(path, profile, commit_timeout=10, immediate_commit=False):

paththe directory: index/ (tantivy) and, for suggest fields, suggest/<field>/
profilethe fields, an IndexProfile
commit_timeoutseconds between commits of the timer; 0: no timer
immediate_commitcommit after every change

Index fields

IndexField.untokenized(name)the value as one term: codes, ids, statuses, tags. Exact match only; case matters
IndexField.text(name)the value split into lowercased words: titles, descriptions
IndexField.int(name)a 64 bit integer; ranges (year:[1960 TO 1970]) are fast
IndexField.unqualified(name)a text field that a query without field: searches; at most one per profile

Options (keyword arguments):

stored=Truethe value comes back in the hits (hit["title"]); otherwise only __id__ does
sortable=Truecan be sorted on (the first value of a document)
drilldown=Truevalues are counted in a search (untokenized and int fields)
suggest=Truevalues are offered by suggest()
ascii_folding=Truetext and unqualified fields: accents and other non-ASCII letters are folded to ASCII, in the index and in queries, so cafe finds Café and strasse Straße (as Lucene’s ASCIIFoldingFilter; meresco-lucene did this). Changing it needs a reindex
hierarchical=Truethe values are paths (vehicle>bicycle), counted as a tree; with drilldown=True on an untokenized field (see Hierarchical drilldown)

A value can be a list: every value is indexed (searching finds any of them; drilldown counts all of them; sorting uses the first). None and [] mean no value. Every index has __id__, the record id, which is always stored.

Adding and deleting

add_record(record_id, values)adds, or replaces the record with this id (an update is a delete followed by an add)
delete_record(record_id, immediate_commit=False)removes it
delete_all()removes everything, also the suggestions

values is a dict of field name to value; fields not in the profile are ignored. Nothing is searchable before a commit.

Commits

A search sees the index as it was at the last commit. A commit takes milliseconds (it writes and syncs files), so sil offers three ways:

immediate_commit=Trueafter every change: searchable at once; for tests and scripts
commit_timeout=10 (default)a timer (an asyncio task: make the index in a running event loop) commits every 10 seconds when there are changes
commit_timeout=0only when asked: commit(immediate_commit=True), and at shutdown()

with index.deferred_commit(): puts off the commits of an immediate-commit index until the end of the block: one commit for many changes. Nested blocks commit at the end of the outermost one, also when it raises.

The writer (tantivy runs a few threads for it) is opened at the first change, not before: an index that is only searched costs little memory.

Searching

index.search(query, page=1, limit=20, sort=None, drilldown=None)
index.search_query("title:dune", ...)          # parses the query language
index.search_all(...)                           # every document
index.search_by_term("genre", "sf", ...)        # one exact term
index.search_by_id("book-1")

All give a dict:

countthe number of hits
docsthe hits of this page: Hits, dicts of stored field to its first value (__id__ included); hit.get_values(name) gives all values
drilldownper drilldown field asked for, a list of (value, count), most first

Build queries for search() with parse_query(text), term_query(field, value) (exact, not analysed) and Index.all_of(q1, q2, ...) (all must match):

q = Index.all_of(index.parse_query("dune"), index.term_query("genre", "sf"))
index.search(q, page=2, limit=10)

Without sort, the best matches come first and equal scores in the order they were added; * and term queries list in the order of adding.

The query language

tantivy’s query language, made forgiving for search boxes:

dune herberteither word (the best matches, with both, first)
+dune +herbert, dune AND herbertboth
rock -rollrock, not roll
"and roll"the phrase
title:dunein a field (words without a field search the unqualified field)
roc*a prefix (made into a regex: tantivy has no prefix query)
year:[1960 TO 1970], year:{1960 TO *]ranges, inclusive [], exclusive {}
(a OR b) AND cgrouping
*everything

A query that is not valid syntax (a stray quote, an unknown field) is searched for literally, with the special characters as spaces, so a search box never gives an error.

CQL

For applications that speak CQL (SRU, or a portal that came from Meresco), sil understands CQL as well. It gives the same hits as Meresco (cqlparser and meresco-lucene) gave:

index.search_cql('fiets AND untokenized.type exact "vehicle>bicycle"', limit=10)
query = index.cql_query("year >= 2010 AND title = fiets")      # a Query for search()
fiets, "fiets band"a word or a phrase in the unqualified field
fiets*a prefix (two or more letters, digits, _ or - before the *; f* is the word f, as in Meresco)
*everything
title = fietsin a text field: analysed as the field is (lowercase, folded with ascii_folding; several words are a phrase)
title exact Fiets, title == Fietsthe term as it is, not analysed; on an untokenized field = does the same
type exact "a>b"a path in a hierarchical field
year >= 2010, <, <=, >ranges, on int fields only
a AND b, a OR b, a NOT bbooleans; OR binds weakest, AND and NOT are read from left to right (as Meresco’s cqlparser did; the CQL standard has no precedence)
title =/boost=2 fietsa boost

Unlike the query language, CQL is not forgiving: CQL that can not be parsed, or that sil does not support (any, all, adj, <>, prox, modifiers other than boost, prefix assignments), raises sil.cql.CqlError (a ValueError). An index that is not in the profile matches nothing; a term without words (title = "*") is left out of the query, as Meresco did.

The tree of a query, to look at or change before searching (for instance to rename old index names):

from dataclasses import replace
from sil.cql import parse, rewrite, to_cql

tree = parse("dc.title = fiets OR boten")
tree = rewrite(tree, lambda c: replace(c, index="title") if c.index == "dc.title" else c)
to_cql(tree)                       # 'title = fiets OR boten'
index.search_cql(tree)

A tree is made of SearchClause(term, index, relation, boost) and Boolean(operator, left, right) (operator "and", "or" or "not").

Sorting

sort is a field name, a dict {"field": name, "reverse": True}, or a list of those: sorted on the first, then the second for equal values, and so on; then in the order of adding. Only sortable=True fields (else RuntimeError: Unsortable field). A document without a value sorts as "" or 0.

Paging is stable: page 2 continues exactly where page 1 stopped, also when many documents have the same value.

Drilldown

result = index.search_query("dune", drilldown=[{"name": "genre", "count": 10}])
result["drilldown"]                    # {"genre": [("sf", 12), ("fantasy", 3)]}

Counts the values of drilldown=True fields in all hits of the query (not only the page), most first; equal counts alphabetically. count (default 10) is how many values; 0 gives all of them. To filter on a value someone clicked, add a term_query with all_of.

Hierarchical drilldown

Some values have levels: a category vehicle with below it bicycle and car, or an organisation with its departments. A hierarchical field counts them as a tree:

IndexField.untokenized("type", drilldown=True, hierarchical=True)

index.add_record("1", {"type": ("vehicle", "bicycle")})   # a path: a tuple
index.add_record("2", {"type": "vehicle>bicycle"})        # or levels with ">"
index.add_record("3", {"type": ("vehicle", "car")})
index.add_record("4", {"type": "boat"})                   # one level

result = index.search_all(drilldown=[{"name": "type"}])
result["drilldown"]
# {"type": [
#     ("vehicle", 3, [
#         ("bicycle", 2, []),
#         ("car", 1, []),
#     ]),
#     ("boat", 1, []),
# ]}

Each value comes with its count and the values one level below it, in the same form ([] for the last level), so value, count = item[:2] works for flat and hierarchical fields alike. A document counts once for every level of its paths, also when two of its paths share a level. In the request:

countvalues per level (default 10; 0: all)
pathstart below this value: ["vehicle"] or "vehicle" gives [("bicycle", 2, []), ("car", 1, [])]
depthhow many levels (default all); 1 gives the top level only, each with []

A term query on a level finds everything below it: term_query("type", "vehicle") finds documents 1, 2 and 3; term_query("type", "vehicle>car") (or the tuple ("vehicle", "car")) finds document 3.

A list of values is several values, as for any field, so a path goes in a tuple or as one string with >: ["vehicle", "bicycle"] would be two values of one level each. A level can therefore not contain >. A hierarchical field can not be sortable, stored or suggest.

Suggestions

index.suggest("place", "new yo")       # ["New York", "York"]

Values of a suggest=True field, for typing ahead: the last word may be incomplete, the words before it must be whole; words anywhere in the value count. At most 10, in the order they were first added, without duplicates. Kept in suggest/<field>/; removed by delete_all(), not by delete_record().

Changing the profile

tantivy has a schema: when the fields (or their options) of the profile change, the existing index cannot be used. sil then starts that index empty, logs a warning, and index.needs_reindex is True. The application rebuilds it from its data; sil never does that by itself.

Threads

Adding, deleting and committing are safe from several threads (one writer, behind a lock). A search works on a snapshot of the index: a commit in another thread does not disturb it. One process per index: tantivy allows one writer, and a second process fails to get it.

Troubleshooting

A search doesn’t find what was just addedno commit yet: immediate_commit=True, or wait for the timer
RuntimeError: no running event loopthe timer needs one: make the index in the application’s startup, or use immediate_commit=True / commit_timeout=0
An exact search on a text field finds nothingtext is split into lowercased words: term_query("title", "dune")
An untokenized search finds nothingexact, case and all: "Dune" is not "dune"
RuntimeError: Unsortable fieldadd sortable=True, then rebuild the index
The index is empty after an upgradethe profile changed; rebuild it (see the log)
ValueError: Failed to acquire Lockfile: LockBusyanother process (or another Index on the same directory) is writing to it