Skip to content
mlmentorship

TF-IDF and BM25

TF-IDF and BM25 are lexical retrieval baselines for search and RAG. BM25 adds term-frequency saturation and document-length normalization.

Published · 6 min read ·Role-specific ·Intermediate

Visual quick review

Visual first · depth when needed

Separate BM25's diminishing return for repeated matches from its penalty for finding the same number of matches in a longer document.

Preparing the visual…

Summary

TF-IDF scores a term in a document as term frequency × inverse document frequency: frequent-in-this-doc but rare-across-the-corpus terms score highest. BM25 is the saturated, length-normalized refinement of the same idea and is the standard lexical retrieval baseline in search and hybrid RAG.

Despite dense embeddings, lexical retrieval has not gone away: hybrid retrieval (BM25 + dense) almost always beats either alone, especially for rare terms, exact identifiers, codes, and out-of-domain queries where embeddings generalize poorly. Any RAG or search interview expects you to know BM25 as the baseline, why it works, and where it fails. It’s also the cheapest big win: a tuned BM25 stage in front of a reranker is hard to beat per dollar.

Term frequency and its problem

Raw count: a term appearing 10× isn’t 10× as relevant as appearing once; relevance saturates. So TF is damped, e.g. , before being used. This saturation idea is exactly what BM25 formalizes.

Inverse document frequency

Rare terms are more discriminative. IDF down-weights common terms (“the”, “data”) and up-weights rare ones:

where is the number of documents and is how many contain term . TF-IDF is then , summed over query terms.

BM25: the production formula

BM25 (Best Match 25, from the Okapi system) adds two things TF-IDF lacks: bounded TF saturation and document-length normalization:

  • : term frequency in the document.
  • (≈1.2–2.0): controls TF saturation. As the term’s contribution asymptotes to , so spamming a keyword has diminishing returns.
  • (≈0.75): controls length normalization via . Long documents don’t win just by containing more words.
  • BM25 typically uses a smoothed IDF, .

BM25 is the default scorer in Lucene / Elasticsearch / OpenSearch.

Learning objective: separate BM25’s diminishing return for repeated matches from its penalty for finding the same matches in a longer document.

One query term, two controls

BM25 rewards more matches, but less each time and less in longer documents.

BM25 term-frequency saturation and document-length normalization Two horizontal bar groups show the BM25 term-frequency factor without IDF, using k one equals 1.2 and b equals 0.75. In an average-length document, one occurrence scores 1.00, three score 1.57, and ten score 1.96, approaching the 2.20 ceiling. Holding frequency at three, a half-average-length document scores 1.76, an average-length document scores 1.57, and a double-average-length document scores 1.29. Thus repeated occurrences have diminishing returns, and the same count contributes less in a longer document. TERM FACTOR ONLY · IDF HELD CONSTANT · k₁ = 1.2 · b = 0.75 1 · SATURATION Average-length document; increase term frequency f. f = 1 1.00 f = 3 1.57 f = 10 1.96 ceiling 2.20 2 · LENGTH NORMALIZATION Three matches in every document; increase document length. 0.5× avg 1.76 1× avg 1.57 2× avg 1.29 Bars use the formula above; multiply each value by that term's IDF.
Read it this way: in the first group, ten matches score less than twice one match and remain below the 2.20 ceiling. In the second, the same three matches lose weight as unrelated document length grows. The values are original calculations from the displayed formula, checked against the Stanford IR textbook, the BM25 review by Robertson and Zaragoza, and Lucene's BM25 implementation.

TF-IDF vs BM25

TF saturationLength normalizationTunableStatus
TF-IDFlogarithmic, unbounded-ishimplicit (cosine on normalized vectors)noteaching baseline
BM25explicit, bounded by explicit via production lexical baseline

When people say “lexical baseline,” they mean BM25, not raw TF-IDF.

Lexical vs dense (and why hybrid wins)

Lexical (BM25)Dense (embeddings)
Matchesexact tokenssemantic similarity
Rare terms / IDs / codesstrongweak
Synonyms / paraphraseweakstrong
Out-of-domainrobustdegrades
Indexinverted index, cheapANN over vectors

Hybrid retrieval fuses the two, commonly with Reciprocal Rank Fusion (RRF) over the two ranked lists or a weighted score combination, then a cross-encoder reranks the top candidates. This is the standard production retrieval stack.

What an interviewer expects you to say

  1. Define TF-IDF = TF × IDF, and explain why TF is damped (relevance saturates) and why IDF weights rare terms (discriminativeness).
  2. Explain that BM25 adds bounded TF saturation () and length normalization (), and that it (not TF-IDF) is the real lexical baseline.
  3. Compare lexical vs dense and argue for hybrid retrieval + reranking, calling out that lexical wins on rare terms, IDs, and out-of-domain queries.
  4. Bonus: mention RRF for fusion and that BM25 is the Lucene/Elasticsearch default.

Common confusions

  • “Embeddings made BM25 obsolete.” No. BM25 still beats dense retrieval on exact-match-heavy queries and out-of-domain corpora, which is why hybrid is standard.
  • “TF-IDF and BM25 are the same.” BM25’s saturation and length normalization matter a lot in practice; raw TF-IDF over-rewards keyword stuffing and long documents.
  • “IDF measures importance.” It measures rarity / discriminativeness in the corpus, which correlates with but isn’t the same as semantic importance.
  • “Higher term frequency is always better.” BM25 explicitly caps the benefit; that’s the point of .
  • “BM25 understands meaning.” It’s purely lexical: no synonyms, no semantics. That gap is exactly what dense retrieval fills.

Related: RAG overview, Designing a RAG system that actually works, Approximate nearest neighbors, Word embeddings, Two-tower retrieval.