Principle:1ay1 Agentty Hybrid Retrieval
| Knowledge Sources | |
|---|---|
| Domains | Information_Retrieval, RAG |
| Last Updated | 2026-10-28 12:00 GMT |
Overview
Retrieval strategy that fuses sparse keyword matching (BM25) with dense semantic vector search (HNSW) to combine exact lexical matching with semantic similarity, exploiting the complementary strengths of each signal.
Description
Keyword (sparse) retrieval like BM25 excels at exact term matching: a search for `ThreadLog::append` finds the function even if the surrounding text is about persistence. Dense (semantic) retrieval via HNSW finds conceptually related content: a search for "saving conversations" finds the append-only log even without the keyword.
Each alone misses half: BM25 fails when the query uses different words than the document; dense retrieval fails on rare identifiers and code tokens that don't appear in the embedding model's training distribution. Hybrid retrieval runs both and fuses the ranked lists, typically via reciprocal rank fusion or score normalization, so a result that ranks well in either signal surfaces.
Usage
Use this principle when:
- Retrieving code and documentation where both exact identifiers and semantic concepts matter
- A single retrieval signal leaves relevant results buried
- Query patterns vary from "exact function name" to "conceptual description"
Theoretical Basis
# Abstract algorithm (NOT real implementation)
def hybrid_retrieve(query, index, k=20):
# Sparse: BM25 over tokenized chunks
bm25_results = bm25.search(query, k)
# Dense: HNSW ANN over embedding
embedding = embed(query)
hnsw_results = hnsw.search(embedding, k)
# Fusion: reciprocal rank fusion
fused = reciprocal_rank_fusion(bm25_results, hnsw_results)
return fused[:k]