What is BM25 (Best Matching 25) Algorithm (original) (raw)

Last Updated : 16 Dec, 2025

BM25 (Best Matching 25) is a ranking algorithm used in information retrieval systems to determine how relevant a document is to a given search query. It’s an improved version of the traditional TF-IDF (Term Frequency–Inverse Document Frequency) approach and is widely used in modern search engines and databases.

In simple terms, BM25 helps rank documents or web pages based on how well they match a user’s search terms making it a cornerstone of effective search and retrieval systems.

bm25

BM25

Working of BM25

BM25 computes a relevance score between a query q and a document d using three main components: Term Frequency (TF), Inverse Document Frequency (IDF) and Document Length Normalization.

1. Term Frequency (TF)

Term frequency measures how often a query term appears in a document. Intuitively, a document containing a query term multiple times is more likely to be relevant. However, BM25 introduces a saturation effect i.e beyond a certain point, additional occurrences of a term contribute less to the score. This prevents overly long documents from being unfairly favored.

Mathematically, the term frequency component is normalized using the formula:

TF(t,d)=\frac{freq(t,d)}{freq(t,d) + k_1 . (1-b+b.\frac{|d|}{\text{avgdl}})}

where:

2. Inverse Document Frequency (IDF)

Inverse document frequency measures the importance of a term across the entire corpus. Rare terms are considered more informative than common ones. For example, the word "the" appears in almost every document and thus carries little value, whereas a rare term like "quantum" is more indicative of relevance.

The IDF component is calculated as:

IDF(t)=log(\frac{N-n_t+0.5}{n_t+0.5})

where:

3. Document Length Normalization

BM25 accounts for document length by normalizing scores to prevent longer documents from dominating the rankings. This is controlled by the parameter b which adjusts the influence of document length relative to the average document length (\text{avgdl}).

4. Final Score Calculation

The final BM25 score for a document d with respect to a query q is computed as:

Score(q,d) = \sum_{t\epsilon q}IDF(t).TF(t,d)

This sums up the contributions of all query terms t in the document d.

BM25 vs. Modern Dense Retrieval

Let's see the comparison between BM25 and Modern Dense Retrieval.

Aspect BM25 (Sparse/Term-based) Dense/Embedding-based Retrieval
Representation Term / lexical features (inverted index) Dense vector embeddings (semantic features)
Semantic matching Exact term or near‐term matches Captures synonyms, paraphrases, conceptual similarity
Computation cost Low (inverted index lookups) Higher (embedding generation, similarity search, GPU usage)
Interpretability High — scoring formula transparent Often lower — model internal weights less interpretable
Storage / indexing Sparse index structure, efficient Requires storing high-dimensional vectors, approximate nearest-neighbour (ANN) structures
Hybrid usage Often used for first‐stage retrieval Often used for re‐ranking or full retrieval in semantic tasks

Applications

Advantages

Limitations