Principle:0xranx OpenContext Hybrid Search
| Knowledge Sources | |
|---|---|
| Domains | Information_Retrieval, Search, NLP |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
A search strategy that combines vector (semantic) similarity and keyword (BM25) matching using Reciprocal Rank Fusion to produce a unified ranking that leverages the strengths of both approaches.
Description
Hybrid search addresses the fundamental trade-off between semantic understanding and exact term matching. Pure vector search excels at finding conceptually related content but may miss exact keyword matches, while pure keyword search (BM25) is excellent for precise term matching but fails to capture semantic relationships.
The approach uses Reciprocal Rank Fusion (RRF) to merge the ranked results from both search modes. RRF is a simple, parameter-free method that combines multiple ranked lists by computing a weighted sum of reciprocal ranks:
Where is a document, is the set of ranked result lists, is the rank of document in list , is the weight for list , and is a constant (typically 60).
The key advantage of RRF over score-based fusion is that it does not require normalizing scores from different retrieval systems, which may have incomparable score distributions.
Usage
Use this principle when building a search system that needs to find both semantically related content and exact keyword matches. It is particularly effective for knowledge base search where users may query with both natural language questions (benefiting from semantic search) and specific technical terms (benefiting from keyword search). The hybrid approach should be preferred over either vector-only or keyword-only search when both types of queries are expected.
Theoretical Basis
BM25 Keyword Search
BM25 (Best Matching 25) is a probabilistic ranking function that estimates the relevance of a document to a given query:
Where is the term frequency of in document , is the document length, is the average document length, and are tuning parameters (typically , ).
Vector (Semantic) Search
Vector search generates an embedding for the query using a neural model and finds the nearest neighbors in a pre-computed vector space. The similarity is typically computed using cosine similarity or dot product.
Reciprocal Rank Fusion
RRF merges the two ranked lists without needing score normalization:
Pseudo-code Logic:
# Abstract algorithm (NOT real implementation)
def rrf_fusion(vector_results, keyword_results, vector_weight=0.7, keyword_weight=0.3, k=60):
scores = {}
for rank, hit in enumerate(vector_results):
scores[hit.id] = scores.get(hit.id, 0) + vector_weight / (k + rank + 1)
for rank, hit in enumerate(keyword_results):
scores[hit.id] = scores.get(hit.id, 0) + keyword_weight / (k + rank + 1)
return sorted(scores.items(), key=lambda x: -x[1])
The weights (vector: 0.7, keyword: 0.3) reflect the empirically observed advantage of semantic search for natural language queries, while still giving meaningful weight to exact keyword matches.
Practical Guide
When implementing hybrid search:
- Index Preparation: Build both a vector index (embeddings stored in a vector database) and a keyword index (tokenized chunks with BM25 statistics) from the same document corpus.
- Query Processing: For each query, generate both a query embedding (for vector search) and tokenize the query (for keyword search) simultaneously.
- Result Fusion: Run both searches independently, then merge using RRF with configurable weights.
- Aggregation: Results can be aggregated by content (individual chunks), by document (deduplicating chunks from the same doc), or by folder (grouping by parent folder).