In this article
Before you start
This page is one step of the embeddings and vector search learning path. It uses ordinary arithmetic and one logarithm.
Why interviewers ask this
BM25 is the keyword half of most search stacks, so any question about search, retrieval-augmented systems or recommendations soon reaches it. Interviewers use it to separate candidates who treat retrieval as "embeddings, always" from those who know what a lexical baseline does and where it breaks.
A weak answer says "BM25 is like term frequency–inverse document frequency (term frequency (TF)-inverse document frequency (IDF))" and stops. A strong answer names the three ingredients (word rarity, saturating term frequency, document length), says what the two parameters do, and explains that BM25 matches words, not meaning. A Staff-level answer adds the system view: BM25 is cheap, needs no training, and is usually combined with vector search in hybrid search and followed by a cross-encoder reranker.
BM25 from first principles
- BM25
- BM25 is a ranking function from the probabilistic retrieval family that scores a document for a query by summing, over the query words, a rarity weight multiplied by a term-frequency factor that saturates and is adjusted for document length. It uses only word counts and collection statistics, with no learned vectors.
- Inverse document frequency
- Inverse document frequency (IDF) is a weight that grows as fewer documents in a collection contain a word, so rare words count for more than common ones when documents are scored.
- Term frequency saturation
- Term frequency saturation is the effect that each extra occurrence of a word in a document adds less score than the one before, so the contribution approaches a ceiling instead of growing in a straight line.
In plain words
Imagine you ask a librarian for books about "kiln glazing". The word "the" appears in every book, so it tells the librarian nothing. "Kiln" appears in few books, so a match there matters. A book that says "kiln" twenty times is probably on topic, but a book that says it two hundred times is not ten times better. And a short pamphlet that says "kiln" three times is a stronger match than a thick volume that says it three times. The analogy breaks in one place: the librarian understands meaning, while BM25 only counts words.
In one precise sentence: BM25 sums, over the query words, a rarity weight times a saturating, length-adjusted count.
What is the BM25 formula?
The name comes from the Okapi system, and BM stands for best matching. The scheme grew out of the Okapi system's evaluations in the mid-1990s (Robertson et al., 1995, as cited in the Jurafsky and Martin chapter), and Robertson and Zaragoza (2009) give the full treatment. It was designed because the earlier binary independence model (BIM) was built for short catalog records and abstracts of fairly consistent length and works less well on full-text collections, where counts and length matter.
The full formula, in the form given by Manning, Raghavan and Schütze (section 11.4.3) with the "plus one" idf used by Lucene, is below. Here tf is the term frequency in document d, Ld is the document length in tokens, Lave is the average document length of the collection, N is the number of documents and df is the number containing the word.
Some implementations omit the constant factor in the numerator. Because it multiplies every score by the same number, the ranking is unchanged.
Step 1: Rare words get a bigger weight
The first factor is the inverse document frequency (IDF). Lucene computes it as the natural logarithm of one plus the ratio of documents without the word to documents with it, with half a document added to each side, and its documentation gives this exact form. The "plus one" keeps the weight from going negative.
Input: N and df for one word. Output: one weight. In a collection of 1,000 documents (illustrative), a word found in 1 document weighs 6.5033, one found in 10 weighs 4.5574, and one found in 500 weighs 0.6931.1 The common word still counts a little, but a match on the rare word is worth nearly ten times as much. You now have the weight of each query word; the next step decides how much its count in the document adds.
Step 2: Repeated words saturate instead of counting linearly
The second factor turns the raw count into a bounded number. The parameter k1 is a non-negative tuning knob that Lucene describes as controlling non-linear term frequency normalization, called saturation. As tf grows, the factor tf times (k1 plus 1) over (tf plus k1 times the length term) climbs toward k1 plus 1 and never passes it.
Input: tf and k1. Output: a factor between 0 and k1 plus 1. With Lucene's default k1 of 1.2 and an average-length document, a word used once gives a factor of 1.0000, three times gives 1.5714, ten times 1.9643 and fifty times 2.1484, under the ceiling of 2.2.1 Fifty occurrences earn only 2.1484 times the factor of one occurrence, not fifty times.1 This is the answer to the interview trap: repeating a word more and more adds less and less.
Step-by-step description
- Rare words get a bigger weight.
- Repeats add less each time.
- Long documents are discounted.
- Per-word scores add up.
- Rare words get a bigger idf weight.
- The term frequency factor saturates and stays under k1 plus 1.
- Longer documents are discounted by length.
- Scores add up across the query words.
Step 3: Long documents are discounted by length
Longer documents are discounted by length, because a long document contains more words by chance and the same count means less. The parameter b controls to what degree document length normalizes tf values. Document length normalization is the rescaling of the term count by the ratio of the document's length to the collection's average length. In the Jurafsky and Martin chapter, b ranges from 1, which scales fully by document length, to 0, which does no length scaling.
Input: Ld, Lave and b. Output: a rescaled denominator. Take tf of 3, k1 of 1.2 and b of 0.75 (Lucene's defaults). A document of 50 tokens (illustrative, average 100) gets a factor of 1.7600, an average one 1.5714, and a 200-token one 1.2941.1 With b set to 0 the length would not matter and the 200-token document would score 1.5714, the same as an average one.1
Step 4: Scores add up across the query words
A query has several words. BM25 computes the product of Steps 1 to 3 for each query word and sums them. A document that repeats the rare word can outscore one that matches the rare word once and the common word many times.
Input: per-word weights and factors. Output: one score per document, used to sort. In a 10-document collection (illustrative), a word found in 2 documents weighs 1.4816 and one found in 8 weighs 0.2578.1 Document X has the rare word once and the common word six times and scores 1.9543. Document Y has the rare word twice and no common word and scores 2.0372, so Y ranks higher.1 You now have the whole function: rarity, saturation, length, sum. The documents are then sorted by score, and the top ones are returned.
Knowing it is half the job. Saying it is the other half.
Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.
Start Free Trial →Worked example
The setup is a toy collection of 10 documents with an average length of 100 tokens, the Lucene defaults k1 of 1.2 and b of 0.75, and a query word found in 2 documents. All values are illustrative inputs, and the outputs follow by arithmetic.1 Take tf of 3 in an average-length document: the idf is ln(1 + 8.5 / 2.5) = 1.4816, the tf factor is 3 × 2.2 / (3 + 1.2 × 1) = 1.5714, and the product is 2.3282.1
| Quantity | Value (computed, illustrative inputs) |
|---|---|
| Score at tf 1, average length | 1.4816 |
| Score at tf 3, average length | 2.3282 |
| Score at tf 10, average length | 2.9103 |
| Score at tf 50, average length | 3.1831 |
| Ceiling of the tf factor at k1 of 1.2 | 2.2 |
| tf factor at tf 10 with k1 of 0 | 1.0000 |
| tf factor at tf 10 with k1 of 1.2 | 1.9643 |
| tf factor at tf 10 with k1 of 3 | 3.0769 |
| tf factor at tf 10 with k1 of 100 | 9.1818 |
The last four rows show what k1 does. In the Jurafsky and Martin chapter, a k of 0 reverts to a binary selection of terms plus idf, and a large k gives raw term frequency plus idf. The k1 of 100 row moves toward that, at 9.1818 for a count of 10.1
Text description of the diagram
- The tf factor is 1.0 at one occurrence and 2.1484 at fifty.
- The ceiling of the tf factor at k1 of 1.2 is 2.2.
Trade-offs and edge cases
BM25 trades understanding for speed and simplicity. BM25 is a sparse lexical retriever: it represents each document by which words it contains, not by a dense vector of learned numbers. It scores lexical matches, so a query for "car" does not match a document that only says "automobile" unless the query is expanded. A dense model built on embeddings places both words near each other in a vector space and finds the match, but needs a trained model and a similarity search index. A two-tower model is one way to train such a retriever.
| Question | BM25 | Dense retrieval |
|---|---|---|
| What does it match? | exact words | meaning, as vector closeness |
| What does it need? | collection statistics: N, df, average length | a trained embedding model |
| Typical miss | synonyms and paraphrases | rare names, codes, exact strings |
Two queries show the split. BM25 scores 0 on a document that only says automobile for the query car, because no word is shared, while dense retrieval scores it high. For an exact part code, BM25 scores high on the exact string, while dense retrieval can drift to similar codes.
Text description of the diagram
- BM25 scores 0 on a document that only says automobile for the query car.
- Dense retrieval can drift to similar codes on an exact part code query.
Edge cases follow. Lucene rejects a negative or infinite k1 and a b outside 0 to 1, and k1 and b are tuning parameters, not constants. Production idf differs from the textbook logarithm of N over df. Statistics are per field, and Lucene computes the average length per field. An extension for documents with several fields, such as headline and body, is BM25F (Robertson, Zaragoza and Taylor, 2004).
On evidence, the BEIR (Benchmarking-information retrieval (IR), an information retrieval (IR) benchmark) paper (Thakur et al., 2021) evaluated 18 public datasets and 10 retrieval systems and reports that BM25 is, in the paper's words, "a robust baseline", while re-ranking and late-interaction models do best on average at high computational cost. The same paper reports that dense and sparse retrievers are cheaper to run but often underperform other approaches on its datasets, which points to weak generalization.
How to answer it in an interview
What is BM25 and how does it work?
BM25 is a keyword ranking function. For each query word it multiplies a rarity weight, the idf, by a term-frequency factor, and sums over the query. The factor saturates, controlled by k1, so repeating a word adds less each time, and it is scaled by document length, controlled by b. It needs only word counts from the collection, no training. It matches words, not meaning, so it misses synonyms, which is why I would pair it with vector search and rerank the merged list.
The spoken structure: say it is a lexical score, name the three ingredients, say what k1 and b do, then give the limit and the hybrid fix.
Common mistakes
- "BM25 is just TF-IDF." (wrong) TF-IDF has no saturation knob or length parameter. (right) "BM25 adds k1 saturation and b length normalization on top of the idf idea."
- "More occurrences always means a proportionally higher score." (wrong) The factor has a ceiling. (right) "Each extra occurrence adds less; the factor approaches k1 plus 1."
- "k1 and b are fixed constants." (wrong) They are tuning parameters. (right) "Lucene defaults to k1 of 1.2 and b of 0.75, and I would tune them on my own queries."
- "BM25 understands meaning." (wrong) It counts word matches. (right) "It misses synonyms unless the query is expanded or a dense retriever is added."
- "BM25 is obsolete because we have embeddings." (wrong) The BEIR results above show it as a dependable baseline. (right) "I keep it as a baseline and as the lexical half of hybrid search."
Follow-up questions
BM25 vs TF-IDF: what is the difference?
Both weight words by rarity and frequency. BM25 adds two parameters: k, which balances term frequency and idf, and b, which controls document length normalization, so frequency saturates and long documents are discounted.
What is BM25 in hybrid search?
BM25 is the lexical scorer whose ranked list is merged with a vector search list, so exact-word matches and meaning matches both reach the result. BM25 is one half of a hybrid search, not hybrid search itself. The hybrid search page covers how the two lists are merged. BM25 can also rescore a short candidate list, but a cross-encoder reranker reads query and document together and usually does better on the same list at higher cost; the transformer overview covers the model family behind such rerankers.
Is BM25 still worth using, or is it outdated?
BM25 is still worth using as a baseline and a first stage. The BEIR authors call it "a robust baseline", and report that dense and sparse retrievers are cheaper to run but often underperform other approaches on their datasets, which points to weak generalization.
Does Google use BM25?
No public Google document says its web search ranks pages with BM25, so treat any claim that it does as unverified. BM25 is the scoring function Apache Lucene ships with defaults k1 = 1.2 and b = 0.75, and it is a common lexical baseline in open-source search.
When does BM25 beat dense retrieval?
BM25 tends to win on queries with rare names, codes or exact strings, where the word itself is the signal, and where no trained embedding model for the domain exists. Test on your own queries rather than assume.
What are BM25+ and BM25L?
One variant by Lv and Zhai (BM25L) shifts the length-normalized term frequency up by a constant so that very long documents are not over-penalized (Lv and Zhai, Special Interest Group on Information Retrieval (SIGIR) 2011). BM25+ adds a lower-bound constant δ to the term-frequency factor, so a document that matches a query word always scores above one that does not (Lv and Zhai, Conference on Information and Knowledge Management (CIKM) 2011).
- BM25 scores a document by summing, over query words, a rarity weight times a saturating, length-adjusted count.
- k1 sets how fast repeated words saturate; b sets how much document length is discounted.
- It needs only collection statistics, no training, and matches words rather than meaning.
- It stays a dependable baseline, and pairs with vector search and reranking in real systems.
Sources
- Introduction to Information Retrieval, ch. 11 Probabilistic information retrieval (section 11.4.3, Okapi BM25) — Cambridge University Press (Manning, Raghavan, Schütze), 1 Apr 2009
- Speech and Language Processing, 3rd ed. draft, ch. 11 Information Retrieval and Retrieval-Augmented Generation — Stanford (Jurafsky and Martin, draft), 7 Jan 2026
- BM25Similarity, Lucene 10.3.1 core API — Apache Lucene (release 10.3.1), 1 Oct 2025
- IndexSearcher, Lucene 10.3.1 core API (default similarity) — Apache Lucene (release 10.3.1), 1 Oct 2025
- BEIR: A Heterogenous Benchmark for Zero-shot Evaluation of Information Retrieval Models (arXiv 2104.08663) — arXiv (Thakur et al.), 17 Apr 2021
- Okapi BM25 — Wikipedia (naming and variants only), 26 Oct 2026
- The Probabilistic Relevance Framework: BM25 and Beyond — Foundations and Trends in Information Retrieval (Robertson and Zaragoza), 1 Dec 2009
- Simple BM25 extension to multiple weighted fields — ACM CIKM (Robertson, Zaragoza and Taylor), 13 Nov 2004
- When documents are very long, BM25 fails! — ACM (Lv and Zhai), 24 Jul 2011
- Lower-bounding term frequency normalization — ACM (Lv and Zhai), 24 Oct 2011