Ranking — TF-IDF and BM25, the Math of Relevance

Finding the documents that contain your words is the easy part; putting the best one first is where search lives or dies. For decades, the answer to "how relevant is this document to this query?" has come from two intuitions made precise: a word matters more in a document that uses it often, and matters less if it's common across all documents. TF-IDF and BM25 turn those intuitions into the scoring functions that rank most of the world's text search — and understanding them demystifies why search returns what it does.

Posts 2–3 gave us matching documents and clean terms. This post is the ranking problem (post 1): scoring and ordering the matches by relevance. BM25 in particular is the workhorse — the default in Elasticsearch and Lucene and the “keyword score” in hybrid RAG retrievers — so understanding it is understanding what classical search relevance actually is.

Two intuitions about relevance

Imagine scoring how relevant a document is to the query “reinforcement learning.” Two common-sense intuitions drive almost all classical ranking:

TF-IDF combines them: a document’s score for a query is the sum, over query terms, of TF × IDF — reward documents that use the query’s terms often (TF), especially the rare, discriminating ones (IDF). This single idea — frequent-in-this-document but rare-across-documents = important — is one of the most influential in information retrieval, and it captures a surprising amount of what “relevance” means. It also explains search behavior you’ve seen: common words barely affect ranking (low IDF), while an unusual query term dominates it (high IDF).

BM25: TF-IDF, done right

TF-IDF is the intuition; BM25 (Okapi BM25) is the refined, production-grade version that fixes TF-IDF’s rough edges and has been the dominant ranking function for decades. It keeps the TF and IDF intuitions but adds two crucial corrections:

BM25 exposes two tunable knobs (commonly called k1, controlling how fast TF saturates, and b, controlling how strongly length is normalized), letting you adapt it to a collection — but its defaults work remarkably well out of the box, which is a big reason it’s the universal default. The practical reality: BM25 is the baseline that’s shockingly hard to beat. Decades of more sophisticated methods have struggled to consistently outperform well-tuned BM25 on general text search, which is why it remains the default scoring function in essentially every full-text search engine and a core component of modern hybrid retrieval.

What ranking by term statistics can and can’t do

BM25 and TF-IDF are lexical — they score based on the words that appear, their frequencies, and their rarity. This is their strength and their ceiling:

This ceiling is precisely what semantic / vector search (post 7) was built to overcome — matching by meaning via embeddings rather than by terms — and it’s why modern systems increasingly run hybrid retrieval: BM25 for exact-term precision and vector search for semantic recall, combining the strengths. But understand this clearly: vector search didn’t make BM25 obsolete. BM25 remains superb at exactly what embeddings are weakest at — exact keyword, rare-term, and precise-phrase matching — which is why the two are combined rather than one replacing the other. Knowing what lexical ranking does well (exact terms, interpretably, fast, untrained) and badly (meaning, synonyms, concepts) is knowing exactly when each half of a hybrid system earns its place.

The throughline: ranking scores how relevant each matching document is, built on two intuitions — term frequency (more mentions = more relevant) and inverse document frequency (rarer words are more informative) — combined as TF-IDF (sum of TF×IDF over query terms), one of IR’s most influential ideas. BM25 is the production-grade refinement and decades-long default (Elasticsearch/Lucene, the keyword score in hybrid RAG), adding TF saturation (repetition with diminishing returns, defeating keyword stuffing) and document-length normalization (relevance = density, not raw count), with tunable knobs but great defaults — a baseline shockingly hard to beat. Its ceiling is that it’s lexical (matches words, not meaning), so it nails exact terms/rare words/phrases fast and interpretably but hits the vocabulary gap — which semantic/vector search (post 7) complements, making hybrid retrieval (BM25 precision + vector recall) the modern norm, not replacement.

Key takeaways

Further reading

Sources & References