Search

Search papers and lessons

BM25

Level 3 · 13 min read

BM25 ranks documents for a query by summing each query word's rarity times a saturating, length-corrected count, so repeating a word helps less and more than it should.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Algorithms
  • Calculus
  • Language & LLMs
  • Linear algebra
  • ReadReady to readNot yet

TL;DR

BM25 is a formula that scores how well a document matches a short keyword query. It is built on TF-IDF, but it fixes two of its weaknesses. The count of a word stops adding value after a few occurrences, and long documents are corrected for having more words. It is the default ranking function in many search engines, and it is a strong baseline for keyword retrieval.

Builds on: TF-IDF, Bag of Words · Leads to: hybrid search systems that combine keyword and embedding scores, and first-stage retrieval in retrieval-augmented generation

Where it's used

Full-text search in libraries and product catalogues. Search engines such as Elasticsearch and Lucene use BM25 as their default text ranking. A catalogue search for "wireless headphones noise cancelling" ranks each product by its description, title and reviews, using the same formula, and the ranking runs over millions of items.

Retrieval for question answering. A retrieval-augmented system first finds a few passages that might answer a question, and then gives them to a language model. Many such systems use BM25 for this first pass, because it finds exact technical words such as product codes or error messages that a meaning-based search can miss.

Hybrid search. Many production systems compute a BM25 score and an embedding similarity for each document, and combine the two. BM25 supplies the exact-word signal, and the embedding supplies the meaning signal, as a dense retriever does. The weights used to combine them are tuned on labelled queries, scored by precision and recall.

How it works

Take a tiny collection of three documents. Each document is a list of words, and the lengths are counted in words. The average length of the collection is (4+2+6)/3=4(4 + 2 + 6)/3 = 4 words.

  • D1: "apple apple banana cherry" (length 4)
  • D2: "apple date" (length 2)
  • D3: "banana banana cherry date fig fig" (length 6)

The query is "apple banana". The formula uses two settings: k1=1k_1 = 1, which controls how fast repeated words saturate, and b=0.5b = 0.5, which controls how much length matters. Real systems use values near k1=1.2k_1 = 1.2 and b=0.75b = 0.75; the smaller values here keep the arithmetic short.

  1. Count each query word in each document. D1 has "apple" twice and "banana" once. D2 has "apple" once and no "banana". D3 has "banana" twice and no "apple". Check: each count is taken from the bag of words, so word order plays no part.

  2. Compute the rarity of each query word. Two of the three documents contain "apple", so its document count is 2. Two also contain "banana". Using the form of inverse document frequency that Lucene uses, which is a Logarithm of a ratio, idf=ln⁡ ⁣(1+N−n+0.5n+0.5)=ln⁡ ⁣(1+3−2+0.52.5)=ln⁡1.6≈0.470\mathrm{idf} = \ln\!\left(1 + \frac{N - n + 0.5}{n + 0.5}\right) = \ln\!\left(1 + \frac{3 - 2 + 0.5}{2.5}\right) = \ln 1.6 \approx 0.470 for both words. Check: a word in every document would get a value near zero.

  3. Compute a length correction for each document. The correction is K=k1(1−b+b⋅∣D∣avgdl)K = k_1\left(1 - b + b \cdot \frac{|D|}{\mathrm{avgdl}}\right). For D1, ∣D∣=4|D| = 4, which equals the average, so K=1⋅(0.5+0.5)=1.0K = 1 \cdot (0.5 + 0.5) = 1.0. For D2, ∣D∣=2|D| = 2, so K=0.5+0.25=0.75K = 0.5 + 0.25 = 0.75. For D3, ∣D∣=6|D| = 6, so K=0.5+0.75=1.25K = 0.5 + 0.75 = 1.25. Check: the longer the document, the larger KK.

  4. Turn each count into a saturating term score. For a word that occurs ff times, the term score is f(k1+1)f+K\dfrac{f (k_1 + 1)}{f + K}. For D1, "apple" with f=2f = 2 gives 2⋅22+1=1.333\dfrac{2 \cdot 2}{2 + 1} = 1.333, and "banana" with f=1f = 1 gives 21+1=1.000\dfrac{2}{1 + 1} = 1.000. For D2, "apple" with f=1f = 1 gives 21+0.75=1.143\dfrac{2}{1 + 0.75} = 1.143. For D3, "banana" with f=2f = 2 gives 42+1.25=1.231\dfrac{4}{2 + 1.25} = 1.231. Check: in D1 the second "apple" adds only 1.333−1.000=0.3331.333 - 1.000 = 0.333, a smaller step than the first.

  5. Multiply by the rarity and add over the query words. D1 scores 0.470⋅(1.333+1.000)=1.0970.470 \cdot (1.333 + 1.000) = 1.097. D2 scores 0.470⋅1.143=0.5370.470 \cdot 1.143 = 0.537. D3 scores 0.470⋅1.231=0.5780.470 \cdot 1.231 = 0.578. Check: the ranking is D1, then D3, then D2.

flowchart LR
    Q["Query words"] --> I["Rarity per word, idf"]
    D["Document counts and length"] --> K["Length correction K"]
    K --> T["Saturating term score"]
    I --> S["Multiply and sum over query words"]
    T --> S
    S --> R["Ranked list"]

The key idea is in steps 4 and 5. The count is turned into a score that rises quickly at first and then levels off, and the length correction shrinks the score of long documents. Both changes are small in this example, but they change which document comes first when counts and lengths disagree.

The math

Definition: Okapi BM25

For a query QQ of terms q1,…,qmq_1, \dots, q_m and a document DD, with f(q,D)f(q, D) the count of term qq in DD, ∣D∣|D| the length of DD, and avgdl\mathrm{avgdl} the average length in the collection,

score(D,Q)=∑i=1midf(qi)⋅f(qi,D) (k1+1)f(qi,D)+k1(1−b+b ∣D∣avgdl)\mathrm{score}(D, Q) = \sum_{i=1}^{m} \mathrm{idf}(q_i) \cdot \frac{f(q_i, D) \, (k_1 + 1)}{f(q_i, D) + k_1 \left(1 - b + b \, \dfrac{|D|}{\mathrm{avgdl}}\right)}

with the inverse document frequency

idf(q)=ln⁡ ⁣(1+N−n(q)+0.5n(q)+0.5),\mathrm{idf}(q) = \ln\!\left(1 + \frac{N - n(q) + 0.5}{n(q) + 0.5}\right),

where NN is the number of documents and n(q)n(q) is the number of documents that contain qq.

In words: each query term contributes its rarity times a saturating count. The constants k1k_1 and bb are settings, not facts about the language. The "+ 0.5" terms smooth the rarity so that a word absent from the collection does not produce a division by zero.

Step 2 in symbols: the rarity

The idf is a logarithm of a ratio of the documents that lack the word to those that have it. The "1 +" inside the logarithm keeps the value positive even for a word that appears in more than half of the documents. This variant is the one Lucene uses. The original probabilistic form, ln⁡N−n+0.5n+0.5\ln\frac{N - n + 0.5}{n + 0.5}, can be negative for common words, which is why the "1 +" is added.

Step 3 in symbols: the length correction

Write ρ=∣D∣/avgdl\rho = |D| / \mathrm{avgdl} for the length of the document relative to the average. The correction is K=k1(1−b+bρ)K = k_1 (1 - b + b\rho). When ρ=1\rho = 1 the document has average length and K=k1K = k_1. With b=0b = 0 the length does not matter at all, and with b=1b = 1 the correction is fully proportional to length.

Step 4 in symbols: saturation

For a fixed document, the term score f(k1+1)f+K\dfrac{f (k_1 + 1)}{f + K} rises with ff but never passes k1+1k_1 + 1. Its limit as ff grows is k1+1k_1 + 1. The first few occurrences add the most, and later occurrences add less and less. This is the saturation that raw counts do not have.

Step 5 in symbols: the score

The score is a sum over the query terms of the product of the rarity and the term score. Documents that contain several rare query terms collect several positive terms.

Worked examples

Example
Example 1: two query words in a tiny collection (tiny)

The collection has two documents: "red red" (length 2) and "red blue" (length 2). So N=2N = 2, avgdl=2\mathrm{avgdl} = 2, and the query is "blue".

  1. The document count of "blue" is n=1n = 1, so idf=ln⁡(1+(2−1+0.5)/(1+0.5))=ln⁡(2)≈0.693\mathrm{idf} = \ln(1 + (2 - 1 + 0.5)/(1 + 0.5)) = \ln(2) \approx 0.693.
  2. Use k1=1k_1 = 1 and b=0.5b = 0.5. Document 2 has "blue" once, and its length equals the average, so K=k1=1K = k_1 = 1.
  3. The term score is f(k1+1)/(f+K)=1⋅2/(1+1)=1f (k_1 + 1) / (f + K) = 1 \cdot 2 / (1 + 1) = 1.
  4. The score for document 2 is 0.693⋅1=0.6930.693 \cdot 1 = 0.693.
  5. Document 1 does not contain "blue", so its score is 0.

Interpretation: a query word that appears in only one document is enough to rank that document first. The repeated "red" in document 1 is irrelevant to this query.

Example
Example 2: saturation as counts grow (standard)

Use k1=1.2k_1 = 1.2 and a document of average length, so K=1.2K = 1.2. The term score f(k1+1)/(f+K)f (k_1 + 1) / (f + K) becomes 2.2f/(f+1.2)2.2 f / (f + 1.2).

  1. f=1f = 1: 2.2⋅1/2.2=1.0002.2 \cdot 1 / 2.2 = 1.000.
  2. f=2f = 2: 2.2⋅2/3.2=4.4/3.2=1.3752.2 \cdot 2 / 3.2 = 4.4 / 3.2 = 1.375.
  3. f=5f = 5: 2.2⋅5/6.2=11/6.2=1.7742.2 \cdot 5 / 6.2 = 11 / 6.2 = 1.774.
  4. f=20f = 20: 2.2⋅20/21.2=44/21.2=2.0752.2 \cdot 20 / 21.2 = 44 / 21.2 = 2.075.
  5. The limit as ff grows is k1+1=2.2k_1 + 1 = 2.2. Check: f=20f = 20 gives 2.075, which is close to it.

Interpretation: going from 1 to 2 occurrences adds 0.375, but going from 5 to 20 adds only 0.30 over fifteen extra occurrences. In a raw count, a document with 20 occurrences would score 20 times as much as one with 1. Under BM25 it scores about twice as much, which matches the intuition that a word repeated 20 times is not 20 times more about the topic.

Example
Example 3: a help-centre search (realistic)

A help centre has N=10,000N = 10{,}000 articles and an average length of 400 words. The query is "reset password". The word "password" appears in 800 articles and "reset" in 2,500. Article A has 300 words, with "password" four times and "reset" twice. The numbers are illustrative. Use k1=1.2k_1 = 1.2 and b=0.75b = 0.75.

  1. The idf of "password" is ln⁡(1+(10,000−800+0.5)/(800+0.5))=ln⁡(12.49)≈2.525\ln(1 + (10{,}000 - 800 + 0.5)/(800 + 0.5)) = \ln(12.49) \approx 2.525.
  2. The idf of "reset" is ln⁡(1+(10,000−2,500+0.5)/(2,500+0.5))=ln⁡(4.00)≈1.386\ln(1 + (10{,}000 - 2{,}500 + 0.5)/(2{,}500 + 0.5)) = \ln(4.00) \approx 1.386.
  3. The length correction is K=1.2(1−0.75+0.75⋅300/400)=1.2⋅0.8125=0.975K = 1.2 (1 - 0.75 + 0.75 \cdot 300/400) = 1.2 \cdot 0.8125 = 0.975.
  4. The term score of "password" is 4⋅2.2/(4+0.975)=8.8/4.975≈1.7694 \cdot 2.2 / (4 + 0.975) = 8.8 / 4.975 \approx 1.769, and of "reset" is 2⋅2.2/(2+0.975)=4.4/2.975≈1.4792 \cdot 2.2 / (2 + 0.975) = 4.4 / 2.975 \approx 1.479.
  5. The score is 2.525⋅1.769+1.386⋅1.479≈4.466+2.050=6.522.525 \cdot 1.769 + 1.386 \cdot 1.479 \approx 4.466 + 2.050 = 6.52.
  6. Now take article B, which has the same counts but 1,000 words. Its correction is K=1.2(0.25+0.75⋅2.5)=2.55K = 1.2 (0.25 + 0.75 \cdot 2.5) = 2.55. The term scores are 8.8/6.55≈1.3448.8 / 6.55 \approx 1.344 and 4.4/4.55≈0.9674.4 / 4.55 \approx 0.967.
  7. The score of article B is 2.525⋅1.344+1.386⋅0.967≈3.393+1.341=4.732.525 \cdot 1.344 + 1.386 \cdot 0.967 \approx 3.393 + 1.341 = 4.73.

Interpretation: the two articles have the same words, but the shorter one scores higher, because the same counts are a larger share of a short text. Without the length correction, the long article would win on sheer volume.

Try it: a word appears in 1 of 100 documents, and it appears once in a document of average length. Using k1=1.2k_1 = 1.2, what is its score? Answer: idf=ln⁡(1+99.5/1.5)=ln⁡(67.33)≈4.21\mathrm{idf} = \ln(1 + 99.5 / 1.5) = \ln(67.33) \approx 4.21. The term score is 1⋅2.2/(1+1.2)=11 \cdot 2.2 / (1 + 1.2) = 1. The score is 4.21⋅1=4.214.21 \cdot 1 = 4.21.

Common mistakes and tradeoffs

It is tempting to think BM25 is a different kind of search from TF-IDF. In fact it is the same idea with a saturating count and a length correction. Its rarity term is the familiar idf, and a reader who knows TF-IDF can follow every step.

It is tempting to think a larger k1k_1 or bb is always better. In fact these are tuned settings. A large k1k_1 lets repeated words count for more, which helps long, repetitive documents. A large bb penalises long documents strongly, which helps when documents vary a lot in length. The usual values, k1k_1 near 1.2 to 2.0 and bb near 0.75, come from experiments and are a starting point, not a law.

It is tempting to think BM25 understands meaning. In fact it matches exact words after tokenisation. "Car" and "automobile" get no credit for each other, and word order is discarded. Stemming and synonym lists can widen the match, but they do not give BM25 a notion of meaning.

It is tempting to think the idf must be non-negative. In fact the formula given by Robertson and Zaragoza can go negative for words that appear in more than half the documents, which is why most implementations add 1 inside the logarithm. Use the same variant on both the index and the query side.

BM25 is fast, needs no training data, and works well for keyword queries and exact technical terms. Use it as the first pass of a search system, or whenever users type specific words. Its main limit is vocabulary mismatch: a query and a relevant document that use different words will not match. The main alternatives are dense embedding search, which captures meaning, and hybrid systems that combine both.

History

In the 1970s and 1980s, Stephen Robertson and Karen Spärck Jones developed a probabilistic model of relevance. It asked, for each word, how much more often it appears in documents that match a query than in documents that do not. Their work built on the inverse document frequency idea that Spärck Jones had introduced in 1972.

The Okapi information retrieval system, built at City University London, tested a series of weighting functions in the Text REtrieval Conference (TREC) evaluations that began in 1992. The weighting function that performed best was given the name BM25, from "best match" and the version number 25 in the series of functions the group tried. Robertson and Zaragoza later wrote a survey of the framework that is the standard reference.

BM25 caught on because it worked well on many collections without tuning, and because it was simple to compute from an Inverted Index. It became the default in widely used open-source search libraries, and it remains a strong baseline for keyword retrieval, even as neural retrieval has grown.

Recap

  • BM25 scores a document for a query by summing, over the query words, each word's rarity times a term score.
  • The term score is f(k1+1)/(f+K)f (k_1 + 1) / (f + K), which rises with the count but never passes k1+1k_1 + 1, so repetition saturates.
  • The length correction KK shrinks the term score of documents longer than average, so that long documents do not win on volume alone.
  • The rarity term is an idf logarithm, and the settings k1k_1 and bb are tuned, usually near 1.2 and 0.75.
  • Read next: Cosine Similarity, which compares documents by their weight vectors in a different way, and retrieval-augmented generation, where BM25 often supplies the first set of passages.

Check yourself

QuestionRecall: what does the saturation in BM25 change compared with raw term counts?Show answer

In raw counts, a word that occurs 20 times scores 20 times as much as a word that occurs once. In BM25 the term score rises with the count but never passes k1+1k_1 + 1, so the first few occurrences add the most and later ones add less and less.

QuestionComputation: with k1=1.2k_1 = 1.2 and a document of average length, what is the term score for a word that occurs 3 times?Show answer

With K=k1=1.2K = k_1 = 1.2, the term score is f(k1+1)/(f+K)=3⋅2.2/(3+1.2)=6.6/4.2≈1.571f (k_1 + 1) / (f + K) = 3 \cdot 2.2 / (3 + 1.2) = 6.6 / 4.2 \approx 1.571.

QuestionWhy: why does BM25 divide the term count by a length correction instead of using the raw count, and what happens if b=0b = 0?Show answer

Long documents contain more words, so they have higher raw counts on the same topic. The length correction KK grows with document length, which lowers the term score of long documents and keeps the score comparable across lengths. If b=0b = 0, the length term drops out, K=k1K = k_1 for every document, and length no longer affects the score.

QuestionApplied: a search engine indexes product reviews, and a query for a rare model code ranks a single very long review first, above short reviews that name the code. Which setting is the likely cause, and what would you change?Show answer

The likely cause is a small bb, which gives long documents too little length penalty, combined with a large k1k_1, which lets repeated words count for more. The fix is to raise bb, toward 0.75, so long documents are penalised for their length, and to check k1k_1 against labelled queries. The change should be verified on queries where the right answer is known, not chosen by eye.

Sources