Skip to main content

A tiny, zero-dependency BM25-lite in-memory text search index.

Project description

zerosearch

A tiny, zero-dependency BM25-lite in-memory text search index — standard library only, a single small module, and good enough to power retrieval for a RAG pipeline. Designed to run anywhere Python runs, including constrained environments like Cloudflare Python Workers (Pyodide) where pulling in scikit-learn/numpy is not an option.

It is a spiritual cousin of minsearch, with the same Index(text_fields, keyword_fields).fit(docs).search(query) shape, but reimplemented from scratch with no third-party dependencies.

Install

pip install zerosearch

Usage

from zerosearch import Index

docs = [
    {"id": "1", "title": "Docker compose basics", "text": "how to start services", "course": "de"},
    {"id": "2", "title": "Kafka consumers", "text": "consumer groups explained", "course": "de"},
]

index = Index(
    text_fields=["title", "text"],
    keyword_fields=["id", "course"],
).fit(docs)

results = index.search(
    "how do I start docker compose",
    filter_dict={"course": "de"},     # exact-match keyword filter
    boost_dict={"title": 3.0, "text": 1.0},  # per-field boosts
    num_results=5,
)
for r in results:
    print(r["score"], r["title"])

Each result is a shallow copy of the original document dict with an added "score" key.

How it works

  • Tokenizer — lowercased word/number tokens; keeps + . # _ - inside a token so c++, node.js, f-string survive (a token must start with a letter/digit). Drops 1-character tokens and a small English stop-word list (both overridable).
  • Inverted index — built once in fit(). A query only scores documents that actually contain a query term, so search is fast even on large corpora.
  • Ranking — BM25-lite: each query term contributes boost * idf * (term_frequency / sqrt(field_length)) per field. IDF and document frequencies are computed over the filtered candidate set.

Customizing

Index(
    text_fields=["title", "text"],
    stop_words={"the", "a", "an"},          # replace the default stop words
    tokenizer=lambda s: s.lower().split(),  # or plug in your own tokenizer
)

License

WTFPL.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zerosearch-0.1.0.tar.gz (54.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zerosearch-0.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file zerosearch-0.1.0.tar.gz.

File metadata

  • Download URL: zerosearch-0.1.0.tar.gz
  • Upload date:
  • Size: 54.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for zerosearch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 06b015a4d631dda8560020ebcfc25d5c2690d6e1e1e51231cf71720fc82d1240
MD5 a2ec39bcb980d4f9891e188b6e5dfb47
BLAKE2b-256 5dcba7bea07a22e2704be791375fca82cd8f07ab36e18c3e4d60ce8d2d122f4f

See more details on using hashes here.

File details

Details for the file zerosearch-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: zerosearch-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for zerosearch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24e966add1c85de84109b37faa01a9ff4fac45f39594324f8febcdf6e67d92c0
MD5 9d1e9b6ac78f64fa4ad387e3079ac7d3
BLAKE2b-256 2b2a1dc34134928370a9a7eb6e62d0f4e7dd101359f5e0f6a3201af2b22663df

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page