Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:1ay1 Agentty Hybrid Retrieval

From Leeroopedia
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

hybrid(q)=fuse(BM25(q,chunks),HNSW(embed(q),k))

# 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]

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment