Skip to main content
Back

bm25s Beats Elasticsearch at Keyword Search 500x Faster in Pure Python

bm25s brings Elasticsearch-level lexical search speed to pure Python with no Java, no PyTorch, and a one-line install

Pro

  • bm25s is a pure-Python BM25 library achieving up to 500x speedup over rank-bm25 using precomputed sparse matrices -- no Java or PyTorch needed.
  • A new MCP server lets you expose any bm25s index as a tool for LLM agents like Claude Desktop with a single CLI command.
  • Memory-mapped index mode cuts RAM usage from 4.36 GB to 0.49 GB on 2M-document corpora with negligible retrieval speed loss.
  • Supports 5 BM25 variants (Robertson, ATIRE, BM25L, BM25+, Lucene) and integrates with Hugging Face Hub for index sharing.
  • A new pip install bm25 package bundles all dependencies for a simpler getting-started experience.
  • Recent research confirms BM25 outperforms top dense embedding models on financial/domain-specific corpora, keeping lexical search highly relevant in 2026.

If you've ever built a RAG pipeline or a search feature and reached for rank-bm25, you know the pain: it works, but it crawls on anything larger than a toy corpus. bm25s (BM25-Sparse) is a pure-Python reimplementation of BM25 that trades the slow, per-query scoring loop for a precomputed sparse matrix, delivering speeds that rival Elasticsearch -- without a JVM in sight.

The problem with existing BM25 tools

BM25 -- Best Match 25 -- is a probabilistic ranking function developed in the 1990s that scores documents against a query using term frequency, inverse document frequency, and document-length normalization. Despite being published in 1994, it remains the default relevance scoring function in Elasticsearch, Apache Solr, and virtually every production search system that handles keyword queries. The Python ecosystem has two dominant options, and both have a catch.

  • Elasticsearch: Scales to billions of documents, but requires spinning up an Apache Lucene server in Java and connecting via a separate Python client. Heavyweight for local or research use.
  • rank-bm25: Pure Python, easy to install, but scores documents one query at a time -- it falls apart on corpora exceeding a million documents.

bm25s achieves up to 500x speedup compared to the most popular Python-based BM25 library. The key insight is architectural: instead of computing scores at query time, bm25s precomputes relevance scores for every token in the vocabulary across every document and stores them in a sparse matrix. At query time, retrieval becomes a simple sparse matrix lookup and sum -- operations that NumPy and SciPy are extremely fast at.

How the speedup actually works

The core trick is called eager sparse scoring. Traditional BM25 implementations iterate over query terms at search time and score each document on the fly. bm25s flips this: it scores all documents for all vocabulary terms during indexing, storing results in a compressed sparse matrix. For BM25 variants that would produce negative scores (which can't be stored sparsely), it shifts scores by a constant offset computed from the formula, then reverses the shift at retrieval time -- preserving exactness while keeping the matrix sparse.

The library uses NumPy sparse matrices for eager scoring and optional Numba JIT compilation, and supports multiple BM25 variants including BM25, BM25-L, and BM25+. The numba backend, introduced in v0.2.0, adds roughly a 2x additional speedup on larger datasets.

Here's the minimal usage pattern:

import bm25s
import Stemmer
corpus = ["transformer models are powerful", "BM25 is fast for lexical search"]
stemmer = Stemmer.Stemmer("english")
# Tokenize and index
corpus_tokens = bm25s.tokenize(corpus, stopwords="en", stemmer=stemmer)
retriever = bm25s.BM25()
retriever.index(corpus_tokens)
# Query
query_tokens = bm25s.tokenize("fast search", stemmer=stemmer)
results, scores = retriever.retrieve(query_tokens, k=2)
# Save and reload
retriever.save("my_index", corpus=corpus)
retriever = bm25s.BM25.load("my_index", load_corpus=True)

Benchmarks: the numbers that matter

Benchmarks were run single-threaded on an Intel Xeon @ 2.70GHz across 15 BEIR datasets (BEIR is a standard heterogeneous benchmark for information retrieval). The results are stark:

Datasetbm25s (QPS)Elasticsearch (QPS)rank-bm25 (QPS)
scifact9522047
nfcorpus119645224
arguana573132
fiqa507164
msmarco12110.07

On large corpora like MSMARCO (8M+ documents) and Natural Questions (2M+ documents), bm25_pt (the PyTorch implementation) runs out of memory entirely. bm25s handles both with its memory-mapped index mode.

Memory-mapped indexes: big corpora on small machines

One of the more practical features is mmap support. Instead of loading the full index into RAM, bm25s can treat the index as a memory-mapped file -- the OS pages in only what's needed. On the NQ dataset (2M+ documents, 100M+ tokens), this drops RAM usage from 4.36 GB to 0.49 GB post-index load, with essentially no retrieval speed penalty (21.09s vs 20.22s). Loading the index itself drops from 8.6 seconds to 0.53 seconds.

# Load a large index without blowing up RAM
retriever = bm25s.BM25.load("big_index", mmap=True)

What it's good at -- and where it stops

bm25s is the right tool when you need:

  • Fast keyword search with no infrastructure overhead
  • The lexical retrieval leg of a hybrid RAG pipeline
  • Reproducible IR experiments across BM25 variants (Robertson, ATIRE, BM25L, BM25+, Lucene)
  • Sharing pre-built indexes via Hugging Face Hub
  • Exposing a search index as an MCP tool for LLM agents

BM25 is highly effective when queries contain rare, distinctive terms -- product SKUs, person names, error codes, regulatory references -- but has no notion of meaning: a query for "automobile repair" will miss documents that use "car maintenance" exclusively. BM25 is lexical-only; for semantic coverage you'll want to combine it with dense embeddings (e.g., SentenceTransformers) for hybrid search, with bm25s as the lexical component.

Recent benchmarks also show that BM25 remains strong for financial documents -- on every metric except Recall@20, BM25 outperforms dense retrieval with text-embedding-3-large, one of the strongest commercial embedding models available. Financial documents contain precise, domain-specific terminology that lexical matching captures effectively.

Beyond search: MCP server and CLI

Recent additions push bm25s beyond a library into a deployable tool. The built-in MCP server (Model Context Protocol -- a standard for exposing tools to LLM agents) lets you serve any bm25s index as a tool that Claude Desktop or any MCP-compatible agent can call directly. Launch it with one command:

bm25 mcp launch --port 8000 --index-dir ./my_index

There's also a full CLI for indexing and searching without writing Python. You can index a CSV, JSONL, or plain text file and start querying it in seconds:

bm25 index documents.csv -o my_index -c text
bm25 search -i my_index "what is machine learning?" -k 10

And for sharing indexes, the Hugging Face integration lets you push and pull pre-built indexes from the Hub with BM25HF.save_to_hub() and BM25HF.load_from_hub() -- useful for reproducibility in research or for distributing production indexes to teammates.

Installation and cost

bm25s is free and MIT-licensed. Install it with:

pip install bm25s          # minimal (NumPy only)
pip install "bm25s[core]"  # adds stemming, progress bars, Numba JIT
pip install bm25           # new high-level package, batteries included

As of a recent update, you can also install BM25 directly with pip install bm25, which bundles all core dependencies and provides the same high-level API. The base install weighs in at 51MB -- the same footprint as rank-bm25 -- compared to 1.2GB for Elasticsearch and 5.3GB for bm25_pt.

The broader takeaway here is that the "you need Elasticsearch for serious BM25" assumption is increasingly outdated. In a RAG pipeline, BM25 typically serves as the fast first-stage retriever that narrows millions of candidate documents down to a manageable shortlist in single-digit milliseconds. bm25s makes that first stage accessible to anyone with a Python environment, no ops team required. For teams building hybrid retrieval systems, it's a strong candidate for the lexical half of the stack.