Back to Roadmap
10:00

BM25 & Hybrid Retrieval

Combining lexical and semantic search for high-precision, high-recall retrieval in modern RAG systems

10 MIN READ VERIFIED CURRICULUM

BM25 and hybrid retrieval are core techniques in modern information retrieval systems, especially in RAG architectures where both keyword precision and semantic understanding are required.

As a RAG Architect, understanding BM25 and hybrid search is essential for building retrieval systems that are both accurate and robust across diverse query types.

Why Retrieval Matters in RAG

In Retrieval-Augmented Generation systems, retrieval quality directly determines the quality of LLM outputs.

Even the most powerful LLM cannot compensate for irrelevant or missing context.

What is BM25?

BM25 (Best Matching 25) is a probabilistic ranking function used in keyword-based search to estimate the relevance of documents to a query.

It improves upon TF-IDF by introducing term saturation and document length normalization.

Core Idea of BM25

BM25 scores documents based on term frequency, inverse document frequency, and document length normalization.

This ensures that repeated terms contribute less after a certain point (term saturation).

score(D, Q) = Σ IDF(qi) * (f(qi, D) * (k1 + 1)) / (f(qi, D) + k1 * (1 - b + b * |D| / avgDL))
text

Term Frequency (TF)

Term frequency measures how often a query term appears in a document.

BM25 reduces the impact of very high term frequencies using saturation.

Inverse Document Frequency (IDF)

IDF measures how rare a term is across the entire corpus.

Rare terms contribute more to relevance scoring than common ones.

Document Length Normalization

Longer documents are penalized to avoid bias toward verbose content.

This ensures fair comparison across documents of different sizes.

Strengths of BM25

BM25 is fast, interpretable, and highly effective for exact keyword matching tasks.

It performs well when queries contain specific terms that must appear in results.

Limitations of BM25

BM25 does not understand semantic meaning or synonyms.

It fails when queries use different wording than the relevant documents.

What is Hybrid Retrieval?

Hybrid retrieval combines BM25-based lexical search with embedding-based semantic search.

This approach leverages the strengths of both methods to improve overall retrieval quality.

Why Hybrid Search Works

Lexical search ensures precision for exact terms, while semantic search captures meaning and context.

Together, they improve both recall and precision.

Hybrid Retrieval Architectures

Common architectures include parallel retrieval (BM25 + vector search independently) followed by result fusion.

Another approach is sequential filtering, where one method narrows candidates for the other.

Score Fusion Techniques

Hybrid systems combine BM25 and semantic scores using weighted sums or rank-based fusion methods like Reciprocal Rank Fusion (RRF).

Proper weighting is critical for balancing lexical and semantic signals.

Reciprocal Rank Fusion (RRF)

RRF combines ranked lists by assigning higher scores to documents that appear near the top of multiple rankings.

It is robust and does not require score normalization.

Vector Search vs BM25

BM25 excels at keyword precision, while vector search excels at semantic similarity.

Hybrid systems use both to cover weaknesses of each approach.

Query Type Sensitivity

Some queries are better suited for lexical search (e.g., IDs, error codes), while others benefit from semantic search (e.g., conceptual questions).

Indexing Strategies

BM25 uses inverted indexes, while semantic search uses vector indexes.

Hybrid systems often maintain both indexes in parallel.

Performance Tradeoffs

Hybrid retrieval increases computational overhead but significantly improves retrieval quality.

Efficient caching and indexing are essential for scalability.

Evaluation Metrics

Metrics like recall@k, precision@k, MRR, and nDCG are used to evaluate retrieval performance.

Hybrid systems typically outperform standalone BM25 or vector search on these metrics.

Common Challenges

Challenges include score normalization, latency overhead, index synchronization, and tuning fusion weights.

Best Practices

Best practices include using RRF for stable fusion, maintaining both lexical and vector indexes, and tuning retrieval weights based on query logs.

Regular evaluation ensures balanced performance across query types.

Summary

BM25 and hybrid retrieval form the foundation of robust RAG systems by combining exact keyword matching with semantic understanding.

By integrating lexical and vector search, RAG architects can build retrieval systems that are both precise and context-aware.