Skip to main content

rank-bm25-rs

CI PyPI Crates.io License

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.

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 int or float (for example a NumPy scalar or NaN), or an integer parameter could overflow NumPy's integer arithmetic;
  • a subclass overrides _initialize or _calc_idf, or k1, b, delta or avgdl were changed after construction;
  • a query is not a list or tuple of str, or doc_ids is 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_freqs or doc_len attributes after construction; rank_bm25 would use a modified idf dictionary.
  • Python 3.10 or newer is required.

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.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rank-bm25-rs 0.1.0
File Size Uploaded
rank_bm25_rs-0.1.0.tar.gz 90.9 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for rank-bm25-rs 0.1.0
File
rank_bm25_rs-0.1.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
rank_bm25_rs-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details
rank_bm25_rs-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl CPython 3.10 abi3 Linux musl 1.2+ ARM64 Details
rank_bm25_rs-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details
rank_bm25_rs-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details
rank_bm25_rs-0.1.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details
rank_bm25_rs-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details

Total release size: 2.6 MB

Release files / rank_bm25_rs-0.1.0.tar.gz

Download URL rank_bm25_rs-0.1.0.tar.gz
Size 90.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8943e261da9e49b09ca533938ecac23d412fce42da4440a71c04721b8299fe26
BLAKE2b-256 checksum
How to use checksums
c3bb1eec08cf952d18aeb6f486f2806b5555fdd3b492f1842aabcc3e9e5897f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release files / rank_bm25_rs-0.1.0-cp310-abi3-win_amd64.whl

Download URL rank_bm25_rs-0.1.0-cp310-abi3-win_amd64.whl
Size 203.8 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
e60044360cb155601c35416653f2246da791fb65e7cf4125d250efdc42479e14
BLAKE2b-256 checksum
How to use checksums
15788d594d887079fdcbcecaaed14c4010584caffa3dbc497c0a25a2a9b9fd1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release files / rank_bm25_rs-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl

Download URL rank_bm25_rs-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl
Size 551.8 kB
Tags CPython 3.10 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
15c7900705a16a43756a315cb33d2b267cc6b2f9383efed5ada1b506191a3827
BLAKE2b-256 checksum
How to use checksums
6d6b9eb0ac36fbd687c56c39264459faad0333cd803e9340cdc98ca3a24e9b93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release files / rank_bm25_rs-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl

Download URL rank_bm25_rs-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl
Size 517.6 kB
Tags CPython 3.10 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
b0240d6906ae105e54071539fb5cc1f13a8736fea39cff91b90b90ada38aef80
BLAKE2b-256 checksum
How to use checksums
ca4ee0bc7e0bb8bcc146062c1c39e0a30c0c305cbca37fdd6e90d7c90b1d478f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release files / rank_bm25_rs-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL rank_bm25_rs-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 339.7 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
758b5eb15639f4c672d022e68e9c4bc89bfccdbf0ebccd230a849058d320df13
BLAKE2b-256 checksum
How to use checksums
8f252a71ac6eba377b0a69bbdd4ed50f391fd6c2c9935228bf0e4b8c99b21640
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release files / rank_bm25_rs-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL rank_bm25_rs-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 338.6 kB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
963044c1a355a987a28eb3da474ed02f68dccc3a68bdeb6065d08dd82420e7f4
BLAKE2b-256 checksum
How to use checksums
e1d4a24abec34e069d13b4633201236f5ae082e6eac25276439a2105c0f0adf5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release files / rank_bm25_rs-0.1.0-cp310-abi3-macosx_11_0_arm64.whl

Download URL rank_bm25_rs-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
Size 298.7 kB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
77f7b167244788c946ebbe3314c9a765581440967c8c926165611b192ebdae91
BLAKE2b-256 checksum
How to use checksums
46f382a3a2a1c16dc815a455424b3cc4687e13a16f857ae73aa03e6d1f1a4bbe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release files / rank_bm25_rs-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl

Download URL rank_bm25_rs-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl
Size 302.1 kB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
78da76a242c8268b31799f1259ecb7805299fee0edd1512bfb83c42a61f73812
BLAKE2b-256 checksum
How to use checksums
71c939d8449954243eddb7891b48525d095ed3bd37fd748c989a19f5d6d421f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

8 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page