BM25, also called Okapi BM25, is a ranking function from information retrieval. Given a search query and a collection of documents, it gives each document a score based on the query words it contains, and the highest scores rank first. BM stands for "best matching." The function grew out of the probabilistic relevance framework that Stephen Robertson, Karen Spärck Jones and colleagues developed from the 1970s on, and it was built and tested in the Okapi search system at City University, London.
BM25 is still a standard way to score text. It's the default scoring method in Elasticsearch, and a common baseline when researchers test newer methods.
The three ideas inside BM25
BM25 adds up a score for each query word found in a document. Each word's share depends on three things.
How rare the word is. A match on a word that appears in few documents counts for more than a match on a common one. This is inverse document frequency, or IDF. A match on "axolotl" tells you more about a document than a match on "the."
How often the word appears, with diminishing returns. The second mention adds less than the first, the tenth adds very little, and no number of repeats pushes the score past a ceiling. This is called saturation, and it's what makes BM25 different from plain term frequency counting.
How long the document is. A long document mentions most words more often simply because it has more words. BM25 compares each document's length with the average in the collection and scales term counts down for longer documents and up for shorter ones.
The formula
For each query term q in document D:
score(q, D) = IDF(q) × tf × (k1 + 1) / (tf + k1 × (1 − b + b × |D| / avgdl))
tfis how many times the term appears in the document|D|is the document's length andavgdlis the average length in the collectionk1sets how quickly repetition saturates; a lower value means faster saturationbsets how much length matters, from 0 (not at all) to 1 (fully)
The document's score is the sum over all query terms. The (k1 + 1) in the top half is a common variant; it changes the scale, not the order of results Robertson and Zaragoza.
Choosing k1 and b
Elasticsearch uses k1 = 1.2 and b = 0.75 by default Elastic. Robertson and Zaragoza report that values of b between 0.5 and 0.8 and k1 between 1.2 and 2 work reasonably well in many settings, but the best values depend on your documents and queries. If you run your own search, tune them against real queries with known good answers.
A worked example: repetition hits a ceiling
Here's the term-frequency part of the formula with k1 = 1.2, for a document of average length:
| Times the word appears | Term-frequency part |
|---|---|
| 1 | 1.00 |
| 2 | 1.38 |
| 3 | 1.57 |
| 5 | 1.77 |
| 10 | 1.96 |
| 20 | 2.08 |
| any number | never above 2.20 |
Twenty mentions score barely twice as much as one. Most of the gain comes from the first few.
Length works the other way. With b = 0.75, one mention in a document twice the average length is worth about 0.71; in a document half the average length, about 1.26.
Variants: BM25F and BM25+
BM25F scores documents with several fields, such as title, body and anchor text, and lets each field count differently. Robertson and Zaragoza describe it as one of the most successful algorithms for web and corporate search.
BM25+ adds a small lower bound to the term-frequency part. Yuanhua Lv and ChengXiang Zhai proposed it in 2011 after showing that, without such a bound, very long documents are penalized too much.
BM25 compared with TF-IDF
| Classic TF-IDF | BM25 | |
|---|---|---|
| Repeated words | Score keeps growing with each mention | Score levels off at a ceiling |
| Document length | Needs separate normalization | Built in, set by b |
| Tuning | Usually none | k1 and b |
Both use IDF, and both treat a document as a bag of words.
What BM25 means for SEO
Google doesn't publish its ranking formulas. It says matching keywords are a basic sign of relevance, but it weighs many other signals and uses language models and a synonym system to understand what a search means Google. So don't try to optimize a page "for BM25."
The idea behind BM25 still teaches something useful. Using the words people search for matters; repeating them adds little. That's one reason keyword stuffing doesn't work, and search engines treat it as spam anyway.
Where you meet BM25 directly is in search you run yourself: your site's search box, a documentation search, or the retrieval step of a retrieval-augmented generation system. There, BM25 is often combined with vector search: BM25 is strong on exact names, product codes and rare terms, while vectors catch matches in meaning.
Limits of BM25
- It ignores word order and closeness. "Dog bites man" and "man bites dog" score the same.
- It only knows the words you give it. Without synonyms or stemming added by your search engine, "car" won't match "automobile."
- It knows nothing about quality. BM25 scores how well text matches a query, not whether the text is accurate, current or well written.
For the field BM25 comes from, see information retrieval.