Home Knowledge Base Hybrid Search: Combining BM25 and Dense Retrieval

Hybrid Search: Combining BM25 and Dense Retrieval

Retrieval Methods

Sparse Retrieval (BM25) Traditional keyword matching with term frequency weighting:

Dense Retrieval (Vector Search) Semantic similarity using embeddings:

Why Hybrid? Neither method is perfect alone. Hybrid search combines strengths:

Query TypeBM25DenseHybrid
Exact keywordStrongWeakStrong
Semantic conceptWeakStrongStrong
Rare termsStrongWeakStrong
SynonymsWeakStrongStrong

Hybrid Implementation

def hybrid_search(query: str, alpha: float = 0.5) -> list:
    # BM25 scores
    bm25_results = bm25_index.search(query, top_k=100)
    bm25_scores = normalize(bm25_results.scores)

    # Dense scores
    query_embedding = embed(query)
    dense_results = vector_store.search(query_embedding, top_k=100)
    dense_scores = normalize(dense_results.scores)

    # Combine scores
    combined = {}
    for doc_id, score in bm25_results:
        combined[doc_id] = alpha * score
    for doc_id, score in dense_results:
        combined[doc_id] = combined.get(doc_id, 0) + (1 - alpha) * score

    # Sort by combined score
    return sorted(combined.items(), key=lambda x: x[1], reverse=True)

Reciprocal Rank Fusion (RRF) Another combination method using ranks instead of scores:

def rrf_score(rank: int, k: int = 60) -> float:
    return 1 / (k + rank)

def rrf_fusion(results_list: list) -> dict:
    scores = {}
    for results in results_list:
        for rank, doc_id in enumerate(results):
            scores[doc_id] = scores.get(doc_id, 0) + rrf_score(rank)
    return scores

Tools with Hybrid Search

ToolHybrid Support
Elasticsearch 8+Native
WeaviateNative
QdrantBuilt-in sparse vectors
PineconeVia sparse-dense
VespaNative

Tuning Alpha

Query PatternRecommended Alpha
Keyword-heavy0.7 (more BM25)
Conversational0.3 (more dense)
Balanced0.5

Tune alpha on your specific dataset and query patterns.

hybrid searchbm25sparse dense

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.