Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome to your next lesson on building robust Retrieval-Augmented Generation (RAG) systems.
In our previous lesson, you learned how to use metadata filtering to improve retrieval precision. By applying filters to structured attributes like dates, categories, or authors, you can dramatically narrow the search space and ensure results meet specific, hard constraints. This is a powerful technique for dealing with structured data associated with your documents.
Today, we will tackle another critical aspect of retrieval enhancement. The learning outcome for this lesson is to apply hybrid search to combine keyword-based and vector-based retrieval. While metadata filtering leverages structured data, hybrid search improves how we search the unstructured text content itself. You will learn why relying solely on semantic search can be a limitation and how combining it with traditional keyword search gives you the best of both worlds.
This lesson will cover:
- The distinct strengths and weaknesses of keyword and vector search.
- The scenarios where one outperforms the other.
- How to combine their outputs using score fusion techniques.
- A practical implementation using LangChain's
EnsembleRetriever.
Let's start by visualizing the overall process.

The Limits of a Single Search Paradigm
You've already seen the power of vector search. It excels at understanding user intent, synonyms, and paraphrasing. A query for "ways to make my code run faster" can find documents about "performance optimization" even if the exact words don't appear.
However, vector search can sometimes be too "fuzzy." It can struggle with queries where specific, literal words are crucial. Consider these examples:
- A search for a specific error code like
err_connection_refused. - Finding a document mentioning a unique product SKU, like
XJ-442. - Looking for text that contains a specific function name or acronym, like
GPTorBM25.
In these cases, the semantic meaning is less important than the literal presence of the keyword. A traditional keyword search engine would find these instantly, while a vector search might get distracted by semantically similar but incorrect results.
This is the core motivation for hybrid search: combining the precision of keyword search with the semantic understanding of vector search.
To get a clear mental model of these concepts, let's watch a video that breaks them down with excellent real-world examples.
Your RAG Won’t Work Without This: Vector vs Keyword vs Hybrid Search
This video from AI with Lena Hall provides a fantastic explanation of keyword, vector, and hybrid search, highlighting when to use each.
Watch from 01:06 to 04:36. Pay close attention to: The fundamental differences between keyword (literal) and vector (semantic) search. The three scenarios (product catalog, support inbox, e-commerce) that illustrate the strengths of each approach. The 'three-question litmus test' for systematically choosing the right search strategy.
As the video explains, the choice is not always about one being better than the other, but about using the right tool for the job. Hybrid search allows you to use both tools at once.
How Hybrid Search Works: Score Fusion
So, if we run a keyword search and a vector search, we get two separate lists of results, each with its own scoring system. How do we combine them into a single, ranked list? This process is called score fusion.
There are several ways to do this, but two common methods are:
- Weighted Combination (Alpha Blending)
- Reciprocal Rank Fusion (RRF)
1. Weighted Combination
This is the most intuitive approach. You assign a weight to each search type, typically using a parameter called alpha (α), and compute a final score.
The formula is straightforward:
- When , the search is purely vector-based.
- When , the search is purely keyword-based.
- A value like gives equal weight to both.
The norm subscript is important: the scores from the keyword search (e.g., BM25) and vector search (e.g., cosine similarity) are on different scales. Before combining them, they must be normalized (e.g., scaled to a 0-1 range).
You can tune based on your use case. If you expect queries with many specific keywords, you might decrease (e.g., to 0.3) to favor the keyword results. If you expect more natural language questions, you might increase it (e.g., to 0.7).
2. Reciprocal Rank Fusion (RRF)
RRF is a more robust technique that doesn't require score normalization. It combines results based purely on their rank in each list. The RRF score for a document is calculated by summing the reciprocal of its rank from each result list.
- is the rank of document
din listi. kis a constant (commonly 60) that dampens the influence of documents with very high ranks.
The intuition is simple: documents that consistently appear at the top of multiple result lists are more likely to be relevant. RRF is often a strong default choice because it effectively merges rankings without needing to worry about the different scales of the underlying scores.
Implementing Hybrid Search with LangChain
As a software engineer, you'll appreciate that modern frameworks abstract away much of this complexity. LangChain provides an EnsembleRetriever that makes implementing hybrid search straightforward. It handles fetching results from multiple retrievers and fusing them.
Let's watch a practical demonstration of how to set this up.
Advanced RAG 03 - Hybrid Search BM25 & Ensembles
This video by Sam Witteveen demonstrates how to build a hybrid search retriever in LangChain by combining a sparse (BM25) retriever and a dense (vector) retriever.
Watch from the beginning to 06:20. The video walks you through the key steps: Introducing hybrid search and the BM25 algorithm for keyword search. Instantiating a sparse BM25Retriever. Instantiating a standard dense FAISS vector store retriever. Combining them with the EnsembleRetriever and setting the weights parameter (which corresponds to alpha).
To complement the video, here is a detailed article that provides a complete, runnable code example. It uses ChromaDB, which you've encountered before, and clearly shows the performance difference between semantic and hybrid search.
Optimizing RAG with Hybrid Search & Reranking
This article from Superlinked offers a great textual walkthrough of implementing hybrid search. It's a useful reference to solidify the concepts from the video and see the full code.
Read the sections 'Implementation Example' and the query comparisons that follow. Specifically, focus on: How the BM25Retriever and vectorstore_retriever are created. How they are combined in the EnsembleRetriever with a weights list. The compelling examples showing how hybrid search provides much more specific and accurate answers for queries involving names ('Joe Biden') and specific terms ('Strait of Hormuz').
The key takeaway from the implementation is its simplicity within a framework. The main components are:
- A keyword retriever, most commonly
BM25Retriever. BM25 is a mature and highly effective algorithm that improves upon the classic TF-IDF. - A vector store retriever, which you are already familiar with.
- An ensemble component that takes the two retrievers and a weighting factor.
# A conceptual example based on the resources
from langchain.retrievers import BM25Retriever, EnsembleRetriever
from langchain.vectorstores import Chroma
# Assume 'chunks' and 'embeddings' are already defined
# 1. Initialize the keyword retriever
bm25_retriever = BM25Retriever.from_documents(chunks)
bm25_retriever.k = 3
# 2. Initialize the vector store retriever
vectorstore = Chroma.from_documents(chunks, embeddings)
vector_retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
# 3. Initialize the EnsembleRetriever
ensemble_retriever = EnsembleRetriever(
retrievers=[bm25_retriever, vector_retriever],
weights=[0.5, 0.5] # Corresponds to 1-alpha and alpha
)
Common Pitfalls to Avoid
As you integrate hybrid search, it's helpful to be aware of common mistakes. The first video you watched has an excellent summary of these.
Your RAG Won’t Work Without This: Vector vs Keyword vs Hybrid Search
Let's revisit the video from AI with Lena Hall for a final, crucial segment on common mistakes and a recap.
Watch from 11:05 to 13:31. This section covers critical mistakes to avoid, such as: Replacing keyword search entirely instead of augmenting it. Embedding entire documents as single chunks. Not pre-filtering with metadata when possible to reduce the search space.
The last point is particularly relevant: metadata filtering (our previous lesson) and hybrid search are not mutually exclusive. A highly effective strategy is to first apply a metadata filter to narrow the search space, and then run a hybrid search on the remaining documents. This is called candidate pruning and is a key optimization for production systems.
Conclusion
In this lesson, you've added another essential tool to your RAG optimization toolkit. By combining the strengths of keyword and vector search, you can build a retriever that is both semantically aware and precise with keywords.
Key Takeaways:
- Vector search excels at semantic understanding but can miss critical keywords, names, and acronyms.
- Keyword search (like BM25) excels at precision but lacks contextual understanding.
- Hybrid search combines both, fusing their results to get the best of both worlds.
- Score fusion is the process of combining the results, commonly done via a weighted sum (using an
alphaparameter) or Reciprocal Rank Fusion (RRF). - Frameworks like LangChain make implementation simple with components like
BM25RetrieverandEnsembleRetriever. - For optimal performance, combine hybrid search with metadata filtering to prune the candidate set before searching.
You now have two powerful methods to improve retrieval: metadata filtering for structured constraints and hybrid search for unstructured content.
In our next lesson, we will address the final step in the retrieval process: using a re-ranking model to improve the relevance of retrieved documents. Even with filtering and hybrid search, your top-k results might not be perfectly ordered. A re-ranker is a specialized model designed to take this list and re-order it for maximum relevance before passing it to the generator.