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.
Glossary
BM25 (Okapi BM25)

What is BM25 (Okapi BM25)?
BM25 is the foundational probabilistic ranking algorithm for sparse, keyword-based search.
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.
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.
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.
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. Withb = 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.
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.
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.
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.
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 = 0applies no normalization;b = 1applies 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).
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 / Metric | BM25 (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 |
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.
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.
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.
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.
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.
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.
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.
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.
Related Terms
BM25 operates within a broader ecosystem of search and retrieval technologies. Understanding these related concepts is essential for designing effective hybrid systems that combine lexical and semantic methods.
Sparse Retrieval
Sparse retrieval is the traditional search paradigm where documents are represented by high-dimensional vectors with most dimensions zero, indicating the presence or absence of specific vocabulary terms. BM25 is its most prominent ranking function.
- Core Mechanism: Relies on lexical matching of query keywords against an inverted index.
- Strengths: Excellent for keyword recall, handles out-of-vocabulary terms, and is computationally efficient.
- Limitation: Struggles with semantic matching (e.g., synonyms, paraphrases).
TF-IDF
Term Frequency-Inverse Document Frequency is a foundational statistical weighting scheme that informs BM25. It evaluates a term's importance in a document relative to a corpus.
- Term Frequency (TF): Measures how often a term appears in a document.
- Inverse Document Frequency (IDF): Penalizes terms that appear in many documents (common words).
- Comparison to BM25: While TF-IDF is a simple product, BM25 introduces non-linear term frequency saturation and document length normalization, making it more robust for ranking.
Inverted Index
An inverted index is the fundamental data structure that enables efficient sparse retrieval. It is a mapping from each unique vocabulary term to a postings list of documents containing that term.
- Function: Allows rapid lookup of which documents contain specific query terms.
- Key for BM25: The index stores the necessary statistics (term frequencies, document lengths) used in the BM25 scoring formula.
- Implementation: Core component of search libraries like Apache Lucene.
Dense Retrieval
Dense retrieval is the neural counterpart to sparse retrieval. It represents queries and documents as dense, low-dimensional vector embeddings where semantic similarity is measured by proximity in vector space.
- Core Mechanism: Uses an embedding model (e.g., Sentence-BERT) and vector search.
- Strengths: Excels at semantic matching, understanding paraphrases and conceptual queries.
- Contrast with BM25: BM25 is lexical and statistical; dense retrieval is semantic and neural. They are often combined in hybrid retrieval.
Hybrid Retrieval
Hybrid retrieval is an architecture that combines sparse (e.g., BM25) and dense retrieval methods to improve both recall and precision.
- Rationale: Leverages the keyword recall of BM25 and the semantic understanding of dense vector search.
- Fusion Methods: Results are combined using techniques like Reciprocal Rank Fusion (RRF) or weighted score interpolation.
- System Design: Represents the state-of-the-art approach in production Retrieval-Augmented Generation (RAG) systems to ensure comprehensive and relevant context retrieval.
Reciprocal Rank Fusion (RRF)
Reciprocal Rank Fusion is a robust, score-agnostic method for merging ranked lists from different retrievers (e.g., BM25 and a vector search).
- Formula: The fused score for a document is the sum of
1 / (k + rank)from each list, whererankis its position in that list andkis a constant (typically 60). - Advantage: Does not require score normalization between disparate systems like BM25 (unbounded scores) and cosine similarity (bounded between -1 and 1).
- Use Case: The preferred fusion technique in many hybrid retrieval implementations.

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