Article

BM25 Explained: The Algorithm That Still Powers Modern Search

· 9 min read

1. Introduction

You type something into a search bar. In milliseconds, results come back, ranked and relevant. Behind that instant response, there's a 30-year-old algorithm quietly doing most of the heavy lifting. It's called BM25, and it's still the backbone of how most search systems work today.

At first glance, searching feels simple: find documents that contain your search words. But the moment you try to rank those documents, to decide which one is most relevant, things get complicated fast.

Say you search for "climate change" and you get back two articles. Article A is a 500-word focused piece that mentions "climate change" 10 times. Article B is a 5,000-word research paper that also mentions it 10 times, but buried among thousands of other words. Which one is more relevant to your search? Intuitively, Article A, the focused one. But a naive word-counting system would rank them equally.

This is exactly the kind of nuance that BM25 was built to handle. But to understand why BM25 was created, we first need to meet the algorithm it replaced.

2. The Algorithm That Came Before: TF-IDF

Before BM25, the go-to approach was called TF-IDF, which stands for Term Frequency times Inverse Document Frequency. The formula looks like this:

TF-IDF(t, d)  =  TF(t, d)  ×  log( N df(t) )

Breaking that down:

  • TF(t, d) is simply how many times a term t appears in document d. More appearances, higher score.
  • IDF is the log of the total number of documents N divided by the number of documents that contain the term. This part is smart: if a word appears in almost every document (like "the"), the IDF shrinks toward zero, making it nearly irrelevant to the score. If a word is rare and specific (like "mitochondria"), the IDF is high, giving it more weight.

The product of the two gives you a relevance score. It was a genuine leap forward for search. But TF-IDF had two problems that nobody had properly solved yet.

Problem 1: No term saturation. TF-IDF keeps rewarding repetition forever. If a document mentions your search term 10 times, it scores higher than one mentioning it 5 times. If it mentions it 1,000 times, it scores even higher. There's no ceiling. If someone mentions "coffee" once in a review, they're probably writing about coffee. If they mention it 5 times, they're definitely writing about coffee. If they mention it 500 times? At some point, extra repetitions stop meaning anything. The signal saturates. TF-IDF never accounts for this, so it can be gamed just by stuffing a document with repeated keywords.

Comparison of TF-IDF and BM25
TF-IDF keeps climbing with every repetition. BM25's curve flattens out, extra mentions stop moving the score.

Problem 2: No document length normalization. TF-IDF blindly rewards longer documents. A 10,000-word document mentioning "coffee" 50 times isn't necessarily more about coffee than a 500-word blog post mentioning it 20 times. The long document simply has more words, so it naturally accumulates more keyword hits. TF-IDF treats both documents as if length doesn't matter at all.

Comparison of two articles with same mentions but different lengths
Same number of mentions, very different concentration. BM25 discounts the longer document for this.

These two flaws together meant TF-IDF could be inconsistent and easy to fool. Something better was needed.

3. Enter BM25: Best Match 25

In the 1990s, researchers Stephen Robertson and Karen Spärck Jones set out to fix both problems. The result was BM25, where "BM" stands for Best Match and "25" marks the 25th iteration of their experimental formula.

The core idea was to take TF-IDF's foundation and add two targeted corrections: cap the reward for repeated terms, and adjust scores based on document length. Simple additions on paper, but they made the algorithm dramatically more accurate in practice.

BM25 does this by asking three questions about every document:

  • Rarity: How rare is this word? A word that appears in almost every document tells you almost nothing. A word that appears in only a few documents is highly informative. BM25 assigns rare words more weight, the same idea as IDF in TF-IDF.
  • Frequency: How many times does it appear in this document? More occurrences generally mean higher relevance. But the benefit of extra occurrences diminishes. The jump from 1 mention to 5 matters a lot. The jump from 50 mentions to 55? Barely anything. BM25 captures this with a concept called saturation, directly solving Problem 1 from TF-IDF.
  • Length: Is this document unusually long? BM25 compares each document's length to the average document length in the collection. If a document is much longer than average, its term frequency scores are adjusted downward, directly solving Problem 2 from TF-IDF.

4. The Formula (Don't Panic)

Here's what BM25 looks like written out:

BM25(D, Q)  =  n Σ i = 1  IDF(qi) · f(qi,D) · (k1 + 1) f(qi,D) + k1( 1 − b + b · |D| avgdl )

The symbols that matter:

  • D = the document being scored
  • Q = your search query
  • q_i = each individual word in your query
  • f(q_i, D) = how many times word q_i appears in document D
  • |D| = the length of the document (word count)
  • avgdl = the average document length across the entire collection
  • k1 = controls how much repetition matters (typically 1.2 to 2.0)
  • b = controls how much length matters (typically 0.75)

What it's actually doing: for each word in your query, it calculates a relevance score for that word in the document, then adds all those scores together. The final total is the document's BM25 score.

The two tuning parameters are what make BM25 adaptable. k1 controls saturation. A higher k1 means the algorithm keeps rewarding word repetition for longer before leveling off. A lower k1 saturates quickly. b controls length normalization. A b of 0 means length is completely ignored; a b of 1 means full normalization. In practice, the defaults (k1 = 1.5, b = 0.75) work well for most use cases.

5. A Worked Example

Let's make this concrete with actual numbers. Here are three documents and a query:

  • D1: "The cat sat on the mat" (6 words)
  • D2: "The dog played in the garden" (6 words)
  • D3: "The cat and dog played together" (6 words)

Query: "cat dog". Parameters: k1 = 1.5, b = 0.75, avgdl = 6.

Step 1: IDF, how rare are our query words?

BM25 uses this IDF formula:

IDF(q)  =  log( N − df + 0.5 df + 0.5  + 1)

where N = total documents (3) and df = number of documents containing the term.

"cat" appears in 2 out of 3 documents (D1 and D3):

IDF(cat)  =  log( 3 − 2 + 0.5 2 + 0.5  + 1)  =  log(1.6)  =  0.47

"dog" also appears in 2 out of 3 documents (D2 and D3), so IDF(dog) = 0.47 as well. Both words are equally rare in this small collection, so neither gets an advantage here.

Step 2: Term frequency in each document. D1 has "cat" once, "dog" zero times. D2 has "cat" zero times, "dog" once. D3 has "cat" once, "dog" once. It's the only document with both query terms.

Step 3: Length normalization. All three documents are 6 words long and the average is also 6 words, so the length normalization factor is exactly 1 for all three. No penalties, no boosts.

Step 4: Calculate the BM25 score for each document.

The TF part of the formula for a term appearing once in a 6-word document:

1 × (1.5 + 1) 1 + 1.5( 1 − 0.75 + 0.75 × 6 6 )  =  2.5 2.5  =  1.0

For a term appearing zero times, the TF part = 0 (numerator is 0).

So the score per matching term = IDF x TF = 0.47 x 1.0 = 0.47.

Putting it all together:

Document Score for "cat" Score for "dog" Total BM25 Score
D1 0.47 0.00 0.47
D2 0.00 0.47 0.47
D3 ★ 0.47 0.47 0.94

Final Ranking: D3 (0.94) > D1 (0.47) = D2 (0.47).

D3 scores almost exactly double D1 and D2, because it's the only document that matches both query words. That lines up perfectly with intuition: if you searched for "cat dog", a sentence about both is more relevant than a sentence about just one.

6. How BM25 Actually Works Inside a Search System

When you type a query into a system like Elasticsearch or Apache Solr, here's what happens under the hood:

  • Step 1: Preprocessing the query. Each word in your query is reduced to its stem, its base form. "Searching" becomes "search", "cheeses" becomes "chees". This way, different forms of the same word are treated as equivalent. The stem is then mapped to an index in the system's vocabulary.
  • Step 2: Looking up the inverted index. The system doesn't scan every document. Instead, it uses a data structure called an inverted index, essentially a giant lookup table that maps each word to the list of documents containing it. It pulls up only the documents relevant to your query words.
  • Step 3: Calculating per-document scores. For each document retrieved, BM25 calculates a score using the IDF of your query word (computed across the entire document collection) combined with the term frequency and length normalization within that specific document.
  • Step 4: Summing across all query words. The final relevance score for a document is the sum of its BM25 scores for each query term. A document that scores well for multiple query words ranks higher than one that scores well for just one.
Flowchart showing the steps from a typed query to a ranked list of documents
The path from a typed query to a ranked list, no model training required.

That's it. Fast, transparent, no machine learning required.

7. Where BM25 Is Used Today

BM25 isn't just a textbook curiosity. It's actively running in systems millions of people use every day.

  • Elasticsearch and Apache Solr both use BM25 as their default ranking algorithm. Every internal company search tool, every e-commerce product search, every document management system built on these platforms is running BM25.
  • Digital libraries and academic databases use it to surface relevant papers from millions of articles. Type a research topic into PubMed or a university library portal, and BM25 is likely doing the initial ranking.
  • Enterprise search: when you search Confluence, SharePoint, or your company's internal knowledge base, BM25 is often the engine behind the results.
  • RAG systems (Retrieval-Augmented Generation), the technology behind AI assistants like ChatGPT's document search and similar tools, often use BM25 as a first-pass retrieval step. It narrows millions of documents down to the most promising candidates before a language model takes over.

8. BM25 vs. Modern Semantic Search

At this point you might be wondering: with all the advances in AI, why are we still talking about a 30-year-old algorithm? Fair question. Modern AI search systems use something called semantic search. They convert text into numerical vectors and find documents that are conceptually similar to your query, even if they don't share exact words. Search "automobile" and it might return documents about "cars" and "vehicles."

BM25 doesn't do that. It's purely keyword-based and doesn't understand meaning, only word frequency and rarity. But BM25 has advantages that semantic search genuinely can't match.

  • It's extremely fast, even over millions of documents. Semantic search requires computing and comparing dense vectors, which is expensive at scale.
  • It's transparent. You can always explain why BM25 ranked a document highly. It contains these specific rare words, this many times. Semantic search is a black box.
  • It requires no training data. Semantic search requires large pre-trained models. BM25 works out of the box on any document collection.
  • It's precise with exact terms. If you're searching for a product code, a person's name, or a specific technical term, BM25 nails it. Semantic search can sometimes drift toward related-but-wrong results.

In practice, the best modern search systems use both: BM25 for keyword precision, semantic search for conceptual understanding, combined into a hybrid score. This is now the industry standard approach.

BM25 is simple enough to be fast, transparent enough to be debuggable, and smart enough to handle the three core problems of text relevance, rarity, frequency, and length, with elegant math and no training required.

9. Conclusion

Here's the honest answer for why a 30-year-old algorithm is still running the world's search: because it works. BM25 is simple enough to be fast, transparent enough to be debuggable, and smart enough to handle the three core problems of text relevance, rarity, frequency, and length, with elegant math and no training required.

Every few years, researchers propose something that might replace it entirely. And every time, BM25 turns out to be harder to beat than expected, especially when you need speed, reliability, and results you can actually explain.

The next time you search for something and the right result appears at the top of the list, there's a decent chance a formula written in the 1990s is the reason why.