Kolkata ·  
AI engineering
retrieval
short · 3 min

BM25

Keyword search that weights rare words more, stops rewarding repeats, and discounts long documents.

updated 27 Sept 2026

Someone types "database connection pooling" into a search box over 1,000 technical documents. Two pages come up:

  • A is a long article about databases in general, three times the average length. It says "database" 50 times, "connection" 3 times, "pooling" once.
  • B is a guide to connection pooling, average length. "database" 5 times, "connection" 4 times, "pooling" 12 times.

Count matching words and A wins, 54 to 21. Wrong page. BM25 is the keyword formula that fixes this with three small changes. Let's make them one at a time.

Fix one: rare words count more

"database" is in 400 of our 1,000 documents, so it tells us very little. "pooling" is in only 10. So we weight each query word by how few documents contain it (the inverse document frequency, or IDF):

IDF(t)=ln⁡N−nt+0.5nt+0.5\text{IDF}(t) = \ln \frac{N - n_t + 0.5}{n_t + 0.5}

NN is the number of documents, ntn_t how many contain the word. That gives "database" 0.41, "connection" (in 100 documents) 2.19 and "pooling" 4.55. One "pooling" is worth about eleven "database".

Fix two: repeats stop helping

The first time a page says "pooling" is good evidence. The fiftieth time adds almost nothing. So BM25 runs the count tftf through a curve that rises fast and then flattens:

tfk1+tf\frac{tf}{k_1 + tf}

With the usual k1=1.2k_1 = 1.2, one mention scores 0.45, five score 0.81, and fifty score 0.98. This flattening is called saturation, and it's what separates BM25 from older tf-idf, where the count keeps adding up.

Fix three: long pages get a discount

A long page matches more words by chance. So BM25 compares each page's length with the average and makes the curve flatter for long pages. A second knob, bb, says how much: 1 means length counts fully, 0 turns it off, 0.75 is common. Why not all the way? Some long pages are just wordy, but others cover more ground, and their extra matches are real.

Back to the two pages

Put the three fixes together and add them up over the query words:

score(D,Q)=∑t∈QIDF(t)⋅tft,Dk1((1−b)+b⋅∣D∣avgdl)+tft,D\text{score}(D, Q) = \sum_{t \in Q} \text{IDF}(t) \cdot \frac{tf_{t,D}}{k_1 \left( (1 - b) + b \cdot \frac{|D|}{\text{avgdl}} \right) + tf_{t,D}}
WordIDFA: countA: scoreB: countB: score
database0.41500.3850.33
connection2.1931.1041.69
pooling4.5511.14124.13
Total542.62216.15

Now the guide wins by more than two to one.

A few things catch people out. BM25 only sees words, so "car" won't find a page that only says "automobile"; matching meaning is what semantic search is for. The upside is that an error code or a product ID matches only itself. A word in more than half the documents gets a negative weight in this formula, and engines handle that differently. And there's no theory for the knobs: k1k_1 from 1.2 to 2 and bb from 0.5 to 0.8 work in many cases, but the best values depend on your data.

The one that bites on Postgres: its built-in ts_rank isn't BM25. It has no IDF, no saturation and no length discount, so page A would win again. Its @@ match also wants every query word, so a great page missing one is dropped before ranking even starts. For real BM25 there you need an extension like pg_textsearch or ParadeDB's pg_search. Then pair it with vectors, so paraphrases get caught too, and you have hybrid search.

Further reading