Inferensys

Glossary

BM25

A probabilistic, term-frequency-based sparse retrieval function that ranks documents by estimating the relevance of query terms, serving as the standard baseline for keyword search in hybrid systems.
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 RETRIEVAL FUNCTION

What is BM25?

BM25 is a probabilistic, term-frequency-based sparse retrieval function that ranks documents by estimating the relevance of query terms, serving as the standard baseline for keyword search in hybrid systems.

BM25, or Best Matching 25, is a bag-of-words retrieval function that scores a document's relevance to a query based on the frequency and distribution of matching terms. It improves upon classic TF-IDF by introducing term saturation—preventing high-frequency words from dominating—and document length normalization, which penalizes overly long documents to ensure fair comparison across a corpus.

As the standard sparse retrieval baseline in modern hybrid search architectures, BM25 excels at precise keyword matching and handling rare, out-of-vocabulary terms that dense embedding models may miss. Its output is typically fused with dense vector search results via reciprocal rank fusion to maximize both lexical precision and semantic recall in retrieval-augmented generation pipelines.

SPARSE RETRIEVAL FUNDAMENTALS

Key Features of BM25

BM25 is a probabilistic ranking function that estimates the relevance of documents to a given search query based on term frequency, inverse document frequency, and document length normalization. It remains the gold-standard baseline for keyword search in modern hybrid retrieval systems.

01

Term Frequency Saturation

Unlike raw TF-IDF, BM25 applies a non-linear saturation function to term frequency. This prevents documents from being scored disproportionately higher just because a query term appears many times. The formula tf / (tf + k1) ensures that after a certain point, additional occurrences add diminishing returns. The k1 parameter (typically between 1.2 and 2.0) controls the saturation curve's steepness.

  • A lower k1 means faster saturation—one or two mentions are enough
  • A higher k1 makes the function behave more like linear TF weighting
  • This reflects the real-world intuition that a document mentioning 'neural network' 100 times is not 100x more relevant than one mentioning it 10 times
02

Inverse Document Frequency (IDF) Weighting

BM25 uses IDF to assign higher weight to rare, discriminative query terms and lower weight to common words. The IDF component is computed as log((N - n + 0.5) / (n + 0.5)), where N is the total number of documents and n is the number containing the term.

  • Rare terms like 'tokenization' receive high IDF scores and dominate ranking
  • Common terms like 'system' receive near-zero IDF and contribute minimally
  • This ensures that distinctive query words drive retrieval precision
  • The +0.5 smoothing prevents division by zero and stabilizes scores for terms absent from the corpus
03

Document Length Normalization

BM25 normalizes term frequency by document length to avoid bias toward longer documents. The b parameter (typically 0.75) controls the degree of normalization. The formula 1 - b + b * (dl / avgdl) adjusts the effective term frequency based on how a document's length compares to the average.

  • b=1 applies full normalization—long documents are penalized proportionally
  • b=0 disables normalization entirely—pure TF scoring
  • The default b=0.75 reflects the assumption that longer documents are more likely to contain query terms by chance
  • This prevents verbose documents from dominating search results unfairly
04

Probabilistic Relevance Framework

BM25 is derived from the Binary Independence Model, a probabilistic framework that estimates the odds of a document being relevant given the presence or absence of query terms. It uses the Robertson-Spärck Jones weighting scheme, which models term distributions across relevant and non-relevant document sets.

  • Assumes term occurrences are independent (naive Bayes-like simplification)
  • Does not require explicit relevance judgments—uses IDF as a proxy
  • The Okapi BM25 variant refined the original model with the saturation and length components
  • Provides a theoretically grounded alternative to purely heuristic vector space models
05

Sparse Retrieval Efficiency

BM25 operates on sparse inverted indexes, making it extremely efficient for large-scale retrieval. Unlike dense embedding search, which requires GPU-accelerated ANN algorithms, BM25 can be executed on CPU with minimal memory overhead.

  • Inverted indexes map each term to a sorted list of document IDs and positions
  • Query processing involves simple integer arithmetic and list intersections
  • Retrieval latency is typically sub-millisecond for millions of documents
  • No model inference is required at query time—purely index-based lookup
  • This efficiency makes BM25 the ideal first-stage retriever in hybrid search pipelines
< 1 ms
Typical Query Latency
Millions
Documents Indexed on CPU
06

Hybrid Search Foundation

In modern Retrieval-Augmented Generation (RAG) architectures, BM25 serves as the sparse retrieval component in hybrid search systems. It complements dense vector search by excelling at exact keyword matching, which embedding models often miss.

  • Captures precise matches for proper nouns, product codes, and technical identifiers
  • Dense retrieval handles semantic similarity and paraphrasing
  • Results from both are fused using Reciprocal Rank Fusion (RRF) or weighted score interpolation
  • This combination consistently outperforms either method alone in benchmark evaluations
  • BM25 provides a strong baseline that ensures recall even when embedding models fail on domain-specific terminology
RETRIEVAL PARADIGM COMPARISON

BM25 vs. Dense Retrieval

A technical comparison of sparse lexical retrieval (BM25) against dense semantic retrieval across key architectural and performance dimensions.

FeatureBM25 (Sparse)Dense RetrievalHybrid

Core Mechanism

Term frequency & inverse document frequency

Embedding vector similarity

Weighted fusion of both

Semantic Understanding

Exact Keyword Match

Out-of-Vocabulary Handling

Zero-Shot Generalization

Index Size Overhead

Low

High (768+ dims)

High

Query Latency

< 10 ms

10-50 ms (ANN)

20-60 ms

Explainability

High (term weights)

Low (black box)

Moderate

BM25 EXPLAINED

Frequently Asked Questions

Clear, technical answers to the most common questions about the BM25 probabilistic retrieval function, its mechanisms, and its role in modern hybrid search systems.

BM25, or Best Match 25, is a bag-of-words probabilistic retrieval function that ranks documents by estimating the relevance of each document to a given search query. It works by calculating a score based on the term frequency (TF) of each query term in a document, balanced by the inverse document frequency (IDF) of that term across the entire corpus. The core innovation of BM25 is its non-linear saturation function for term frequency, which prevents a document from being scored infinitely higher just because it repeats a keyword many times. Additionally, it normalizes for document length, preventing longer documents from having an unfair advantage simply because they contain more words. The formula is: score(D, Q) = Σ IDF(qi) * (TF(qi, D) * (k1 + 1)) / (TF(qi, D) + k1 * (1 - b + b * (|D| / avgdl))), where k1 controls term frequency saturation and b controls document length normalization.

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.