Hybrid Search Explained: Combining BM25 Lexical Search, Dense Vectors, and RRF
Dense vector search excels at paraphrases and multilingual concepts, yet it routinely fails on exact identifiers: error code 131049, SKU ARC-204-BLK, legal clause numbers, or proper nouns not seen frequently in pretraining. Lexical search (BM25 / full-text search) nails exact matches but misses synonyms. Hybrid search combines both signals so neither blind spot reaches users.
Why Score Normalization Is Tricky
BM25 produces unbounded positive scores that depend on document length and inverse document frequency, while cosine similarity lives in [-1, 1]. Adding raw scores together lets whichever retriever has larger numeric variance dominate.
| Fusion Method | How It Works | Pros | Cons |
|---|---|---|---|
| Linear Weighted Sum | alpha * norm(dense) + (1 - alpha) * norm(bm25) | Tunable preference toward semantic or exact matches | Sensitive to score outliers and distribution drift |
| Reciprocal Rank Fusion (RRF) | Sums 1 / (k + rank) across retrievers | Scale-free, zero training required, robust defaults | Ignores raw score margins between rank 1 and rank 2 |
| Learned Reranker Fusion | Union top-K from both retrievers, score with cross-encoder | Highest final precision | Adds 40 to 150ms inference latency |
Implementing Reciprocal Rank Fusion (RRF)
With k = 60 (the classic constant from Cormack et al.), a document appearing near the top of both lexical and vector results receives a decisive boost, while a strong #1 exact keyword match still surfaces even if vector search ranked it lower.
export interface ScoredCandidate {
id: string;
score: number;
}
export function reciprocalRankFusion(
rankedLists: ScoredCandidate[][],
k = 60
): ScoredCandidate[] {
const fused = new Map<string, number>();
for (const list of rankedLists) {
list.forEach((item, index) => {
const rank = index + 1;
const contribution = 1 / (k + rank);
fused.set(item.id, (fused.get(item.id) ?? 0) + contribution);
});
}
return Array.from(fused.entries())
.map(([id, score]) => ({ id, score }))
.sort((a, b) => b.score - a.score);
}
Practical Full-Text Tips for Multilingual Knowledge Bases
- Use language-aware stemming or
simpleconfiguration alongside language-specific dictionaries so brand names, SKUs, and mixed-language queries (for example English API terms inside Russian prose) still tokenize cleanly. - Weight
titleandheadingshigher than body text (such assetweight(..., 'A')vs'C') in full-text vectors so canonical guide pages beat incidental mentions. - Run lexical and vector queries concurrently with
Promise.allso hybrid retrieval adds near-zero wall-clock latency compared to single-channel search.
Next: see how rewriting ambiguous user messages boosts both retrievers in Query Transformation in RAG.