Rank-BM25#

Examples for rank_bm25 are arranged as a learning path for lexical retrieval rather than as isolated API demonstrations. They use only small in-memory corpora, make no network requests, and keep result identities bound to the index whenever a result could leave the process.

The examples intentionally avoid inventing a plotting layer for a text-ranking API. Sphinx-Gallery still uses plot_*.py filenames, but these examples render their useful output as code and text.

Install#

The full Scikit-Plots distribution already includes Rank-BM25:

python -m pip install scikit-plots

For a smaller installation that owns only the Rank-BM25 package and the Scikit-Plots core dependencies:

python -m pip install scikit-plots-rank-bm25

Do not install the full and partial distributions into the same environment; they own overlapping scikitplot package files. See Scikit-Plots Partial Distributions for the partial-distribution model.

Start here#

  1. Quick start and stable identities — build a BM25Okapi index, retrieve by document ID, see deterministic tie ordering, and verify that an out-of-vocabulary query returns no sparse hits.

  2. Choose a recipe — run the same corpus through BM25Okapi, BM25L, and BM25Plus; inspect the versioned recipe identity and compare rankings without treating one score scale as interchangeable with another.

  3. Sparse retrieval versus dense diagnostics — use BM25.candidate_count, BM25.get_top_ids, and BM25.get_scores for the jobs they actually perform.

  4. Save and reload an index — publish a pre-tokenized index to a temporary directory, reload it, and verify that identities and retrieval survive the round trip.

Which example should I use?#

Goal

Example

Add lexical search to an application

Rank-BM25 Quick Start with Stable Document IDs

Decide among Okapi, BM25L, and BM25+

Compare the Three Rank-BM25 Scoring Recipes

Understand candidate filtering and whole-corpus scores

Sparse Retrieval and Dense BM25 Diagnostics

Persist an index across process starts

Persist and Reload a Rank-BM25 Index