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 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: , which controls how fast repeated words saturate, and , which controls how much length matters. Real systems use values near and ; the smaller values here keep the arithmetic short.
-
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.
-
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, for both words. Check: a word in every document would get a value near zero.
-
Compute a length correction for each document. The correction is . For D1, , which equals the average, so . For D2, , so . For D3, , so . Check: the longer the document, the larger .
-
Turn each count into a saturating term score. For a word that occurs times, the term score is . For D1, "apple" with gives , and "banana" with gives . For D2, "apple" with gives . For D3, "banana" with gives . Check: in D1 the second "apple" adds only , a smaller step than the first.
-
Multiply by the rarity and add over the query words. D1 scores . D2 scores . D3 scores . 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
For a query of terms and a document , with the count of term in , the length of , and the average length in the collection,
with the inverse document frequency
where is the number of documents and is the number of documents that contain .
In words: each query term contributes its rarity times a saturating count. The constants and 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, , can be negative for common words, which is why the "1 +" is added.
Step 3 in symbols: the length correction
Write for the length of the document relative to the average. The correction is . When the document has average length and . With the length does not matter at all, and with the correction is fully proportional to length.
Step 4 in symbols: saturation
For a fixed document, the term score rises with but never passes . Its limit as grows is . 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
The collection has two documents: "red red" (length 2) and "red blue" (length 2). So , , and the query is "blue".
- The document count of "blue" is , so .
- Use and . Document 2 has "blue" once, and its length equals the average, so .
- The term score is .
- The score for document 2 is .
- 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.
Use and a document of average length, so . The term score becomes .
- : .
- : .
- : .
- : .
- The limit as grows is . Check: 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.
A help centre has 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 and .
- The idf of "password" is .
- The idf of "reset" is .
- The length correction is .
- The term score of "password" is , and of "reset" is .
- The score is .
- Now take article B, which has the same counts but 1,000 words. Its correction is . The term scores are and .
- The score of article B is .
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 , what is its score? Answer: . The term score is . The score is .
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 or is always better. In fact these are tuned settings. A large lets repeated words count for more, which helps long, repetitive documents. A large penalises long documents strongly, which helps when documents vary a lot in length. The usual values, near 1.2 to 2.0 and 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 , which rises with the count but never passes , so repetition saturates.
- The length correction 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 and 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 , so the first few occurrences add the most and later ones add less and less.
QuestionComputation: with and a document of average length, what is the term score for a word that occurs 3 times?Show answer
With , the term score is .
QuestionWhy: why does BM25 divide the term count by a length correction instead of using the raw count, and what happens if ?Show answer
Long documents contain more words, so they have higher raw counts on the same topic. The length correction grows with document length, which lowers the term score of long documents and keeps the score comparable across lengths. If , the length term drops out, 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 , which gives long documents too little length penalty, combined with a large , which lets repeated words count for more. The fix is to raise , toward 0.75, so long documents are penalised for their length, and to check against labelled queries. The change should be verified on queries where the right answer is known, not chosen by eye.
Sources
- Robertson and Zaragoza, "The Probabilistic Relevance Framework: BM25 and Beyond" (2009): https://doi.org/10.1561/1500000019
- Manning, Raghavan and Schütze, Introduction to Information Retrieval, Chapter 6: https://nlp.stanford.edu/IR-book/
- Okapi BM25, Wikipedia: https://en.wikipedia.org/wiki/Okapi_BM25