BM25 Retrieval: How the Classic Ranking Formula Works

BM25 Retrieval: How the Classic Ranking Formula Works

BM25 retrieval is the ranking formula behind most keyword search, and it remains the baseline every newer retriever is measured against. In plain terms, BM25 scores each document by the query words it contains, giving rare words more weight, letting repeated words count less each time, and adjusting for document length. If you build search or RAG, you need to understand it, because it is fast, explainable and often surprisingly hard to beat.

BM25, short for Best Matching 25, grew out of the probabilistic relevance framework developed by Stephen Robertson, Karen Spärck Jones and colleagues at City University London. The formula reached its standard form at the TREC-3 conference in 1994, and Robertson and Zaragoza later set out the full theory in their 2009 monograph on the probabilistic relevance framework. Decades later it still ships as the default scorer in search libraries such as Apache Lucene.

Key takeaways

  • BM25 scores documents by matching query terms, weighted by rarity, frequency and document length.
  • Term frequency saturates, so repeating a word cannot inflate a score without limit.
  • Length normalisation stops long documents winning just for being long.
  • The parameters k1 and b control saturation and length normalisation; common defaults are 1.2 and 0.75.
  • BM25 is the strongest baseline for exact-match queries and the first stage of most hybrid systems.

How BM25 retrieval works

BM25 treats a document as a bag of words: the order of the words does not matter, only which words appear and how often. For a query, it adds up a score for each query term the document contains. A document that matches several rare query terms scores highly; a document that matches only a common word scores little.

Each term’s contribution has three parts. The first is inverse document frequency, or IDF: a term that appears in few documents is more informative than one that appears everywhere. The second is a saturating term-frequency factor: the first few occurrences of a word add a lot, later ones add less. The third is length normalisation: a match in a short document counts more than the same match in a very long one.

The Wikipedia page on Okapi BM25 gives the full formula with each symbol defined, and it is the best single reference if you want to implement the scorer yourself.

Best for: keyword search, exact-match queries, first-stage retrieval in RAG, and any system where results must be explainable.

Inverse document frequency: rare words matter more

IDF is the idea that a word’s value depends on how widely it is used. In a collection of cooking articles, the word “recipe” appears almost everywhere and tells you little. The word “saffron” appears rarely and tells you a lot. BM25 therefore weights “saffron” far more heavily than “recipe” when both appear in a query.

The classic IDF formula uses the logarithm of the ratio between the number of documents and the number containing the term, with a small smoothing constant to avoid division by zero. Modern implementations add one inside the logarithm so that a term appearing in every document still gets a small positive weight rather than a negative one.

This is why BM25 handles distinctive terms so well. A query containing a product name, a person’s name or a technical term gets most of its score from exactly those words, which is usually what the user cares about.

Term frequency saturation: repeats count less

Simple TF-IDF scoring multiplies a term’s weight by how often it appears. That creates an obvious exploit: repeat a word fifty times and the score keeps climbing. BM25 fixes this with saturation. The term-frequency factor rises quickly for the first few occurrences and then levels off towards a ceiling.

The ceiling is controlled by the parameter k1. With a low k1, the score saturates fast and the tenth occurrence adds almost nothing. With a high k1, repeats keep adding score for longer. The usual default of 1.2 sits in the middle and works well across many collections, though tuning it on your own data is worth doing.

Saturation is one of the main reasons BM25 beats plain TF-IDF in practice. It reflects a real fact about language: a document that mentions a word eleven times is more likely to be about that topic than one that mentions it once, but it is not eleven times more likely.

Length normalisation: long documents do not get a free pass

Long documents contain more words, so they match more query terms by chance. Without correction, a long document would outrank a short, focused one even when the short one is more relevant. BM25 corrects for this by scaling term frequency against the document’s length relative to the average length in the collection.

The parameter b controls how strong this correction is, from zero, meaning no normalisation, to one, meaning full normalisation.

Length normalisation matters most when your collection mixes very short and very long documents, such as titles alongside full reports. If all your documents are similar in length, the choice of b matters much less.

Hands typing on a laptop beside a notebook with a ranking list
BM25 turns a simple idea, count the matching words fairly, into a ranking that still anchors modern search.

The BM25 formula in plain language

For each query term, BM25 multiplies the term’s IDF by a frequency factor. The frequency factor is the term count times (k1 + 1), divided by the term count plus k1 times a length factor. The length factor is (1 – b) plus b times the document length divided by the average document length. The document’s total score is the sum of these per-term values.

You do not need to memorise the formula to use BM25 well, but you do need to know what each part does. IDF rewards rare terms. The frequency factor rewards repetition with diminishing returns. The length factor penalises verbosity. Together they produce a ranking that is hard to game by stuffing keywords or padding documents.

BM25 vs TF-IDF

TF-IDF is the older and simpler idea: multiply term frequency by inverse document frequency and add up the results. BM25 keeps that skeleton and adds the two refinements described above, saturation and length normalisation. The difference shows up clearly on real collections.

A document that repeats a query term dozens of times will dominate a TF-IDF ranking, even if it is barely about the topic. Under BM25, the same document gains only a bounded advantage. Similarly, a very long document that mentions every query term once will rank highly under TF-IDF but be pulled down by BM25’s length correction.

That is why BM25 replaced TF-IDF as the default in production search. TF-IDF is still useful as a teaching tool and a quick baseline, but there is rarely a reason to ship it when BM25 is available in the same library.

BM25 vs dense retrieval

Dense retrieval encodes queries and documents as vectors and matches them by meaning rather than by words. It handles synonyms and paraphrases that BM25 misses entirely. But it is weak precisely where BM25 is strong: exact names, codes, numbers and rare terms can be blurred away in a single vector.

The practical answer is usually to use both. Our comparison of dense retrieval vs sparse retrieval explains the trade-off in full, and our guide to hybrid search in RAG shows how to combine BM25 with a dense retriever so each covers the other’s blind spot.

Best for: any retrieval pipeline that needs a reliable, cheap first stage, and any evaluation where a new method must prove it beats the classic baseline.

Tuning k1 and b

Most teams start with the Lucene defaults, k1 of 1.2 and b of 0.75, and those defaults are a reasonable choice. Tuning is worth doing when your documents differ from typical text, for example when they are very short, very long or highly repetitive.

Parameter What it controls Lower value Higher value
k1 Term frequency saturation Repeats stop adding score quickly Repeats keep adding score longer
b Length normalisation strength Long documents penalised less Long documents penalised more
Typical range Both parameters k1 1.2, b 0.5 k1 2.0, b 0.8
Lucene default Both parameters k1 1.2 b 0.75

To tune them, build a small test set of queries with labelled relevant documents, then try a grid of values and measure ranking quality. Keep the test set fixed and avoid tuning on the same queries you report results on. Small collections may not show a clear winner, in which case the defaults are fine.

Using BM25 retrieval in RAG

In a RAG pipeline, BM25 usually serves as the first-stage retriever or as half of a hybrid retriever. It is cheap enough to run over the whole collection on every query, and its results are a strong input to a reranker. Many teams retrieve the top 50 or 100 passages with BM25, then rerank them with a cross-encoder before passing the best few to the language model.

Chunking choices affect BM25 more than many teams expect. Short chunks make term frequencies small and length normalisation less important; long chunks dilute the signal of a single matching term. Our guide to RAG chunking strategies covers how to pick a chunk size, and RAG retrieval latency explains where the time goes when BM25 runs at scale.

Common BM25 mistakes

The most common mistake is indexing the wrong unit. If you index whole documents when users need passages, a long document that mentions a term once in passing can outrank a short passage that is entirely about it. Index the unit you want to retrieve, usually a chunk, not the whole document.

When BM25 retrieval is not enough

BM25 cannot understand meaning, and that is a hard limit. It will miss documents that express the query’s idea in different words, and it cannot match a question in one language to an answer in another. When those failures dominate your query logs, add dense retrieval or query expansion rather than tuning BM25 harder.

Implementation checklist

  • Index retrievable chunks, not whole documents, so each result is a usable passage.
  • Check your analyser: stemming, stop words and case handling shape every score.
  • Start with k1 1.2 and b 0.75, then tune on a labelled test set if results justify it.
  • Measure recall at the depth your generator reads, not at depth 100.
  • Log queries BM25 answers badly and classify them before choosing a fix.
  • Keep BM25 as a baseline when you add dense retrieval, so you can prove the new method helps.
  • Re-index promptly when documents change; BM25 freshness is one of its strengths.

Frequently asked questions

What does BM25 stand for?

BM25 stands for Best Matching 25. It is the 25th weighting scheme in the Okapi team’s numbered series, and it is also known as Okapi BM25 after the retrieval system it was built for.

What are good default values for k1 and b?

The widely used defaults are k1 of 1.2 and b of 0.75, which are the defaults in Apache Lucene. Robertson and Zaragoza report that k1 between 1.2 and 2.0 and b between 0.5 and 0.8 work reasonably well in many cases.

Is BM25 still used in modern search?

Yes. BM25 is the default scorer in Lucene, Elasticsearch and OpenSearch, and it remains the standard baseline and first-stage retriever in most RAG systems, often combined with dense retrieval.

Does BM25 understand synonyms?

No. BM25 matches exact terms only, so synonyms need query expansion, a synonym list or a dense retriever running alongside it.

How is BM25 different from TF-IDF?

BM25 adds term frequency saturation and document length normalisation to the TF-IDF idea. Those two refinements stop keyword stuffing and long-document bias from distorting the ranking.

Can BM25 work for RAG on its own?

Yes, for exact-match heavy collections it can. For paraphrase-heavy questions it will miss relevant passages, so most RAG systems pair it with dense retrieval and a reranker.

Latest articles

Scroll to Top