A user asks an AI assistant: "Why does my FastAPI application return a 504 Gateway Timeout?"
A semantic search system might retrieve documents discussing slow APIs, request timeouts, and network latency. But it could miss a document containing the exact error code 504.
A keyword search system might find the exact error code but overlook a highly relevant document describing the same problem with different terminology.
This is a common retrieval problem in AI applications. Neither keyword search nor vector search is sufficient for every query.
Hybrid search combines both approaches to retrieve information using exact keyword matches and semantic similarity. It is particularly useful in Retrieval-Augmented Generation (RAG), where the quality of retrieved context directly affects the answers generated by an LLM.
1. Why Keyword Search and Vector Search Have Different Strengths
Before understanding hybrid search, it helps to look at the two retrieval methods separately.
Keyword Search with BM25
BM25 is a probabilistic ranking algorithm commonly used in information retrieval systems.
It ranks documents based on factors such as:
- How frequently query terms appear in a document.
- How rare those terms are across the document collection.
- How long the document is.
For example, consider a search for AWS Lambda cold start.
BM25 can prioritise documents containing those exact terms, especially when they are relatively uncommon in the collection.
This makes it useful for technical searches involving product names, error codes, API identifiers, configuration values, and specific terminology.
However, keyword search depends on matching terms. A document explaining how to reduce serverless function startup latency might be relevant even if it does not contain the exact phrase "cold start".
BM25 may not rank it as highly as a document containing the original keywords.
Dense Retrieval with Vector Search
Dense retrieval uses embedding models to convert queries and documents into numerical vectors.
The system compares these vectors to find semantically similar content.
For example, these queries communicate similar ideas:
- How can I reduce API response time?
- Ways to improve application latency.
- Why is my backend responding slowly?
Even though the wording differs, an embedding model may represent these queries in similar regions of its vector space.
This allows dense retrieval to find relevant documents without requiring exact keyword matches.
But vector search has its own limitations. It may retrieve conceptually related documents while overlooking an exact error code, product identifier, or technical symbol that is essential to the question.
This is why combining both methods can produce better retrieval results.
2. What Is Hybrid Search?
Hybrid search runs multiple retrieval methods against the same collection and combines their results into a unified ranking.
In a typical RAG application, the two primary methods are:
BM25 retrieval: Searches documents using lexical keyword relevance.
Dense retrieval: Searches document embeddings using vector similarity.
Both methods run for the same user query, often in parallel.
The system then merges their candidate results using a ranking strategy such as Reciprocal Rank Fusion (RRF) or score-based fusion.
The result is a list that benefits from both exact term matching and semantic understanding.
3. How Hybrid Search Works in a RAG Pipeline
Consider a company knowledge base containing API documentation, troubleshooting guides, and internal engineering articles.
An employee asks:
"How do I fix a connection reset error when calling the payment API?"
A hybrid retrieval pipeline works as follows:
Step 1: Process the query
The application prepares the original query for two retrieval paths.
The keyword search uses the query text, while the dense retrieval path converts it into an embedding.
Step 2: Run BM25 retrieval
The keyword search looks for relevant terms such as connection reset and payment API.
It can identify documents containing those technical terms and related occurrences.
Step 3: Run dense retrieval
The embedding model converts the question into a dense vector.
The vector search retrieves documents discussing similar concepts, even when they use different language, such as network interruptions or failed payment requests.
Step 4: Combine the results
The application merges the BM25 and vector results into one candidate list.
A document discovered by both methods may receive a stronger combined position.
Step 5: Rerank the candidates
An optional reranking model evaluates the retrieved documents against the original question and reorders them according to their relevance.
Step 6: Send context to the LLM
The highest-ranked passages are provided to the language model, which generates an answer using the retrieved information.
This separation is important: retrieval identifies potential evidence, ranking selects the most useful evidence, and the LLM generates the response.
4. How Hybrid Ranking Works: Reciprocal Rank Fusion
One challenge with combining BM25 and vector search is that their relevance scores are not directly comparable.
BM25 might return a score of 12.4, while vector similarity might return 0.87. These values represent different scoring systems.
Simply adding them together can produce an unreliable ranking.
A common solution is Reciprocal Rank Fusion (RRF).
Instead of combining the original scores, RRF combines the positions of documents in multiple ranked lists.
Its formula is:
[ RRF(d) = \sum_{r \in R(d)} \frac{1}{k + rank_r(d)} ]
Here:
drepresents a document.rank_r(d)represents its position in a retrieval result list.kis a constant that controls how much ranking differences affect the final score.R(d)represents the result lists containing that document.
Consider this simplified example:
| Document | BM25 rank | Vector rank |
|---|---|---|
| Document A | 1 | 3 |
| Document B | 4 | 1 |
| Document C | 2 | 2 |
| Document D | 3 | Not found |
RRF rewards documents that rank well across multiple retrieval methods.
Document C performs consistently in both lists, while A and B each perform particularly well in one of them.
This allows the system to combine different relevance signals without requiring their original scores to share the same scale.
RRF is a useful starting point, although weighted score-based fusion can be preferable when score distributions have been calibrated and their differences carry useful information.
5. Why Reranking Improves Hybrid Search
Hybrid search improves candidate retrieval, but its initial ranking is not always the final ranking you want.
A document may be returned because it contains the correct keywords or has a similar embedding, even though it does not directly answer the user's question.
This is where reranking comes in.
A reranker takes the original query and a smaller list of retrieved documents, evaluates their relevance more carefully, and produces a new ranking.
For example:
- Initial retrieval returns 50 candidate passages.
- A reranking model evaluates those candidates against the original question.
- The system selects the most relevant passages for the LLM.
Cross-encoder models are commonly used for this task. Unlike a typical bi-encoder retrieval system, which embeds the query and documents separately, a cross-encoder evaluates the query and candidate text together.
This can provide a more detailed relevance signal, but it requires more computation.
Therefore, reranking is usually applied after initial retrieval rather than against the entire document collection.
An important limitation remains: reranking cannot recover a relevant document that the initial retrieval stages failed to retrieve.
It improves the ordering of candidates, not the coverage of the entire collection.
6. When Should You Use Hybrid Search?
Hybrid search is especially useful when an application handles different kinds of queries.
Consider these scenarios:
Technical documentation: Users search for API names, error codes, configuration keys, and conceptual explanations.
Enterprise knowledge bases: Employees use natural language to find policies, internal documents, and specific business terminology.
E-commerce search: Users describe products using broad concepts but may also search for exact product names or model numbers.
Code search: Developers need to locate functions, class names, error messages, and code examples while also finding conceptually related implementations.
Pure vector search may be sufficient for some semantic retrieval tasks, while BM25 may work well for exact terminology. Hybrid search is valuable when both types of relevance matter.
However, it introduces additional retrieval work, infrastructure, and ranking configuration. It should be justified by improvements in retrieval quality rather than added automatically to every application.
7. How to Evaluate a Hybrid Retrieval System
A hybrid pipeline should be measured against real queries, not just assumed to be better because it combines two methods.
Start by creating a representative evaluation dataset containing exact keyword searches, paraphrased questions, rare technical terms, error codes, and natural-language requests.
Then compare BM25, dense retrieval, and hybrid retrieval using metrics such as:
- Recall@K: Measures whether relevant documents appear within the top K results.
- Precision@K: Measures how many retrieved results in the top K are relevant.
- MRR: Rewards systems that place the first relevant result higher.
- nDCG@K: Evaluates ranking quality when documents have different levels of relevance.
Also measure latency and retrieval cost. A more accurate system may still be unsuitable if it adds too much response time.
Test reranking separately to establish whether its additional computation meaningfully improves the final results.
Conclusion
Keyword search and vector search solve different retrieval problems.
BM25 helps locate exact terminology, while dense retrieval finds information based on semantic similarity. Hybrid search combines their results, and reranking improves the ordering of the retrieved candidates.
For production RAG systems, this creates a more flexible retrieval pipeline than relying on either method alone.
The goal of hybrid search is not to retrieve more documents. It is to make sure the right documents reach the LLM.



