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.
Glossary
BM25

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.
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.
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.
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
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
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
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
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
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
BM25 vs. Dense Retrieval
A technical comparison of sparse lexical retrieval (BM25) against dense semantic retrieval across key architectural and performance dimensions.
| Feature | BM25 (Sparse) | Dense Retrieval | Hybrid |
|---|---|---|---|
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 |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
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.
Related Terms
BM25 is a foundational component of modern hybrid search. Explore the complementary technologies and concepts that work alongside it to build production-grade retrieval systems.
TF-IDF: The Predecessor
The Term Frequency-Inverse Document Frequency weighting scheme that BM25 evolved from. While TF-IDF multiplies raw term frequency by inverse document frequency, BM25 introduces non-linear term frequency saturation and document length normalization to prevent longer documents from dominating rankings. TF-IDF lacks the probabilistic grounding and tunable parameters (k1, b) that make BM25 robust across diverse corpora.
Hybrid Search Fusion
The architectural pattern combining BM25's sparse lexical matching with dense vector similarity. This dual-retrieval approach compensates for each method's blind spots:
- BM25 excels at exact keyword matches, acronyms, and rare terms
- Dense vectors capture semantic similarity and paraphrasing Fusion strategies include reciprocal rank fusion (RRF) and convex combination of normalized scores, typically yielding 10-20% recall improvements over either method alone.
Sparse Vector Encoding
Modern systems represent BM25 scores as sparse vectors for efficient computation. Instead of storing full term-document matrices, only non-zero term weights are indexed. Technologies like SPLADE and uniCOIL extend this by learning sparse representations through neural models, bridging the gap between purely statistical BM25 and learned dense embeddings. These learned sparse vectors maintain the interpretability of lexical matching while capturing some semantic relationships.
Query Expansion Techniques
BM25's reliance on exact term matching makes it sensitive to vocabulary mismatch between queries and documents. Query expansion mitigates this by adding related terms before retrieval:
- Synonym expansion: Adding equivalent terms from a thesaurus
- Pseudo-relevance feedback: Extracting terms from top initial results
- Neural query rewriting: Using LLMs to generate alternative phrasings Effective expansion can improve BM25 recall by 15-30% without modifying the underlying index.
BM25F: Multi-Field Variant
An extension of BM25 designed for structured documents with multiple fields (title, abstract, body). BM25F calculates per-field term frequencies and combines them with field-specific weights before applying the saturation function. This prevents a term appearing in a low-importance field from contributing equally to one in a high-importance field. Implementations exist in Apache Lucene and Indri, making it essential for search over semi-structured content.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us