Learn/LangChain Mastery/Part 5 · 4. Embeddings & Vector Stores
Lesson 37 of 68
SyllabusLangChain Mastery · Part 5

Hybrid Search Concepts

The best of both worlds. Reciprocal Rank Fusion.

Course outline & contents
LangChain Mastery
Embeddings: What Matters in PracticeEmbedding Models in LangChainVector Stores: FAISS, ChromaPersisting & Reloading IndexesMetadata FilteringDense vs Sparse (BM25 Overview)Hybrid Search Concepts

We saw that Dense Search (Vectors) and Sparse Search (Keywords) have opposite strengths. Hybrid Search runs both and combines the results.

  1. User Query: "Error 501 on login page."
  2. Dense Retriever: Finds docs about "Login Issues" (Concepts).
  3. Sparse Retriever: Finds docs containing "Error 501" (Exact Match).
  4. Ensemble: Combine the two lists.

How do you combine the lists? You can't just add the scores (Cosine Similarity score 0.8 is not comparable to BM25 score 15.0). We use Rank Fusion.

We look at the rank, not the score.

  • Doc A: Rank 1 in Dense, Rank 5 in Sparse.
  • Doc B: Rank 10 in Dense, Rank 1 in Sparse.

RRF gives points for being high on either list.

hybrid_search.py
from langchain.retrievers import EnsembleRetriever

ensemble_retriever = EnsembleRetriever(
  retrievers=[bm25_retriever, vector_retriever],
  weights=[0.5, 0.5]
)

docs = ensemble_retriever.get_relevant_documents("query")

Hybrid Search is the current state-of-the-art for production RAG. It provides the "Safety" of keywords with the "Magic" of vectors.

Key Intuition: "Don't put all your eggs in one index."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonDense vs Sparse (BM25 Overview)Next lesson →The Retriever Interface