rank-bm25-rs
rank_bm25 with a Rust index: the same BM25 scores, down
to the last bit, 600–950 times faster per query on 10,000 to 100,000 documents, and indexing
4–6 times faster (benchmarks).
rank_bm25 is downloaded about nine million times a month, for example through LangChain's
BM25Retriever and crawl4ai. It scores a query by looking up each term in a dictionary of every
document, in Python, so a query over 100,000 documents takes about 190 ms. rank-bm25-rs keeps an
inverted index and visits only the documents that contain a query term, while performing every
remaining floating-point operation in NumPy's order, so the scores, idf values and rankings are
exactly those of rank_bm25.
The Python package is rank-bm25-rs; the Rust crate is rank-bm25. This is an independent
project, not affiliated with the author of rank_bm25.
Python
pip install rank-bm25-rs
Change the import and keep the rest of your code:
from rank_bm25_rs import BM25Okapi # was: from rank_bm25 import BM25Okapi
corpus = ["Hello there good man!", "It is quite windy in London", "How is the weather today?"]
bm25 = BM25Okapi([doc.split(" ") for doc in corpus])
query = "windy London".split(" ")
bm25.get_scores(query)
# array([0. , 0.93729472, 0. ])
bm25.get_top_n(query, corpus, n=1)
# ['It is quite windy in London']
BM25Okapi, BM25L and BM25Plus take the same arguments as in rank_bm25 0.2.2, including
tokenizer, and have the same methods (get_scores, get_batch_scores, get_top_n) and
attributes (idf, doc_freqs, doc_len, avgdl, corpus_size, average_idf). Indexes can be
pickled.
To score many queries at once, get_scores_many runs them on all CPU cores without the GIL and
returns one row per query:
bm25.get_scores_many([["windy", "London"], ["is"]])
# array([[0. , 0.93729472, 0. ],
# [0. , 0.10042443, 0.10946263]])
Set RAYON_NUM_THREADS to limit the threads. The result holds 8 bytes per query and document
(10,000 queries over 100,000 documents take 8 GB), so for large workloads pass the queries in
chunks:
for start in range(0, len(queries), 1000):
scores = bm25.get_scores_many(queries[start : start + 1000])
Code you do not control
When a library imports rank_bm25 itself, call install() before importing it:
import rank_bm25_rs
rank_bm25_rs.install() # `import rank_bm25` now returns rank_bm25_rs
from langchain_community.retrievers import BM25Retriever
Modules that imported rank_bm25 before the call keep the original.
Rust
[dependencies]
rank-bm25 = "0.1"
use rank_bm25::{Builder, Params};
let mut builder = Builder::new(Params::okapi());
for doc in ["Hello there good man!", "It is quite windy in London", "How is the weather today?"] {
builder.add_document(doc.split(' '));
}
let index = builder.build()?;
let scores = index.get_scores(&["windy", "London"]);
Params::l() and Params::plus() select the other variants; the fields hold k1, b and the
variant's epsilon or delta. Index is Send + Sync.
Compatibility
The tests compare rank-bm25-rs with rank_bm25 0.2.2 and fail on any difference in any score:
corpora from one to thousands of documents, empty documents, repeated and unknown query terms,
property-based tests, and every variant with parameters that make rank_bm25 return NaN or
negative scores (k1 = 0, b > 1, negative delta and epsilon). A separate script compares
the full CNN/DailyMail, XSum and PubMed test splits; CI runs it on Linux, macOS and Windows.
rank_bm25's own code, bundled in the package, runs whenever the Rust index could behave
differently, so results, NumPy warnings and errors do not change; only the speed-up is lost:
- the corpus is not an exact list or tuple of exact lists or tuples of
str(for example a generator, or integer tokens); - a parameter is not a finite
intorfloat(for example a NumPy scalar or NaN), or an integer parameter could overflow NumPy's integer arithmetic; - a subclass overrides
_initializeor_calc_idf, ork1,b,deltaoravgdlwere changed after construction; - a query is not a list or tuple of
str, ordoc_idsis not a list, tuple or array of integers; - a query produces NaN or infinite scores, or NumPy's error handling (
np.errstate) is not the default.
Known differences:
- The Rust index does not see changes made to the
idf,doc_freqsordoc_lenattributes after construction;rank_bm25would use a modifiedidfdictionary. - Python 3.10 or newer is required. On free-threaded Python the module keeps the GIL enabled.
Development
cargo test -p rank-bm25
cd bindings/python
uv sync --group dev
uv run pytest
cd benchmarks && uv run --group bench python parity.py # full test splits
License
Apache-2.0, the same licence as rank_bm25, whose scoring this project ports. See
NOTICE.
Metadata
Release files for rank-bm25-rs 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| rank_bm25_rs-0.1.1.tar.gz | 91.6 kB | With provenance | Details for rank_bm25_rs-0.1.1.tar.gz |
Built distributions (wheels)
Total release size: 2.7 MB
Release files / rank_bm25_rs-0.1.1.tar.gz
| Download URL | rank_bm25_rs-0.1.1.tar.gz |
|---|---|
| Size | 91.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
619e37cf3dc134e399786a80b7dbbd96f1e382199038aaa423f7e416165e5df6
|
|
BLAKE2b-256 checksum How to use checksums |
40ce677ceef355b7c2e2f5fe82c5636428a3b162fc8766377be7d819ca370431
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logRelease files / rank_bm25_rs-0.1.1-cp310-abi3-win_amd64.whl
| Download URL | rank_bm25_rs-0.1.1-cp310-abi3-win_amd64.whl |
|---|---|
| Size | 206.2 kB |
| Tags | CPython 3.10 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
94a13c3b24a27d3441cf9d6a75f56d3e240c6c3efaf1ed7e49d23f11124f6654
|
|
BLAKE2b-256 checksum How to use checksums |
f1984ea7ef22779ccac3728e4670c119fa6baccb64d4dbcaff28fb8902ce32b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logRelease files / rank_bm25_rs-0.1.1-cp310-abi3-musllinux_1_2_x86_64.whl
| Download URL | rank_bm25_rs-0.1.1-cp310-abi3-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 554.8 kB |
| Tags | CPython 3.10 Linux musl 1.2+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
ecfa4369340ca69bb6a1101f3a6c341b08a4c87c822d838310da3d6d76b2014a
|
|
BLAKE2b-256 checksum How to use checksums |
79d2bd29c21c4517417670f4dba72daafa3752dfc2db8446c5e3ed55dc462fd3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logRelease files / rank_bm25_rs-0.1.1-cp310-abi3-musllinux_1_2_aarch64.whl
| Download URL | rank_bm25_rs-0.1.1-cp310-abi3-musllinux_1_2_aarch64.whl |
|---|---|
| Size | 520.1 kB |
| Tags | CPython 3.10 Linux musl 1.2+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
ffd245d67ba60765e95ab1c3998946f55a730fc7d4c37d0e11f1447c63bc8aca
|
|
BLAKE2b-256 checksum How to use checksums |
0cd2282a4d82dd6c1f3afeb0cae7f2fa9219345cffab396341d63b270b40e3a7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logRelease files / rank_bm25_rs-0.1.1-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | rank_bm25_rs-0.1.1-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 342.6 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
abeaebdbd99a15bb6526591903bf628f6d824e15746ba44b574947d797cf56b2
|
|
BLAKE2b-256 checksum How to use checksums |
b24b77fe281544dc716f20d2bfa960f095c27b1c6f01de4043d2a2d3c260779b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logRelease files / rank_bm25_rs-0.1.1-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
| Download URL | rank_bm25_rs-0.1.1-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl |
|---|---|
| Size | 341.1 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
587498558857fbc531e43b3606e37dccdce555a53c38d557ce47b990acd4ef5b
|
|
BLAKE2b-256 checksum How to use checksums |
c0626390b1dc03ac433af672e594eda7cdcf195b8cf840aef18d4d68f6878461
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logRelease files / rank_bm25_rs-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
| Download URL | rank_bm25_rs-0.1.1-cp310-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 301.0 kB |
| Tags | CPython 3.10 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
a72b76a3e5d0a3ed24a9a78d37f78a31c6956a99e92dd5462625ff5fda401950
|
|
BLAKE2b-256 checksum How to use checksums |
d98b0d89c644d1d0a4bb12d4810c59879527e74b0af9e6e8610b53690a5caa68
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logRelease files / rank_bm25_rs-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl
| Download URL | rank_bm25_rs-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 304.1 kB |
| Tags | CPython 3.10 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
09e6729a5617d1744d41ee9795d98fd074c6e16a5de9b103f76ee86b43bb8af2
|
|
BLAKE2b-256 checksum How to use checksums |
16ec308948a6bf3a137dbed021237d6e47810637fc3114f41b989aedb333f94b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logSecurity for rank-bm25-rs 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 8 files in this release have been uploaded with provenance metadata.
Attestation for rank_bm25_rs-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.