BM25 Probabilistic Ranking (Okapi)

Bibliometrics
Introduced by
Stephen Robertson; Karen Spärck Jones; Hugo Zaragoza (Okapi team, City University London)
Year
2009
Type
Probabilistic term-weighting and document-scoring pipeline for ranked retrieval
Family
Information-retrieval evaluation / ranking functions

What does it do?

BM25 (Okapi 'Best Matching 25') is a classical ranking function for information retrieval and a common term-weighting scheme in lexical search engines and bibliographic databases.

It was developed by Stephen Robertson, Karen Spärck Jones and colleagues at City University London, and its probabilistic basis is set out in Robertson and Zaragoza's 2009 monograph. It scores a document against a query as a sum over query terms of an inverse-document-frequency weight multiplied by a saturating, length-normalized transform of within-document term frequency, governed by two free parameters, k1 and b.

The intuition behind the method

Imagine ranking papers by how well they match a search. A first instinct is to count query words: a paper mentioning 'bibliometrics' ten times seems more relevant than one mentioning it twice. That is too crude in two ways. First, relevance saturates: going from one mention to two is strong evidence, but going from twenty to twenty-one adds little, so the contribution of term frequency should level off. Second, a long paper has more chances to contain any word, so raw counts unfairly favour long documents, and length has to be discounted. BM25 builds both intuitions into one formula. It weights each query term by how rare it is across the collection, since rare words are more discriminating, then adds a term-frequency contribution that rises quickly and flattens, scaled down for documents longer than average. Documents that contain rare query terms a reasonable number of times score highest, and repeated keywords or sheer length add progressively less.

History

BM25 grew out of the Okapi project at City University London, which built on probabilistic relevance models developed by Robertson and Spärck Jones from the 1970s onward. Its effectiveness was shown in the early-1990s TREC evaluations, where the Okapi system of Robertson, Walker and colleagues was a leading entry. BM25 is one of a numbered series of Okapi weighting schemes tested at TREC, and the name records its position in that series. Robertson and Zaragoza's 2009 monograph, 'The Probabilistic Relevance Framework: BM25 and Beyond', gives a systematic account of the framework, the resulting ranking functions, and the BM25F extension for structured documents. BM25 remains a standard lexical baseline in information retrieval and a common component of hybrid search systems.

When to use it

Use BM25 when you need a fast, interpretable lexical ranking function for full-text or bibliographic search, such as ranking abstracts or papers against keyword queries. It suits the first-stage retrieval layer of a multi-stage pipeline that later applies neural re-ranking, and it serves as a baseline against which a new ranker should be compared. It fits best when relevance depends largely on shared vocabulary between query and document and when term and length statistics are available from an inverted index. Do not rely on BM25 alone when relevance is semantic and the query and relevant documents use different words for the same concept; dense retrieval can complement it there. Because BM25 treats query terms as contributing independently, it does not capture term proximity or phrase structure. For documents with distinct fields, such as titles, abstracts and body text, the BM25F extension described in robertson2009 addresses fielded structure.

Limitations and considerations

  • Purely lexical: it matches shared terms and does not recognize synonyms or paraphrases unless query expansion or a separate embedding model is added.
  • Treats query terms as contributing independently, so term proximity, word order and phrase structure are ignored unless the model is extended.
  • The length-normalization term assumes document length is a reasonable proxy for exposure to terms; when a document combines fields of very different length, such as a short title and a long body, a single length statistic mixes them, which is the motivation for BM25F.
  • Effectiveness depends on k1 and b for the collection at hand; the commonly cited defaults are starting points and may be suboptimal for atypical corpora.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Strong, widely used lexical baseline: the standard reference against which many ranking methods are compared in information retrieval.
  • Cheap to compute from inverted-index statistics (term frequency, document frequency, document length, average length), which makes it practical as a first-stage ranker over large collections.
  • Each component has a clear role: IDF rewards rare query terms, saturation limits the effect of repeated terms, and length normalization removes the advantage of sheer document length.
  • Two interpretable parameters, k1 and b, can be adjusted to a collection without training a neural model.

Applications

  • First-stage ranked retrieval in search engines and bibliographic and digital-library databases.
  • Lexical baseline in information-retrieval evaluation and in benchmarking of new ranking methods.
  • Candidate generation for neural or learning-to-rank re-ranking in multi-stage retrieval pipelines.
  • Hybrid retrieval, where BM25 scores are combined with dense-embedding similarity to cover both exact-match and semantic relevance.
  • Field-weighted search over documents with titles, abstracts and body text, using the BM25F extension.

How it works

How it works

1

The pipeline begins by weighting each query term by how informative it is, using an inverse document frequency (IDF) factor. A term that appears in few documents is highly discriminating and receives a large weight; a term that appears in nearly every document carries little information and is down-weighted. The original probabilistic weight, from the relevance-weighting tradition behind the probabilistic relevance framework (robertson2009), is ln((N - n + 0.5)/(n + 0.5)), which can become negative for terms occurring in more than half of the documents. The form shown here adds 1 inside the logarithm, a common non-negative variant used in implementations such as Lucene; it is an implementation variant rather than the original form. The 0.5 added to the counts keeps the weight defined when a term occurs in no document or in every document. The step encodes the principle that matching a rare query term is stronger evidence of relevance than matching a common one.

Formula
\mathrm{IDF}(q_i) = \ln\!\left(\frac{N - n(q_i) + 0.5}{n(q_i) + 0.5} + 1\right)
2

Next, the raw within-document frequency of each query term is passed through a saturating transform governed by the parameter k1. As term frequency grows, the transform increases with diminishing returns and approaches k1 + 1 as the frequency becomes large. The first occurrence of a term therefore carries most of the evidence, and each further occurrence adds less. A small k1 makes the contribution level off after the first few occurrences, while a large k1 keeps it closer to linear in the count.

Formula
S(q_i, D) = \frac{f(q_i, D)\,(k_1 + 1)}{f(q_i, D) + k_1}
3

The third step corrects for document length. A longer document mentions a given term more often simply because it has more words, which would otherwise inflate its score. BM25 introduces a length-normalization factor B that interpolates, via the parameter b, between no normalization (b = 0) and full normalization by the ratio of document length to average length (b = 1). This factor scales the saturation denominator, so a document longer than the collection average is penalized and a shorter one is boosted. The value b = 0.75 is a commonly used starting point from Okapi practice and later implementations, and should be checked against the collection at hand.

Formula
B = 1 - b + b\,\frac{|D|}{\mathrm{avgdl}},\qquad 0 \le b \le 1
4

The full BM25 score combines the three components. For each query term, the IDF weight multiplies the length-normalized, saturating term-frequency contribution, and these per-term contributions are summed across the query. The closed-form score requires only term-frequency, document-frequency, document-length and average-length statistics, which an inverted index stores compactly. This keeps scoring at retrieval time cheap, which is one reason BM25 is widely used as a first-stage ranker before any more expensive re-ranking.

Formula
\mathrm{score}(D, Q) = \sum_{i=1}^{n} \mathrm{IDF}(q_i)\,\frac{f(q_i, D)\,(k_1 + 1)}{f(q_i, D) + k_1\bigl(1 - b + b\,\frac{|D|}{\mathrm{avgdl}}\bigr)}
5

Finally, every candidate document is scored and sorted in descending order to produce the ranked list returned to the user or passed to a downstream re-ranker. The behaviour depends on the two free parameters. Commonly used starting values are k1 around 1.2 to 2.0 and b around 0.75, drawn from Okapi practice and later implementations. For a specific collection, k1 and b are usually tuned on a set of queries with relevance judgments, because the best values depend on how verbose and repetitive the documents are.

Formula
\hat{D} = \operatorname*{arg\,sort}_{D \in C}\, \mathrm{score}(D, Q);\quad k_1 \in [1.2, 2.0],\ b \approx 0.75

Common pitfalls

  • Treating BM25 as a small variant of TF-IDF: its term-frequency saturation and explicit length normalization change rankings substantially, and these two components are where its behaviour differs.
  • Leaving k1 and b at library defaults on a corpus whose document lengths or repetition patterns differ sharply from typical text, then attributing poor results to the method rather than to the parameters.
  • Comparing BM25 scores across different queries or collections as if they were calibrated probabilities. The IDF component depends on the query terms and the collection, so scores are meaningful mainly for ranking documents within one query.
  • Expecting BM25 to retrieve documents that share meaning but not vocabulary with the query, which requires an additional semantic component.
  • Computing average document length and document frequencies over a different collection than the one being ranked, for example from a single shard or another index. Such statistics shift scores and can change ranks, so compute them over the same collection used for ranking.

Frequently asked questions

How is BM25 different from TF-IDF?

Both weight terms by inverse document frequency, but classical TF-IDF uses a term-frequency component without an explicit saturation parameter, and typically normalizes vectors by length. BM25 adds a saturation function controlled by k1, so repeated occurrences of a term contribute diminishing evidence, and a length-normalization term controlled by b, which discounts long documents relative to the average. These two components are the main differences in how BM25 ranks documents.

What do the parameters k1 and b control, and how should I set them?

k1 controls term-frequency saturation: small values make the score depend mostly on whether a term appears, while large values keep the contribution closer to linear in the count. b controls document-length normalization: b = 0 disables it, and b = 1 normalizes fully by length relative to the average. Commonly used starting values are k1 between 1.2 and 2.0 and b near 0.75. For a given collection, tune them on held-out queries with relevance judgments.

Is BM25 still relevant given neural retrieval models?

Yes. BM25 is fast, requires no training, and is still a standard first-stage and baseline ranker. Many systems use it to retrieve candidates and then re-rank with a neural model, or combine BM25 scores with dense-embedding similarity in hybrid retrieval. Reporting BM25 as a baseline is common practice when a new ranker is evaluated.

Related methods

Sources

  1. Robertson, S., & Zaragoza, H. (2009). The Probabilistic Relevance Framework: BM25 and Beyond. Foundations and Trends in Information Retrieval, 3(4), 333-389.

    Open DOI reference
  2. Robertson, S. E., Walker, S., Jones, S., Hancock-Beaulieu, M. M., & Gatford, M. (1995). Okapi at TREC-3. In Overview of the Third Text REtrieval Conference (TREC-3), NIST Special Publication 500-225, 109-126.

    Open publication reference