Inferensys

Glossary

BM25

A probabilistic, bag-of-words retrieval function that ranks documents by estimating the relevance of term frequencies while accounting for document length normalization and term saturation, serving as the standard baseline for sparse lexical search.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
SPARSE LEXICAL RETRIEVAL

What is BM25?

BM25 is a probabilistic, bag-of-words retrieval function that ranks documents by estimating the relevance of term frequencies while accounting for document length normalization and term saturation, serving as the standard baseline for sparse lexical search.

BM25, or Best Match 25, is a ranking function that scores a document's relevance to a query based on the frequency of matching terms, but with two critical controls: term saturation and document length normalization. Term saturation prevents a single keyword from dominating the score simply by being repeated excessively, while length normalization corrects the bias that longer documents naturally have a higher chance of containing a query term, ensuring a fair comparison between a short abstract and a lengthy manual.

As the foundational algorithm for sparse retrieval, BM25 relies on an inverted index for computationally efficient, exact keyword matching, making it highly interpretable and effective for queries where precise terminology is critical. In modern hybrid retrieval strategies, BM25's lexical precision is combined with the semantic understanding of dense vector search—a pairing often merged using algorithms like Reciprocal Rank Fusion (RRF)—to maximize both recall and precision in systems like Retrieval-Augmented Generation (RAG).

PROBABILISTIC LEXICAL RETRIEVAL

Core Characteristics of BM25

BM25 is a bag-of-words retrieval function that ranks documents by estimating the relevance of term frequencies while accounting for document length normalization and term saturation, serving as the standard baseline for sparse lexical search.

01

Term Frequency Saturation

Unlike linear TF-IDF weighting, BM25 applies a non-linear saturation curve to term frequency. The formula tf / (tf + k1) ensures that the fifth occurrence of a term contributes far less than the first. This prevents keyword stuffing from artificially inflating relevance scores.

  • k1 parameter (typically 1.2–2.0): Controls the slope of the saturation curve
  • A higher k1 means term frequency has a more linear, prolonged impact
  • A lower k1 means rapid saturation after the first few occurrences
  • This reflects the empirical observation that relevance does not increase linearly with term repetition
02

Document Length Normalization

BM25 compensates for the fact that longer documents naturally contain more words and higher term frequencies by applying a length normalization factor. The parameter b (typically 0.75) controls the strength of this correction.

  • b = 1: Full normalization—document length is fully accounted for
  • b = 0: No normalization—long documents have an unfair advantage
  • The normalization uses the ratio of document length to average corpus length
  • This ensures a short, highly relevant document can outrank a long, marginally relevant one
03

Inverse Document Frequency Component

BM25 incorporates an IDF-like term specificity weight that penalizes common words and rewards rare, discriminative terms. The standard formulation uses log((N - df + 0.5) / (df + 0.5)) where N is the total document count and df is the number of documents containing the term.

  • Rare terms that appear in few documents receive high IDF weights
  • Common stopwords that appear in most documents approach zero weight
  • This component is computed once per term across the entire corpus
  • Unlike TF-IDF, BM25's IDF is derived from a probabilistic relevance framework
04

Probabilistic Relevance Framework

BM25 is grounded in the Probability Ranking Principle, which states that documents should be ranked by their probability of relevance given the query. It is derived from the Binary Independence Model using a 2-Poisson approximation for term distributions.

  • The scoring function estimates P(relevant | document, query)
  • Term independence is assumed (bag-of-words model)
  • The formula balances term frequency evidence against document length
  • This probabilistic foundation provides theoretical justification for the heuristic components
05

BM25F: Multi-Field Extension

BM25F extends the base algorithm to handle structured documents with multiple fields (title, body, anchor text) by computing a weighted, length-normalized term frequency across all fields before applying the standard BM25 formula.

  • Each field receives its own boost weight and length normalization parameter
  • Title matches can be weighted more heavily than body matches
  • The combined pseudo-term-frequency is fed into the standard BM25 equation
  • This is the foundation for search in systems like Elasticsearch and Apache Lucene
06

BM25L: Long Document Correction

BM25L addresses a known deficiency where BM25 unfairly penalizes very long documents that contain relevant information distributed across many sections. It modifies the term frequency component to prevent the score from asymptotically approaching zero for long documents.

  • Introduces an additional parameter δ to adjust the saturation curve
  • Prevents excessively long documents from being systematically demoted
  • Particularly useful for book-length documents or technical manuals
  • Maintains the core probabilistic framework while fixing edge-case behavior
SPARSE RETRIEVAL EXPLAINED

Frequently Asked Questions

Clear, technical answers to the most common questions about the BM25 algorithm, its role in modern hybrid search, and how it compares to dense vector retrieval.

BM25 is a probabilistic, bag-of-words retrieval function that ranks documents by estimating the relevance of term frequencies while accounting for document length normalization and term saturation. It operates on a sparse inverted index, meaning it only considers exact token matches between the query and documents.

At its core, BM25 calculates a score for each matching term and sums them. The formula has three critical components:

  • Term Frequency (TF) Saturation: Unlike simple TF, BM25 applies a non-linear saturation function (tf / (k1 + tf)). The parameter k1 (typically 1.2–2.0) controls how quickly additional occurrences stop adding value. A word appearing 100 times isn't 100x more relevant than one appearing 10 times.
  • Inverse Document Frequency (IDF): This dampens common words. A term that appears in nearly every document gets a weight near zero, while a rare term gets a high weight. The standard formula is log((N - df + 0.5) / (df + 0.5)).
  • Document Length Normalization: The b parameter (typically 0.75) controls how much document length matters. A b of 1.0 fully normalizes by length; a b of 0 ignores length entirely. This prevents longer documents from having an unfair advantage simply because they contain more words.

The final score for a document D given a query Q is the sum over each query term t: IDF(t) * (tf(t, D) * (k1 + 1)) / (tf(t, D) + k1 * (1 - b + b * |D|/avgdl)).

SPARSE LEXICAL VS. DENSE SEMANTIC VS. NEURAL SPARSE

BM25 vs. Dense Retrieval vs. Learned Sparse Retrieval

A technical comparison of three core retrieval paradigms for modern answer engines, contrasting their indexing mechanisms, matching logic, and operational trade-offs.

FeatureBM25Dense RetrievalLearned Sparse Retrieval

Core Mechanism

Probabilistic term frequency with TF saturation and document length normalization

Bi-encoder neural network encoding text into dense vectors for cosine similarity search

Neural model predicting term importance weights for vocabulary tokens, generating sparse vectors

Index Type

Inverted index with term-level postings lists

Vector index using ANN algorithms like HNSW or IVF-PQ

Inverted index with learned term weights instead of raw TF

Matching Paradigm

Exact lexical token matching

Semantic similarity in latent embedding space

Learned lexical matching with contextual term weighting

Out-of-Vocabulary Handling

Zero-Shot Domain Transfer

Exact Term Matching Precision

Query Latency (Relative)

< 10 ms

10-50 ms

< 15 ms

Index Size (Relative to Raw Text)

10-30%

200-500%

15-40%

Training Data Required

Large-scale query-document pairs or contrastive data

Training corpus for term importance prediction

Explainability

High — scores directly attributable to term frequencies

Low — scores are opaque latent space distances

Medium — weights are vocabulary-grounded but learned

Synonym and Paraphrase Handling

Document Length Sensitivity

Normalized via pivot length parameter

Minimal — fixed-size vector output

Minimal — learned term weighting adapts

Typical Recall@1000 on BEIR

~0.85

~0.95

~0.92

Typical MRR@10 on BEIR

~0.30

~0.42

~0.38

Fusion Compatibility

Requires score normalization for RRF or weighted sum

Native cosine similarity scores; easily fused

Scores are sparse but require normalization for fusion

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.