Part 2 · Finding the evidence · Chapter 5
Keyword search and BM25
In one sentence: BM25 scores a document by the query words it contains, giving more credit to rare words, less and less credit for each repeat, and a correction so that long documents do not win just by being long.
Why you should care
BM25 dates from the 1990s and is still the first thing to try. It needs no training, no graphics card and no labelled data. It is fast on hundreds of millions of documents, and you can explain every score by hand. Every retrieval run we submitted to the TREC 2026 RAG track started with BM25.
Words you need
- Term
- One word of the query or the document, after the text is split and cleaned.
- Term frequency (tf)
- How many times a term appears in one document.
- Document frequency (df)
- How many documents in the collection contain the term.
- IDF
- Inverse document frequency. A weight that is high for rare terms and low for common ones.
- Saturation
- Each extra repeat of a term adds less than the one before.
- Length normalization
- A correction for document length, so a long document is not favoured only for its size.
- k1 and b
- The two settings of BM25. k1 controls saturation. b controls length normalization.
- Sparse retrieval
- Search that matches words. The name comes from the word-count vectors, which are mostly zeros.
Core The idea
Chapter 3 showed the inverted index: for each word, the list of documents that contain it. BM25 is the rule that turns those lists into a ranking. It rests on three ideas.
- Rare words tell you more. If the query is “bridge repair” and almost every document says “bridge”, the word “repair” does most of the work. BM25 gives each term a weight, the IDF, that grows as fewer documents contain it.
- Repeats count, but less each time. A document that says “repair” five times is probably more about repair than one that says it once. It is not five times more about it. The credit for repeats flattens out.
- Long documents get a handicap. A long document has more chances to contain any word. BM25 compares each document's length with the average and lowers the credit for long ones.
The score of a document is the sum, over the query terms, of the term's IDF times its repeat credit.
A worked example
A collection has 1,000 documents. The query is “bridge repair”. The word “bridge” is in 100 documents and “repair” is in 10.
The rarer word weighs about twice as much. Now the repeat credit, for a document of average length with k1 = 1.2:
| Times the term appears | 1 | 2 | 3 | 5 | 10 | 20 |
|---|---|---|---|---|---|---|
| Repeat credit | 1.000 | 1.375 | 1.571 | 1.774 | 1.964 | 2.075 |
That is saturation. The credit can never pass k1 + 1 = 2.2, however often the word appears. Now score two documents of average length:
| Document | “bridge” | “repair” | Score |
|---|---|---|---|
| A | 3 times: 2.299 × 1.571 = 3.612 | once: 4.557 × 1.000 = 4.557 | 8.169 |
| B | 10 times: 2.299 × 1.964 = 4.515 | never: 0 | 4.515 |
Document A wins clearly. One use of the rare word beats ten uses of the common one.
Last, length. If document A were twice the average length, with the same word counts, its score would fall from 8.169 to 6.209. The same words in a longer text are a weaker signal.
What BM25 cannot do
BM25 matches words, not meanings. A query for “car” does not find a document that only says “automobile”. A query in English does not find a document in Spanish. This gap is called the vocabulary mismatch problem. Chapter 6 and Chapter 7 are two different answers to it.
Watch and read
- VideoA no nonsense intro to BM25 Abhishek Thakur. A plain walk through the formula. Link checked 15 Nov 2026
- VideoBM25 Algorithm: Overcoming the Limitations of TF-IDF MLWorks. BM25 set against the older TF-IDF weighting. Link checked 15 Nov 2026
- ArticlePractical BM25, Part 2: The BM25 Algorithm and its variables Shane Connelly, Elastic. Each part of the formula, with experiments on k1 and b. Read 15 Nov 2026
- ArticleOkapi BM25 Wikipedia. The formula as we write it above, and its history. Checked 15 Nov 2026
- ToolPyserini The Python toolkit behind the TREC RAG 2026 search service. Checked 14 Nov 2026
For the two videos we confirmed that the link opens and the title and channel match. We have not watched them through.
How we did it at TREC 2026
In the RAG track we did not build an index. The organizers host the ClimbMix-400b collection behind a search service built on Pyserini, and every team sends queries to it. Our first-stage search was BM25 through that service, for every run.
- One query was not enough. Each narrative is a paragraph, not a short query. We sent seven versions of it to BM25 and fused the seven lists (Chapter 7, Chapter 8).
- Our baseline run is BM25 only. The run
isu-bm25-rrf7uses no language model at any step. It is the floor that every other run of ours must beat. - Depth is a choice. First we asked for 100 documents per query, later for 1,000. The service returned 1,000 documents in about four seconds, so depth was cheap.
- We followed the pacing rule. The track's search skill tells clients to send a new request only after the last one has come back, with about a dozen workers at most. Our script does that and backs off when the service says it is busy.
In the Change Detection track we did build our own BM25, over a news stream, and paired it with an embedding model (Chapter 6).
| Document set | Set precision | Set recall | Nugget gain |
|---|---|---|---|
| Track median | 0.3499 | 0.1002 | 0.6268 |
| Our BM25 baseline, seven queries fused, top 100 | 0.2004 | 0.0840 | 0.7139 |
Official scores, 114 runs from 27 teams. Precision and recall on 82 narratives, nugget gain (AutoNuggetizer nuggets) on 25. Source: the RAG tab of our results dashboard.
What surprised us
Try it
- Redo the worked example by hand for a document C with “bridge” once and “repair” twice, at average length. Does it beat document A?
- Install the
rank_bm25package, index twenty sentences of your own and search them. Find a query where the top result is wrong, and say which of the three ideas caused it. - Set b to 0 and then to 1 and run the same queries. Which documents move?
- Write a query whose best answer shares no word with it. Keep it for the lab in Chapter 6.
Check yourself
- Why does “repair” weigh more than “bridge” in the example?
- A document repeats a query word 200 times. What stops it from getting a huge score?
- What does setting b = 0 do?
- Name one kind of query where BM25 fails however well it is tuned.
- Why keep a BM25-only run when you have stronger models?
Answers
- It appears in fewer documents, so its IDF is higher.
- Saturation. The repeat credit can never pass k1 + 1.
- It turns length normalization off. Long and short documents are treated alike.
- A query that uses different words from the relevant documents, or a different language.
- It is the reference point. Without it you cannot tell what the stronger models added or lost.
Go deeper
- Where the formula comes from. BM25 grew out of the probabilistic relevance framework of Robertson, Spärck Jones and colleagues, and was tested in the early TREC years on the Okapi system. “BM” stands for “best match”, and 25 was the number of the variant.
- Tuning. k1 and b should be tuned on held-out topics for your collection. Software packages ship different defaults, so two “BM25” runs may not be the same system. Report the values you used.
- Query expansion with RM3. Run the query, take the frequent words of the top documents, add them to the query and search again. It helps with vocabulary mismatch and it can also drift off topic.
- Fields. BM25F scores the title and the body separately with different weights.
- Learned sparse retrieval. Models such as SPLADE learn which words to add to a document and how much each word should weigh, and still use an inverted index.
- Project idea. On a public test collection, sweep k1 and b over a grid and plot nDCG at 10. How flat is the surface near the best point?