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.

2 min readLast updated

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.

Technical Insight

BM25's term-frequency component saturates: the k1 parameter caps how much repeated words boost a score, so a term appearing 50 times is not 50x more relevant than once. The b parameter blends raw and length-normalized frequency. IDF down-weights common words like 'the' and rewards distinctive ones. Because it operates on an inverted index mapping each word to its document list, scoring touches only documents containing query terms, making it extremely efficient.

Strategic Impact

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.

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.

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

Risks & Guardrails

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.

Implementation Roadmap

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

Keep up with AI in 3 minutes a day

One short email each weekday with the three AI stories that actually matter. 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

Next guide

ColBERT Late Interaction Retrieval

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.