BM25
Definition
BM25 (Best Match 25) is the dominant lexical retrieval algorithm — the scoring function behind Elasticsearch’s default relevance, Solr, Lucene, and most production search engines. It is a probabilistic term-frequency weighting model that improves on TF-IDF by accounting for document length and term-frequency saturation.
Formula
BM25(d,q) = Σ_{t∈q} IDF(t) × (tf(t,d) × (k1+1)) / (tf(t,d) + k1×(1-b+b×|d|/avgdl))
Where:
tf(t,d)= term frequency of term t in document dIDF(t)= inverse document frequency = log((N-n+0.5)/(n+0.5)+1)|d|= document length in tokensavgdl= average document length in the corpusk1∈ [1.2, 2.0] = term frequency saturation parameterb∈ [0, 1] = document length normalization (typically 0.75)
Key Innovations Over TF-IDF
1. Term Frequency Saturation
In TF-IDF: score scales linearly with tf (10 occurrences = 10× weight of 1 occurrence).
In BM25: the k1 parameter saturates tf — going from 1 to 2 occurrences increases score a lot; going from 10 to 20 occurrences adds little.
Intuition: Mentioning “python” once signals relevance; mentioning it 50 times doesn’t mean 50× more relevant.
2. Document Length Normalization
The b parameter penalizes long documents. Without it, long documents would score higher simply because they contain more terms — even if they’re less dense with the topic.
b=0: no length normalization
b=1: full normalization
b=0.75: standard compromise
Parameters
BM25 parameters are tunable:
| Parameter | Default | Effect |
|---|---|---|
| k1=1.2 | Low | Term saturation happens quickly |
| k1=2.0 | High | More benefit from repeated terms |
| b=0.0 | — | Ignore document length |
| b=1.0 | — | Strong length normalization |
Elasticsearch defaults: k1=1.2, b=0.75. Tuning can improve NDCG by 2–5%.
”BM25” in a results table is not one number
When a paper reports beating BM25, it beat somebody’s BM25. From Improving Zero-Shot Ranking with Vespa Hybrid Search - part two — k1=0.9, b=0.4, with the scoring function applied independently to title and text and the two combined linearly, against the BM25 figures published alongside BEIR:
| Dataset | Published BM25 | Tuned BM25 |
|---|---|---|
| TREC-COVID | 0.656 | 0.690 |
| HotpotQA | 0.603 | 0.623 |
| ArguAna | 0.315 | 0.393 |
| BEIR average | 0.440 | 0.453 |
The ArguAna gap is a quarter of the baseline’s value. Two practical consequences: tune the lexical baseline before adding a neural component, since it is the cheapest gain on the table; and discount reported neural improvements measured against a default-configured BM25. The per-field scoring matters as much as the parameters — see Linear Score Combination and, for the multi-field generalization, BM25F below.
BM25F
BM25F (BM25 with Fields) extends BM25 to handle multi-field documents (title, body, URL):
- Different field weights (title match more important than body match)
- Field-specific length normalization
Essential for e-commerce (product name vs. description) and enterprise search (email subject vs. body).
Bayesian BM25 (BB25)
A probabilistic recasting by Doug Turnbull that converts a raw BM25 score into an estimate of
P(relevant | score), so it can be combined with other probability-calibrated signals (embedding
similarity, CTR, recency) without a hand-tuned mixing weight.
- The BM25 score enters as a likelihood, passed through a sigmoid
- A prior is derived from term frequency and field-length normalization
- Bayes’ theorem gives the posterior probability of relevance
- The sigmoid’s steepness and midpoint are fit from relevance labels; the midpoint is typically the corpus median BM25 score, making it collection-specific
See Bayesian BM25 for the full treatment, and Score Normalization for the alternative of rescaling scores rather than calibrating them.
BM25 vs. Semantic Search
| Dimension | BM25 | Semantic (Bi-Encoder) |
|---|---|---|
| Vocabulary mismatch | Fails | Handles |
| Exact term match | Excellent | Can miss |
| Speed | Very fast | Slower (ANN) |
| Interpretability | High | Low |
| OOV terms | Fails | Handles |
The standard combination: Hybrid Search (BM25 + bi-encoder).
Unboundedness, and Why It Matters in Fusion
BM25 has no upper bound. The same three properties that make it a good lexical ranker — term
frequency, IDF, and length normalization — mean a short document repeating a distinctive term can
emit an arbitrarily large score. Saturation via k1 damps the growth from repetition; it does not
cap the score.
That is harmless when BM25 ranks alone, because only the ordering matters. It becomes a problem the moment BM25 is added to a bounded score such as cosine similarity, which sits in a narrow band typically well under 1: the unbounded branch can dominate the sum and the bounded one stops affecting the ranking. Normalize before combining — Linear Score Combination, Relative Score Fusion — or fuse on ranks instead (Reciprocal Rank Fusion).
A second, subtler bias shows up in the same setting. BM25’s ideal profile — short and repetitive — describes explanatory writing, while reference material tends to state a fact once inside a long document. When the reference is the thing that answers the query, BM25 systematically prefers the wrong document. Worked example: Hybrid Fusion Failure - BM25 Displacing Reference Documents.
Related Concepts
-
Embeddings — BM25 is the classical (non-neural) sparse retrieval baseline
-
Sparse Embeddings — BM25 as the foundational sparse representation
-
Hybrid Search — BM25 as the sparse component
-
Sparse Vector Retrieval — BM25 is the classical sparse retrieval baseline
-
SPLADE — learned sparse model that outperforms BM25
-
ELSER — +17% NDCG@10 over BM25
-
Retrieval Pipeline — BM25 often the fast first stage
-
NDCG — BM25 baseline NDCG@10 = 0.228 on MS MARCO
-
Zero-Shot Retrieval — why a parameter-free model keeps winning out-of-domain
-
Full-Text Search — BM25 is its standard relevance model
-
ParadeDB (
pg_search) and psql_bm25s — bring BM25 to PostgreSQL (whose nativets_rankis not BM25); see Search using PostgreSQL
People
-
Doug Turnbull — BM25 in production, BB25 extension, practical tuning guides
-
Can BM25 be a Probability — Doug Turnbull; BB25 framework, calibration via gradient descent for hybrid search
-
Improving Zero-Shot Ranking with Vespa Hybrid Search — Jo Kristian Bergum; BM25 as the baseline that survives domain shift, trailing neural methods by 7–18 points on MS MARCO and beating them across BEIR
-
Improving Zero-Shot Ranking with Vespa Hybrid Search - part two — the tuned configuration above, and BM25 as one leg of a hybrid that reaches 0.481