Мазмұнға өту
Мақала Kazakh тілінде әлі жарияланбаған. English нұсқасы көрсетілуде.

Hybrid Search Explained: Combining BM25 Lexical Search, Dense Vectors, and RRF

2 мин оқу

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 MethodHow It WorksProsCons
Linear Weighted Sumalpha * norm(dense) + (1 - alpha) * norm(bm25)Tunable preference toward semantic or exact matchesSensitive to score outliers and distribution drift
Reciprocal Rank Fusion (RRF)Sums 1 / (k + rank) across retrieversScale-free, zero training required, robust defaultsIgnores raw score margins between rank 1 and rank 2
Learned Reranker FusionUnion top-K from both retrievers, score with cross-encoderHighest final precisionAdds 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.

ts
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 simple configuration alongside language-specific dictionaries so brand names, SKUs, and mixed-language queries (for example English API terms inside Russian prose) still tokenize cleanly.
  • Weight title and headings higher than body text (such as setweight(..., 'A') vs 'C') in full-text vectors so canonical guide pages beat incidental mentions.
  • Run lexical and vector queries concurrently with Promise.all so 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.

Бұл пайдалы болды ма?