Back to all posts
2 Sept 20262 min read

Understanding BM25: The Classic Ranking Formula That Still Powers RAG

When you type a query into Google, an e-commerce site, or even an AI-powered RAG system — the magic that decides which document appears first is built on something called BM25( Best Matching Version 25).

Even in this era of large language models, BM25 remains one of the most reliable, interpretable, and mathematically elegant algorithms for document ranking.

Let’s decode it. 👇

What is BM25?

BM25 (Best Matching 25) is a ranking function used by search engines to estimate how relevant a document is to a user’s query.

It’s an improved version of TF-IDF — the classic term-weighting method — but with smarter normalization for document length and term saturation.

In simple terms, BM25 answers:

“How relevant is this document to the given search query?”

The Intuition Behind BM25

Imagine you’re searching for “machine learning fundamentals.”

  • A document that mentions machine learning many times should rank higher.

  • But if a word repeats too much, the importance should eventually saturate (repetition ≠ more meaning).

  • And a shorter document with those terms should rank higher than a huge, irrelevant one.

BM25 captures these intuitions beautifully.

The BM 25 Formula

Article contentArticle content

What Makes BM25 So Good?

  1. Term Frequency Saturation

  2. Document Length Normalization

  3. Inverse Document Frequency (IDF)

  4. Tunable Parameters (k₁, b)

Example Intuition

Let’s say two documents contain:

  • Doc A: “machine learning machine learning deep learning”

  • Doc B: “introduction to machine learning”

If you search “machine learning”:

  • BM25 will assign a relevance score to both documents.

  • Doc A, with repeated “machine learning”, initially gains from frequency — but its advantage flattens due to BM25’s saturation.

  • Doc B, being shorter and focused, often ends up with a higher overall score because of better length normalization.

That’s BM25’s brilliance — balanced scoring.

Where BM25 Is Used

Even today, BM25 is:

  • The default ranking model in Elasticsearch and Lucene (which power major search engines).

  • A baseline for retrieval in RAG systems (before embeddings or hybrid search).

  • Used in academic papers as a standard baseline in information retrieval benchmarks.

Why It Still Matters in the Age of AI

While embeddings and vector databases (like FAISS or Milvus) dominate new systems, BM25 remains:

  • Fast

  • Explainable

  • Deterministic

Modern AI systems often combine BM25 with embeddings in hybrid retrieval, using the best of both worlds: lexical precision + semantic understanding.

So, every time an AI system finds the “most relevant” passage for your question, there’s a good chance BM25 (or something inspired by it) is working quietly in the background.

Newsletter

Enjoyed this? Get the next one in your inbox.

New posts land straight in your inbox. No spam, unsubscribe anytime.