Skip to content

Reference

db.enable_keyword_search

Index a metadata field for keyword (BM25) search.

Index a metadata field for keyword (BM25) search with db.search(text=...).

db.enable_keyword_search(field: str = "text") -> None
  • field : str, default "text" Name of a string field in each record’s metadata JSON. "a.b" names a nested field; a list of strings is indexed as one text.
  • ValueError field is empty or has an empty part ("a..b"), or is longer than 256 bytes.

  • OSError Reading the metadata failed.

  • Reuses the saved index when it covers the same field; otherwise builds it from metadata, reading every record once.
  • The index is written by the next save() and used again after srvdb.open() without calling this again.
  • Records without a string value in the field are not indexed.
  • Call it again to rebuild a damaged index file.
import srvdb
db = srvdb.create("./docs", dimension=128)
v = [1.0] + [0.0] * 127
db.add(["a"], [v], ['{"text": "error E0308"}'])
db.enable_keyword_search("text")
print(db.search(None, k=1, text="e0308")[0][0])
# a