Course contentsShow
← AI Engineering
Lesson 381 of 1,88610. RAG - Improving RetrievalFree lesson

Hybrid Search: Combining Dense and Sparse Retrieval

Learn to combine semantic (dense) and keyword (sparse) search for better recall across different query types.

Hybrid Search: Combining Dense and Sparse Retrieval

What you'll learn: How to blend semantic vector search with traditional keyword search to handle both conceptual and exact-match queries effectively.

The Core Idea

You've learned about dense retrieval (vector embeddings for semantic search) and sparse methods (keyword/BM25 search). But what if a user asks "What's the capital of France?" (exact keyword match works great) versus "Tell me about French government seats" (semantic understanding shines)?

Hybrid search combines both approaches, running your query through dense and sparse retrieval pipelines simultaneously, then merging the results with smart ranking.

How It Works

Think of it like searching your memory and checking your notes:

  1. Dense path: Convert query to embedding, find semantically similar chunks
  2. Sparse path: Use keyword matching (BM25) to find exact term overlaps
  3. Fusion: Merge both result sets using Reciprocal Rank Fusion (RRF) or weighted scoring
# Conceptual flow (no real API calls yet)
dense_results = vector_db.similarity_search(query_embedding, k=20)
sparse_results = bm25_index.search(query_keywords, k=20)

# Combine with weights
hybrid_results = merge_and_rank(
    dense_results, 
    sparse_results, 
    dense_weight=0.7, 
    sparse_weight=0.3
)

Why It Matters for RAG

Some queries need exact product names, error codes, or technical terms — sparse excels here. Others need concept matching across paraphrased content — dense wins. Hybrid gives you both, dramatically improving retrieval recall across diverse query types.

Key Takeaway: Hybrid search combines the precision of keyword matching with the flexibility of semantic search, ensuring you catch relevant documents whether users speak literally or conceptually.