Hybrid Search Implementation (Vector + Full-Text) for RAG

We design and deploy artificial intelligence systems: from prototype to production-ready solutions. Our team combines expertise in machine learning, data engineering and MLOps to make AI work not in the lab, but in real business.
Showing 1 of 1All 1566 services
Hybrid Search Implementation (Vector + Full-Text) for RAG
Medium
from 1 week to 3 months
Frequently Asked Questions

AI Development Areas

AI Solution Development Stages

Latest works

  • image_website-b2b-advance_0.webp
    B2B ADVANCE company website development
    1317
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1226
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    925
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1156
  • image_logo-advance_0.webp
    B2B Advance company logo design
    620
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    894

When implementing RAG systems, a common dilemma is how to find a document both by meaning and by exact number. Hybrid search—a combination of vector (dense) and full-text (sparse/BM25) retrieval followed by result fusion—solves this problem. In practice, hybrid search consistently outperforms either method alone on most corporate datasets. For example, on one project hybrid search (RRF) improved MRR@5 by 12% relative to pure dense search while maintaining high recall for exact terms. We implement such solutions turnkey, with guaranteed retrieval quality on your data. Project cost typically ranges from $5,000 to $10,000 depending on dataset size and complexity. Request a consultation on hybrid search implementation and get a project assessment.

Why Dense Search Alone Is Not Enough

Dense embeddings average semantics—that's both a strength and a weakness. A query like "contract No. DA-2023-451" will have high cosine similarity with contracts in general, but not with the specific document by number. BM25 finds the exact string "DA-2023-451" instantly.

  • Dense search performs poorly for: exact numbers (contract, SKU, serial number), abbreviations and specific acronyms, rare technical terms, queries for exact quote search.
  • BM25 performs poorly for: paraphrased queries (synonyms), semantically similar concepts with different words, cross-lingual queries, vague descriptions ("something about payment after delivery").

Why Hybrid Search Is Better Than Dense or BM25 Alone

Combining the two approaches yields synergy: dense covers semantics, BM25 covers exact matches. The real-world case below shows that hybrid RRF (without reranker) outperforms dense+reranker in MRR@5 (0.83 vs 0.80) and NDCG@5 (0.81 vs 0.77). Meanwhile, hybrid+reranker achieves 0.89/0.87. In other words, hybrid search implementation achieves a balance between semantic similarity and exact keyword matching. According to our A/B test on 400 queries, hybrid RRF outperforms dense+reranker by a factor of 1.04 in MRR@5. For many tasks, this eliminates the need for an expensive reranker.

Fusion Algorithms

Reciprocal Rank Fusion (RRF)—the most robust method. RRF is a fusion method proposed by Cormack et al. (2009)—see more on Wikipedia.

RRF Code
from collections import defaultdict

def reciprocal_rank_fusion(
    dense_results: list[tuple],   # [(doc_id, score), ...]
    sparse_results: list[tuple],
    k: int = 60  # RRF constant (typically 60)
) -> list[tuple]:
    """
    RRF score = sum(1 / (k + rank_i)) across all lists
    k=60 standard value (Cormack et al.)
    """
    scores = defaultdict(float)

    for rank, (doc_id, _) in enumerate(dense_results, 1):
        scores[doc_id] += 1 / (k + rank)

    for rank, (doc_id, _) in enumerate(sparse_results, 1):
        scores[doc_id] += 1 / (k + rank)

    return sorted(scores.items(), key=lambda x: -x[1])

Relative Score Fusion (RSF)—normalized combination:

RSF Code
def relative_score_fusion(
    dense_results: list[tuple],
    sparse_results: list[tuple],
    alpha: float = 0.5  # Weight for dense
) -> list[tuple]:
    """Normalizes scores to [0,1] and weights them"""
    scores = defaultdict(float)

    # Normalize dense
    if dense_results:
        max_d = max(s for _, s in dense_results)
        min_d = min(s for _, s in dense_results)
        for doc_id, score in dense_results:
            norm = (score - min_d) / (max_d - min_d + 1e-8)
            scores[doc_id] += alpha * norm

    # Normalize sparse
    if sparse_results:
        max_s = max(s for _, s in sparse_results)
        min_s = min(s for _, s in sparse_results)
        for doc_id, score in sparse_results:
            norm = (score - min_s) / (max_s - min_s + 1e-8)
            scores[doc_id] += (1 - alpha) * norm

    return sorted(scores.items(), key=lambda x: -x[1])

Fusion Algorithm Comparison

Parameter RRF RSF
Principle Sum of inverse ranks Weighted sum of normalized scores
Sensitivity to scales Low (uses only rank) High (requires normalization)
Tuning One parameter k Parameter alpha
Robustness High Medium (depends on alpha)
Recommended k/alpha k=60 (empirical) alpha=0.5 (default)

SPLADE: Advanced Sparse Encoder

SPLADE (Sparse Lexical and Expansion Model) generates sparse vectors with lexical expansion—the model learns to "expand" the query with synonyms and related terms. According to the BEIR benchmark, SPLADE outperforms BM25 by 1.2–1.5 times in NDCG@10.

from fastembed import SparseTextEmbedding

sparse_model = SparseTextEmbedding(
    model_name="prithivida/Splade_PP_en_v1"
)

def encode_sparse(text: str) -> dict:
    """Returns sparse vector {token_id: weight}"""
    output = list(sparse_model.embed([text]))[0]
    return {
        "indices": output.indices.tolist(),
        "values": output.values.tolist(),
    }

SPLADE outperforms BM25 on most BEIR benchmarks. For Russian, we recommend the model naver/efficient-splade-VI-BT-large-query or multilingual variants.

Implementation with Qdrant (Practical Example)

from qdrant_client import QdrantClient
from qdrant_client.models import (
    SparseVector, Prefetch, FusionQuery, Fusion,
    NamedVector, NamedSparseVector
)
from fastembed import TextEmbedding, SparseTextEmbedding

dense_model = TextEmbedding("BAAI/bge-m3")  # Multilingual dense
sparse_model = SparseTextEmbedding("prithivida/Splade_PP_en_v1")
client = QdrantClient(url="http://localhost:6333")

def hybrid_search(query: str, top_k: int = 5) -> list[dict]:
    # Dense embedding
    dense_vec = list(dense_model.embed([query]))[0].tolist()

    # Sparse embedding
    sparse_output = list(sparse_model.embed([query]))[0]
    sparse_vec = SparseVector(
        indices=sparse_output.indices.tolist(),
        values=sparse_output.values.tolist()
    )

    results = client.query_points(
        collection_name="hybrid_docs",
        prefetch=[
            Prefetch(query=dense_vec, using="dense", limit=50),
            Prefetch(query=sparse_vec, using="sparse", limit=50),
        ],
        query=FusionQuery(fusion=Fusion.RRF),
        limit=top_k,
        with_payload=True,
    )

    return [
        {"text": r.payload["text"], "source": r.payload["source"], "score": r.score}
        for r in results.points
    ]

Practical Case: Alpha Impact on Retrieval Quality

From our practice: on a project with 12,000 corporate knowledge base documents (contracts, regulations, FAQs), we tested 400 queries of various types. Results:

Configuration MRR@5 NDCG@5 Exact Term Recall
Dense only (BGE-M3) 0.74 0.71 0.58
BM25 only 0.67 0.63 0.91
Hybrid RRF (k=60) 0.83 0.81 0.84
Hybrid RSF (α=0.6) 0.81 0.79 0.81
Dense + Reranker 0.80 0.77 0.61
Hybrid + Reranker 0.89 0.87 0.86

Hybrid RRF without reranker already beats dense+reranker. The combination hybrid+reranker yields the best result. For comparison, using SPLADE as a sparse encoder gives an MRR@5 improvement of about 0.03–0.05 over BM25 with the same fusion method.

How to Tune RRF Fusion on Your Dataset?

Optimal k for RRF: k=60 is an empirically robust value. Too small k (10–20) gives large weight to top positions. Too large (100+) levels out differences between positions. On real data, test k∈{20, 40, 60, 80} on a validation set. For RSF, tune alpha from 0.3 to 0.7 in steps of 0.1.

Step-by-Step Hybrid Search Implementation Process

  1. Audit current retrieval scheme: analyze used embeddings, vector DB stack, and quality metrics.
  2. Select and configure sparse encoder: install SPLADE or other sparse encoder suited to your language and domain.
  3. Integrate dual search: set up indexing of dense and sparse vectors in Qdrant/Pinecone/Weaviate.
  4. Implement fusion: deploy RRF or RSF with initial parameters (k=60, alpha=0.5).
  5. Test and optimize: run your queries, tune parameters using MRR/NDCG metrics.
  6. Document and hand over: describe the process, train the team, deliver code and configs.

What's Included in the Project

  • Integration code for hybrid search into your RAG system.
  • Configuration files for Qdrant/Pinecone.
  • Documentation on setup and operation.
  • Team training (2-hour webinar).
  • Retrieval quality guarantee (metrics measured before/after).
  • One month post-project support.

Contact us for a free project assessment. Get a consultation on hybrid search implementation and improve your RAG system's retrieval quality.