BM25 (Best Matching 25) is a probabilistic ranking function used by search engines to estimate the relevance of documents to a given search query. It builds upon the TF-IDF (Term Frequency-Inverse Document Frequency) framework but introduces critical non-linear term frequency saturation and document length normalization. This prevents very long documents from dominating results simply by containing many query term repetitions, making BM25 more robust for real-world corpora with varied document lengths.
Glossary
BM25 (Best Matching 25)

What is BM25 (Best Matching 25)?
BM25 is a foundational algorithm in information retrieval that ranks documents by their estimated relevance to a search query.
The algorithm calculates a relevance score for each document by summing the contributions of each query term. Its core parameters control term frequency saturation (k1) and document length normalization (b), allowing it to be tuned for specific datasets. As a sparse retrieval method, BM25 operates on lexical keyword matching and is a cornerstone of hybrid search systems, where its precise lexical recall is combined with the semantic understanding of dense vector retrieval from embedding models.
BM25 vs. TF-IDF vs. Dense Vector Search
A technical comparison of three core information retrieval algorithms used in semantic indexing, highlighting their underlying mechanisms, performance characteristics, and ideal use cases for agentic memory systems.
| Feature / Metric | BM25 (Best Matching 25) | TF-IDF (Term Frequency-Inverse Document Frequency) | Dense Vector Search |
|---|---|---|---|
Core Mechanism | Probabilistic model using term frequency (TF), inverse document frequency (IDF), and document length normalization. | Statistical weight calculated as the product of term frequency (TF) within a document and inverse document frequency (IDF) across the corpus. | Semantic similarity search over dense, continuous vector embeddings generated by a neural network (e.g., SBERT, OpenAI embeddings). |
Representation Type | Sparse (lexical). Creates a high-dimensional vector where dimensions correspond to vocabulary terms. | Sparse (lexical). Similar high-dimensional vector based on term presence and corpus statistics. | Dense (semantic). Fixed, low-dimensional vector (e.g., 384, 768, 1536 dimensions) representing semantic meaning. |
Query Understanding | Exact keyword matching. Cannot match on synonyms or conceptual meaning without explicit expansion. | Exact keyword matching. Suffers from the same vocabulary mismatch problem as BM25. | Semantic understanding. Maps queries and documents to a shared latent space, enabling synonym and concept matching. |
Handles Vocabulary Mismatch | |||
Built-in Document Length Normalization | |||
Requires Training Data | |||
Typical Index Structure | Inverted index for fast lookups of term-to-document mappings. | Inverted index, similar to BM25. | Dense vector index (e.g., HNSW, IVF) for approximate nearest neighbor (ANN) search. |
Inference Latency | < 10 ms | < 10 ms | 10-100 ms (highly dependent on index type and scale) |
Index Storage Footprint | Medium (stores postings lists). | Medium (similar to BM25). | High (stores full floating-point vectors for all chunks/documents). |
Ideal Use Case | Precise keyword retrieval, legal document search, e-commerce product search where exact term matching is critical. | Simple baseline retrieval, document similarity for small, static corpora. | Semantic search, question answering, retrieval-augmented generation (RAG) where understanding user intent and paraphrasing is key. |
Commonly Paired With | Hybrid search with dense vectors, metadata filtering. | Limited modern use; often a pedagogical baseline. | Hybrid search with BM25, re-ranking models, knowledge graph retrieval. |
Frequently Asked Questions
BM25 is a foundational probabilistic ranking algorithm in information retrieval. These questions address its core mechanics, its role in modern search systems, and its practical application for engineers building semantic search and agentic memory systems.
BM25 (Best Matching 25) is a probabilistic ranking function used by search engines to estimate the relevance of documents to a given search query. It works by scoring documents based on the term frequency (TF) of query words within the document, balanced by the inverse document frequency (IDF) of those words across the entire corpus, with built-in normalization for document length to prevent bias towards longer texts. The core formula incorporates tunable parameters (k1 and b) to control the saturation of term frequency and the strength of length normalization, respectively. Unlike simple keyword matching, BM25's probabilistic foundation models the information content of terms, making it robust and highly effective for sparse lexical search.
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 information retrieval and semantic search technologies. These related concepts define the algorithms, data structures, and strategies used to segment, index, and retrieve information efficiently.
Inverted Index
An inverted index is the foundational data structure for sparse, keyword-based retrieval like BM25. It maps each unique term (or token) to a list of documents (and often positions within those documents) where the term appears.
- Core Mechanism: Enables fast full-text search by allowing the system to look up a term and immediately retrieve all documents containing it.
- Contrast with BM25: BM25 uses the statistics derived from an inverted index (term frequencies, document frequencies) to compute its probabilistic relevance score. The index provides the raw data; BM25 provides the ranking algorithm.
Hybrid Search
Hybrid search is an information retrieval strategy that combines the results of sparse (e.g., keyword-based BM25) and dense (e.g., vector similarity) retrieval methods.
- Implementation: Typically uses a weighted score fusion (e.g., reciprocal rank fusion or a learned linear combination) to merge ranked lists from both retrievers.
- Rationale: Leverages the precise lexical matching of BM25 for fact-seeking queries while utilizing the semantic understanding of vector search for conceptual or paraphrased queries. This combination often yields higher recall and precision than either method alone.
Term Frequency-Inverse Document Frequency (TF-IDF)
TF-IDF is a statistical measure used to evaluate the importance of a word to a document in a collection. It is the direct conceptual predecessor to BM25.
- TF (Term Frequency): Measures how often a term appears in a document.
- IDF (Inverse Document Frequency): Measures how rare or common a term is across the entire corpus.
- Key Difference from BM25: TF-IDF produces a static weight for a term-document pair. BM25 improves upon this by introducing non-linear term frequency saturation and document length normalization, making it more robust for ranking in search engines.
Dense Passage Retrieval (DPR)
Dense Passage Retrieval is a neural retrieval architecture that represents queries and documents as dense vector embeddings in a shared semantic space.
- Core Mechanism: Uses separate BERT-based encoders to map questions and passages into high-dimensional vectors. Relevance is scored via vector similarity (e.g., dot product).
- Contrast with BM25: DPR is a dense retriever that captures semantic meaning, allowing it to match queries with lexically different but semantically similar passages. BM25 is a sparse retriever that relies on exact keyword overlap. DPR requires significant training data, while BM25 is unsupervised.
Query Expansion
Query expansion is a technique to improve retrieval recall by augmenting a user's original search query with additional related terms or phrases.
- Methods: Can be rule-based (using a thesaurus), statistical (using pseudo-relevance feedback), or neural (using a language model to generate expansions).
- Synergy with BM25: Since BM25 relies on term overlap, a poorly formulated query may miss relevant documents. Query expansion introduces synonymous or related terms, increasing the chance of lexical matches and improving BM25's effectiveness, especially for short or ambiguous queries.
Maximal Marginal Relevance (MMR)
Maximal Marginal Relevance is a re-ranking algorithm designed to reduce redundancy and increase diversity in a set of retrieved documents.
- Core Function: Selects documents that are both relevant to the query and sufficiently novel compared to documents already selected.
- Use with BM25: After BM25 retrieves a top-k set of relevant documents, MMR can reorder them. This is crucial in Retrieval-Augmented Generation (RAG) to ensure the language model receives a set of passages that cover different aspects of the query, rather than multiple near-identical passages that all score highly on BM25.

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