Language AI GUIDE

BM25 and Lexical Retrieval

BM25 is the classic keyword-based ranking function that scores documents by how often query terms appear, adjusted for term rarity and document length.

Listen to this guideAI-narrated · ~2 min

On this page2 min read
  1. Overview
  2. Deep Dive
  3. Real-World Implementation
  4. The Future of BM25 and Lexical Retrieval
  5. Putting it into practice
  6. Keep Exploring
  7. Frequently asked questions

Overview

Decades old, it remains a remarkably strong and ubiquitous baseline for search.

Deep Dive

BM25 (Best Matching 25) is a bag-of-words ranking function from the probabilistic Okapi framework of the 1990s. For each query term it combines three signals: term frequency (how often the word appears in a document, with diminishing returns controlled by a parameter k1), inverse document frequency (rarer words across the collection count more), and document-length normalization (parameter b, so long documents are not unfairly favored). Sum these per-term scores and you get the document's rank. It needs no training and runs blazingly fast via inverted indexes, which is why search engines like Elasticsearch and Lucene use it by default. Despite the rise of neural retrieval, BM25 still wins or ties on many benchmarks, especially for rare terms, exact identifiers, and out-of-domain queries.

Real-World Implementation

Default relevance ranking in Elasticsearch, OpenSearch, and Apache Lucene/Solr

First-stage candidate retrieval that feeds a slower neural reranker in two-stage search

Code and log search where exact identifiers and error codes must match precisely

Mining hard negative examples to train dense retrievers like DPR

The Future of BM25 and Lexical Retrieval

BM25 is unlikely to disappear; instead it is increasingly paired with neural methods in hybrid retrieval, where lexical and dense scores are fused (often via reciprocal rank fusion). Learned sparse models like SPLADE blend BM25-style sparsity with neural term weighting, and BM25 frequently serves as the first-stage retriever before neural rerankers. Its speed, interpretability, and zero training cost guarantee a lasting role in production search.

Putting it into practice

Why it matters

  • Speed and scale. Language workflows can move faster without sacrificing consistency.
  • Access and reach. It expands access across languages and communication styles.
  • Clearer decisions. Teams can spend more time on judgment while automation handles repetition.

Watch out for

  • Hallucinated facts can quietly enter reports, support flows, or research outputs.
  • Prompt sensitivity can create inconsistent results across similar requests.
  • Sensitive text data may be exposed if access controls are weak.

Next steps

  1. Define output format, tone, and quality standards before rollout.
  2. Ground responses with trusted sources whenever accuracy matters.
  3. Keep a human review checkpoint for high-stakes outputs.
  4. Track failure patterns and retrain prompts or workflows regularly.

Keep Exploring

Free newsletter

Get the daily AI briefing

Three verified AI stories every weekday morning, written in plain English. Free forever, no ads.

One email each weekday. Unsubscribe in one click. We never sell or share your address.

Test yourself

Take the BM25 and Lexical Retrieval quiz

Instant feedback on every answer, and a shareable certificate with a verifiable ID once you pass a course.

Start quiz

Support free AI education. AI Understanding is a 501(c)(3) nonprofit — no ads, no paywall, ever. Make a donation

Frequently asked questions

What is BM25 and Lexical Retrieval?

BM25 is the classic keyword-based ranking function that scores documents by how often query terms appear, adjusted for term rarity and document length. Decades old, it remains a remarkably strong and ubiquitous baseline for search.

What does BM25 primarily use to rank documents?

BM25 combines term frequency, inverse document frequency (term rarity), and document-length normalization into a relevance score.

What role does inverse document frequency (IDF) play in BM25?

IDF rewards terms that are rare across the collection and down-weights common words, since rare matches are more informative.

Why does BM25 apply document-length normalization (the b parameter)?

Without normalization, longer documents would accumulate more term matches; the b parameter adjusts for length so comparisons are fair.

What data structure makes BM25 fast at scale?

An inverted index lets BM25 score only the documents that contain query terms, making retrieval very efficient.

In the standard BM25 formula, what effect does setting b to zero have?

b controls the strength of length normalization; zero removes that adjustment while other scoring components remain.

BM25 and Lexical Retrieval Guide | AI Understanding