BM25
BM25 is a probabilistic information-retrieval ranking function that scores documents for a query using term frequency, inverse document frequency, and document-length normalization.
Elasticsearch in Action, Second Edition
Okapi BM25 is a probabilistic information-retrieval ranking algorithm that scores documents using term frequency, document frequency, and document-length normalization.
- How it is used
- Ranks full-text Elasticsearch results by a relevance score so stronger document matches appear first.
- How deep it goes
- Explains term frequency, inverse document frequency, length normalization, saturation, parameters, and per-field selection, while deferring custom algorithms.
- Examples
- Uses Java book-title searches, contrasting Effective Java, Head First Java, and a title containing Java three times.
- What you can do after
- Operate/configure it by selecting similarity per field and recognizing how frequency and field length affect ranking.
Read how Elasticsearch in Action, Second Edition explains BM25 →
Domain-Specific Small Language Models
A lexical information-retrieval ranking algorithm that scores documents using term frequency, inverse document frequency, and document length.
- How it is used
- Ranks local document chunks by keyword relevance for retrieval in an agentic RAG workflow.
- How deep it goes
- Defines the ranking factors and implements retrieval through a library; stops short of deriving the scoring formula.
- Examples
- Uses a chunked PDF paper and a smart-home query to demonstrate BM25 retrieval and a weighted hybrid retriever.
- What you can do after
- Apply it via LangChain’s BM25Retriever as an agent tool or combine it with semantic retrieval using EnsembleRetriever.
Read how Domain-Specific Small Language Models explains BM25 →
LLM Evaluation and Alignment, The Foundational Ideas
BM25 is a probabilistic ranking function that scores documents for a query using term frequency, inverse document frequency, and document-length normalization.
- How it is used
- Deploys BM25 for sparse retrieval when precise terminology should strongly influence document ranking.
- How deep it goes
- Defines BM25 through its scoring factors and identifies its strengths, failure modes, and evaluation metrics without deriving the formula.
- Examples
- Contrasts precise-terminology queries with conceptual or paraphrased questions to show when BM25 or dense search is preferable.
- What you can do after
- Diagnose retrieval failures, choose BM25 for terminology-heavy queries, and evaluate rankings with precision, recall, and MAP.
Read how LLM Evaluation and Alignment, The Foundational Ideas explains BM25 →
The Art of AI Product Development
BM25 is a lexical ranking function that scores documents using term frequency, document frequency, and document-length normalization.
- How it is used
- Deploys BM25 alongside semantic search to improve precision for technical, product-specific, and ambiguous queries.
- How deep it goes
- Defines scoring through term frequency, document frequency, and length normalization without deriving the ranking algorithm.
- Examples
- Uses product taxonomy phrases, API endpoints, and the ambiguous term “business” to demonstrate exact-match needs.
- What you can do after
- Apply BM25 through hybrid retrieval to prioritize exact terms while retaining semantically related context.
Retrieval Augmented Generation, The Foundational Ideas
BM25 is a probabilistic term-weighting and document-ranking function based on term frequency, inverse document frequency, and document-length normalization.
- How it is used
- Ranks documents for exact-term queries when lexical overlap is the intended retrieval signal.
- How deep it goes
- Explains the scoring variables, frequency saturation, and length normalization, but stops short of semantic matching.
- Examples
- Uses a five-document corpus comparing automobile, car, vehicle, maintenance, and unrelated computer terms.
- What you can do after
- Apply BM25 via the rank_bm25 Python implementation and interpret exact-term ranking failures.
Read how Retrieval Augmented Generation, The Foundational Ideas explains BM25 →
Building Reliable AI Systems
BM25 is a keyword-based ranking algorithm that scores documents according to query-term frequency while accounting for document length and term saturation.
- How it is used
- Ranks documents by query-term frequency as a fast, reproducible keyword-retrieval baseline.
- How deep it goes
- Explains term-frequency ranking and common-word penalties qualitatively, without deriving a scoring formula.
- Examples
- Uses an iPhone troubleshooting search and a telecom roaming query with device and plan details.
- What you can do after
- Apply BM25 retrieval with Haystack's InMemoryBM25Retriever and connect extracted metadata filters to the retriever.
Generative AI in Action
BM25 is a probabilistic ranking function that estimates document relevance to a query using term frequency, inverse document frequency, document length, and term-frequency saturation.
- How it is used
- Ranks documents for retrieval-augmented generation and search queries where lexical matching and efficiency are important.
- How deep it goes
- Defines ranking inputs and contrasts alternatives but stops short of mathematical derivation or implementation details.
- Examples
- Uses query-term frequency, inverse document frequency, document length, and term-frequency saturation as ranking signals.
- What you can do after
- Evaluate/understand retriever choices using lexical matching, semantic understanding, computational efficiency, and resource availability.
Also covered in
- Natural Language Processing in Action, Second Edition
- Designing AI SystemsIn early access (MEAP)
- Build an AI Agent (From Scratch)
What to know first
- retrieval-augmented generationRetrieval-augmented generation is a technique that retrieves relevant information from external sources and supplies it as context to a generative model when producing an output.
- similarity searchSimilarity search is a retrieval technique that finds or ranks items by their similarity to a query, often by comparing feature or vector representations under a similarity or distance measure.
- embeddingAn embedding is a numerical vector representation of an item in a feature space, encoding selected properties or relationships for computational tasks such as comparison or retrieval.
- BERTBERT (Bidirectional Encoder Representations from Transformers) is a pretrained Transformer encoder language model that produces contextual text representations using information from both directions.
Related concepts
- lexical searchkind of · Lexical search retrieves or ranks items by matching query terms or other surface-text features against indexed content, often using sparse representations and statistical weighting.
- hybrid searchused by · Hybrid search is an information-retrieval technique that combines multiple retrieval methods or relevance signals to find or rank relevant results.
- similarity searchcontrasts with · Similarity search is a retrieval technique that finds or ranks items by their similarity to a query, often by comparing feature or vector representations under a similarity or distance measure.
- term frequency-inverse document frequencyalternative to · Term frequency-inverse document frequency (TF-IDF) is a text-weighting scheme that combines a term’s frequency in a document with its inverse frequency across a document collection.
- relevanceproduces · Relevance is the degree to which information, a result, or a response pertains to a query, task, purpose, or information need.
- Elasticsearchused by · Elasticsearch is a distributed search and analytics engine for indexing, querying, and analyzing structured and unstructured data.
Questions about BM25
What is BM25?
BM25 is a probabilistic information-retrieval ranking function that scores documents for a query using term frequency, inverse document frequency, and document-length normalization.
Which Manning books explain BM25?
Elasticsearch in Action, Second Edition, Domain-Specific Small Language Models, LLM Evaluation and Alignment, The Foundational Ideas, The Art of AI Product Development, Retrieval Augmented Generation, The Foundational Ideas, Building Reliable AI Systems and Generative AI in Action.
What should I know before learning BM25?
Manning books that teach BM25 usually introduce retrieval-augmented generation, similarity search, embedding and BERT first.
What is the difference between BM25 and similarity search?
BM25 is a probabilistic information-retrieval ranking function that scores documents for a query using term frequency, inverse document frequency, and document-length normalization. Similarity search is a retrieval technique that finds or ranks items by their similarity to a query, often by comparing feature or vector representations under a similarity or distance measure.
What is BM25 a kind of?
BM25 is a kind of lexical search. Lexical search retrieves or ranks items by matching query terms or other surface-text features against indexed content, often using sparse representations and statistical weighting.
What is BM25 also called?
BM25 is also called Best Match 25 or Best Matching 25.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.






