BM25 (Best Match 25) is a probabilistic retrieval function that scores a document's relevance to a query by balancing term frequency saturation against inverse document frequency (IDF). Unlike simple TF-IDF, BM25 applies a non-linear saturation curve to term frequency, preventing a document that repeats a keyword 100 times from being scored 100 times more relevant than one that repeats it once. The function also normalizes for document length, preventing longer documents from having an unfair advantage in scoring.
Glossary
BM25

What is BM25?
BM25 is a probabilistic bag-of-words retrieval function that ranks documents based on term frequency saturation and inverse document frequency, serving as the standard baseline for sparse lexical search.
As the default algorithm in Elasticsearch and Lucene, BM25 serves as the ubiquitous sparse retrieval baseline. It operates purely on exact lexical matching, making it computationally efficient and highly effective for queries where precise keyword overlap matters. In modern Answer Engine Architecture, BM25 is typically deployed as the sparse leg of a hybrid retrieval strategy, where its scores are fused with dense vector similarity scores via Reciprocal Rank Fusion (RRF) to combine exact matching with semantic understanding.
Key Features of BM25
BM25 is not a single formula but a family of scoring functions with tunable parameters. These core features define its behavior and explain its enduring role as a sparse retrieval baseline.
Term Frequency Saturation
Unlike linear TF-IDF, BM25 applies a non-linear saturation curve to term frequency. The parameter k1 controls how quickly the impact of additional term occurrences diminishes. A document containing a term 5 times is not 5 times as relevant as one containing it once. This prevents keyword stuffing from dominating scores and models the diminishing returns of repeated term usage in natural language.
Document Length Normalization
BM25 normalizes term frequency by document length relative to the average corpus length. The b parameter (typically 0.75) controls the strength of this normalization:
b=1: Full normalization by lengthb=0: No length normalization This compensates for the higher probability of term occurrence in longer documents, ensuring short, dense documents can compete fairly with lengthy ones.
Inverse Document Frequency (IDF)
BM25 inherits the IDF component from TF-IDF, weighting rare terms more heavily than common ones. The standard IDF formula is:
codeIDF(q_i) = ln((N - n(q_i) + 0.5) / (n(q_i) + 0.5) + 1)
Where N is the total document count and n(q_i) is the number of documents containing the term. This gives high discriminative power to terms that appear in few documents.
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 2-Poisson model of term distribution, where relevant and non-relevant documents are modeled as two distinct Poisson distributions. This theoretical foundation distinguishes it from purely heuristic vector space models.
Tunable Parameters: k1 and b
BM25's behavior is governed by two free parameters:
- k1 (default 1.2): Controls term frequency saturation. Higher values give more weight to repeated terms.
- b (default 0.75): Controls length normalization. Higher values penalize long documents more. These parameters can be optimized via grid search against relevance judgments on a development set, allowing domain-specific tuning without retraining.
Sparse Lexical Representation
BM25 operates on sparse bag-of-words vectors, where each dimension corresponds to a vocabulary term. Unlike dense embeddings, these representations are:
- Interpretable: Exact term matches are visible
- Efficient: Inverted index lookups are extremely fast
- Zero-shot: No training data required This makes BM25 the standard baseline against which all neural retrieval models are benchmarked.
BM25 vs. TF-IDF vs. Dense Retrieval
A technical comparison of the core mechanisms, mathematical foundations, and operational characteristics distinguishing the three dominant text retrieval paradigms.
| Feature | BM25 | TF-IDF | Dense Retrieval |
|---|---|---|---|
Core Mechanism | Probabilistic relevance ranking with term saturation | Linear term frequency-inverse document frequency weighting | Semantic matching via dense vector embeddings |
Term Frequency Saturation | |||
Document Length Normalization | |||
Vocabulary Mismatch Handling | |||
Mathematical Foundation | Probabilistic relevance framework (Robertson-Spärck Jones) | Vector space model with heuristic weighting | Learned neural representations via dual-encoder transformers |
Scoring Complexity | O(|Q| * |D|) sparse dot product | O(|Q| * |D|) sparse dot product | O(d) approximate nearest neighbor search |
Tunable Parameters | k1 (term saturation) and b (length normalization) | None (deterministic formula) | Embedding model weights and similarity metric |
Out-of-Vocabulary Robustness | Zero score for unseen terms | Zero score for unseen terms | Generalizes to unseen terms via subword tokenization |
Frequently Asked Questions
Clear, technical answers to the most common questions about the BM25 probabilistic retrieval function, its mechanisms, and its role in modern search pipelines.
BM25 is a bag-of-words retrieval function that ranks documents by estimating the probability of relevance to a given query. It operates on two core principles: term frequency saturation and inverse document frequency (IDF).
Unlike a simple linear count, BM25 applies a non-linear saturation curve to term frequency using the formula tf / (k1 * ((1 - b) + b * (docLength / avgDocLength)) + tf). This prevents a document mentioning a term 100 times from being scored 100 times higher than one mentioning it once. The k1 parameter controls the saturation slope, while b controls document length normalization. The IDF component, typically computed as log((N - n + 0.5) / (n + 0.5)), penalizes common terms and rewards rare, discriminative ones. The final score for a query-document pair is the sum of the IDF-weighted saturated term frequencies for each matching query term.
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 is the standard baseline for sparse lexical search. These related concepts define the modern retrieval stack, from dense alternatives to fusion and evaluation.
Dense Retrieval
Encodes queries and documents into dense vector embeddings to perform semantic matching via approximate nearest neighbor (ANN) search. Unlike BM25's exact token matching, dense retrieval captures conceptual similarity and paraphrasing. It excels when vocabulary mismatch exists between query and document terms.
Hybrid Scoring
Fuses relevance signals from dense vector search and sparse lexical retrieval (BM25) to combine semantic understanding with exact keyword matching precision. This approach mitigates the weaknesses of each method: dense retrieval's struggle with rare entities and BM25's vocabulary mismatch problem.
Two-Stage Retrieval
A cascade architecture where a fast, lightweight retriever (often BM25 or a bi-encoder) selects candidate documents from a large corpus, and a computationally intensive cross-encoder re-ranker refines the ordering of the top-k candidates. BM25 is the most common first-stage retriever due to its speed and zero-shot capability.
Reciprocal Rank Fusion (RRF)
An unsupervised algorithm that merges multiple ranked lists into a single consensus ranking by scoring documents based on the reciprocal of their rank positions across input lists. Formula: score(d) = Σ 1/(k + rank_i(d)). It requires no training data or score calibration, making it ideal for combining BM25 and dense retrieval results.
Learning to Rank (LTR)
A supervised machine learning paradigm that trains models to optimize the ordering of documents for a given query. Unlike BM25's unsupervised formula, LTR models learn from relevance judgments using pointwise, pairwise, or listwise loss functions. LambdaMART is a classic gradient boosted tree implementation.
Normalized Discounted Cumulative Gain (NDCG)
A listwise ranking evaluation metric that measures ranking quality by discounting relevance gains logarithmically by position and normalizing against an ideal ranking. It is the standard metric for evaluating BM25 baselines against learned rankers, emphasizing top-weighted relevance.

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