Inferensys

Glossary

BM25 (Okapi BM25)

BM25 is a probabilistic ranking function used in sparse retrieval that scores documents based on the frequency of query terms, with built-in term frequency saturation and document length normalization.
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

What is BM25 (Okapi BM25)?

BM25 is the foundational probabilistic ranking algorithm for sparse, keyword-based search.

BM25 (Okapi BM25) is a probabilistic ranking function used in sparse retrieval to score and rank documents based on their relevance to a keyword query. It improves upon the classic TF-IDF model by introducing non-linear term frequency saturation and document length normalization, which prevent very long documents from dominating results simply by containing more terms. This makes it the de facto standard for efficient, high-recall lexical search in systems like Apache Lucene and Elasticsearch.

The algorithm calculates a relevance score by summing the contributions of each query term present in a document. Its key parameters—k1 and b—control term frequency saturation and length normalization, respectively, allowing it to be tuned for specific corpora. In modern Retrieval-Augmented Generation (RAG) pipelines, BM25 is often used in a hybrid retrieval system alongside dense retrieval methods like vector search to combine the broad recall of lexical matching with the semantic understanding of neural embeddings.

SPARSE RETRIEVAL

Key Features of BM25

BM25 (Okapi BM25) is a probabilistic ranking function that scores documents based on query term frequency, with built-in saturation and normalization. These features make it a robust, tunable baseline for keyword search.

01

Term Frequency Saturation

BM25 applies a non-linear saturation function to raw term frequency counts. This prevents very long documents with many repetitive terms from dominating the rankings disproportionately.

  • The core formula uses the parameter k1 to control the saturation curve. A common default is k1 = 1.2.
  • As term frequency increases, its contribution to the score increases at a diminishing rate. This models the principle of diminishing returns in relevance.
  • Example: A term appearing 10 times is more relevant than appearing once, but not ten times more relevant.
02

Document Length Normalization

BM25 normalizes scores based on document length relative to the average document in the collection, penalizing very long documents.

  • This is controlled by the parameter b, which typically ranges from 0 to 1. A common default is b = 0.75.
  • With b = 0, length normalization is disabled. With b = 1, full normalization is applied.
  • The mechanism counteracts the tendency of longer documents to have higher term frequencies simply because they contain more words, addressing the verbosity hypothesis.
03

Inverse Document Frequency (IDF)

BM25 incorporates a variant of Inverse Document Frequency to weight terms by their rarity across the entire corpus.

  • Common terms (e.g., 'the', 'is') that appear in many documents receive a low IDF weight, reducing their impact on the final score.
  • Rare, specific terms that appear in few documents receive a high IDF weight, making them strong signals of relevance.
  • This feature allows BM25 to prioritize discriminative keywords that effectively narrow down the result set.
04

Field-Aware Scoring

In modern implementations like Elasticsearch, BM25 can be applied per-field, allowing different tuning for titles, body text, or metadata.

  • This enables field boosting, where matches in a title field can be weighted more heavily than matches in a body text field.
  • Parameters (k1, b) can be set independently for different fields based on their characteristics.
  • This mirrors the concept of pivoted length normalization from earlier IR research, adapting the core algorithm to structured documents.
05

Absence of Query Term Coordination

Unlike older Boolean-influenced models, BM25 does not explicitly reward documents that match a higher number of unique query terms (query coordination).

  • The score is a sum of the contributions from each query term independently.
  • A document matching four query terms will typically score higher than one matching two, but this is a side effect of summation, not a built-in coordination factor.
  • This design simplifies the probabilistic model and often proves more robust, as it avoids over-penalizing documents that are highly relevant but miss one uncommon term.
06

Tunable Parameters (k1 & b)

BM25's behavior is controlled by two core, interpretable hyperparameters, making it adaptable to different document collections.

  • k1: Controls term frequency saturation. Lower values (~0.5-1.0) lead to quicker saturation, flattening the impact of high frequencies. Higher values (~1.5-2.0) let frequency have a stronger linear influence.
  • b: Controls document length normalization. b = 0 applies no normalization; b = 1 applies full normalization. Tuning this is critical for collections with highly variable document lengths.
  • Optimal parameters are often found via grid search on a relevance-labeled dataset (e.g., using nDCG or MAP).
SPARSE RETRIEVAL ALGORITHMS

BM25 vs. TF-IDF: A Technical Comparison

A detailed comparison of two foundational sparse retrieval algorithms used in lexical search, highlighting their mathematical formulations, practical behaviors, and suitability for modern hybrid retrieval systems.

Feature / MetricBM25 (Okapi BM25)TF-IDF

Core Ranking Function

Probabilistic relevance framework (Robertson/Spärck Jones)

Vector space model (Salton et al.)

Term Frequency (TF) Saturation

Document Length Normalization

Non-linear, tunable parameter (b)

Cosine normalization (linear)

Inverse Document Frequency (IDF) Formulation

Probabilistic IDF (Robertson)

Standard logarithmic IDF

Tunable Hyperparameters

k1 (TF saturation), b (length norm)

Handles Common Terms (Stop Words)

Yes, via IDF dampening

Poorly, requires explicit stop word lists

Typical Use Case

First-stage retrieval in modern RAG, web search

Baseline retrieval, simple document search

Performance on Long Documents

Superior, due to robust length normalization

Degrades, prone to bias towards longer docs

Integration Complexity (e.g., with Dense Retrieval)

Low, standard in libraries (Elasticsearch, Pyserini)

Medium, often requires custom implementation for fusion

APPLICATIONS

Where is BM25 Used?

BM25's robust, term-based scoring makes it a foundational component in numerous production systems, from enterprise search to modern AI architectures.

02

Hybrid Retrieval for RAG

In Retrieval-Augmented Generation (RAG) systems, BM25 is a key component of hybrid retrieval. It operates as the sparse retriever, complementing dense vector search. This combination improves recall by catching relevant documents that may use different terminology than the query. BM25 excels at matching named entities, acronyms, and technical jargon where lexical overlap is a strong signal, providing a robust baseline that semantic search can refine.

03

First-Stage Retrieval (Recall)

Due to its speed and efficiency, BM25 is frequently deployed as the first-stage retriever in a two-stage retrieval pipeline. It quickly scans millions of documents using an inverted index to produce a candidate set (e.g., top 100-1000 results) with high recall. These candidates are then passed to a more computationally expensive cross-encoder reranker for precise scoring. This architecture balances low latency with high final accuracy.

04

Enterprise & E-Discovery

BM25 is extensively used in enterprise search and legal e-discovery platforms. These domains often deal with precise, keyword-heavy queries (e.g., contract clauses, part numbers, case references) where term frequency and document length are reliable relevance indicators. Its deterministic, non-machine learning nature provides explainability—users can see which terms matched and contributed to the score—which is crucial for audit trails and legal compliance.

05

Academic & Patent Search

Specialized search systems in academic literature (e.g., PubMed, arXiv search) and patent databases rely on BM25 and its variants. These corpora contain highly technical language where specific term usage is paramount. The algorithm's term frequency saturation prevents overly long documents (like lengthy research papers) from dominating results, while its inverse document frequency component correctly weights rare, domain-specific terms.

06

Baseline for IR Research

In information retrieval research, BM25 serves as the standard baseline against which new neural and learned retrieval models are evaluated. Its performance on benchmarks like MS MARCO and TREC tracks is well-established, providing a stable point of comparison. A new model must consistently outperform BM25 to be considered an advancement, cementing its role as a fundamental benchmark for retrieval effectiveness.

BM25 (OKAPI BM25)

Frequently Asked Questions

BM25 is a foundational probabilistic ranking function for sparse, keyword-based search. These FAQs address its core mechanics, practical applications, and its critical role in modern hybrid retrieval systems.

BM25 (Best Matching 25), also known as Okapi BM25, is a probabilistic ranking function used in sparse retrieval to score and rank documents based on their relevance to a given keyword query. It works by calculating a relevance score for each document using a formula that balances three core factors: the frequency of query terms within the document (term frequency), the inverse frequency of terms across the entire corpus (inverse document frequency), and a normalization factor for document length. Unlike simpler models, BM25 incorporates non-linear term frequency saturation to prevent very high term counts from disproportionately dominating the score and a document length normalization component to fairly compare documents of varying sizes.

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.