BM25 (Best Matching 25)
Lexical ranking function for information retrieval. Finds exact word/phrase matches. Complements semantic embeddings in hybrid search systems.How it works
Extends TF-IDF (Term Frequency–Inverse Document Frequency):- TF-IDF: measures how important a word is to a document relative to a corpus — high frequency in doc, low frequency across corpus = high score
- BM25 refinements: applies a saturation function to term frequency (prevents high-frequency common words from dominating) and normalizes for document length
Where embeddings fail, BM25 succeeds
Semantic embeddings generalize — useful for meaning-based retrieval but lose specificity. BM25 finds exact strings:- Error codes:
"TS-999","HTTP 429" - Proper names, product names, version numbers
- Technical identifiers without semantic neighbors
Where BM25 fails, embeddings succeed
BM25 requires lexical overlap. Paraphrase, synonyms, cross-lingual queries, and conceptual similarity without shared vocabulary all evade BM25.In hybrid search
Standard pattern: retrieve top-K via BM25, retrieve top-K via embeddings, combine with rank fusion (e.g., Reciprocal Rank Fusion), deduplicate. Best of both retrieval modes. Used in: qmd, Contextual Retrieval, most production RAG pipelines.Related
- Contextual Retrieval — adds document context to BM25 index entries to reduce context-stripping failures
- qmd — local search engine using BM25 + vector hybrid