Hybrid Search for RAG: BM25 + Vector Search + Re-ranking

Why pure vector search misses exact matches, how hybrid search combines BM25 and embeddings with reciprocal rank fusion, and how re-rankers improve precision...

Published December 1, 2026

Vector search finds passages with similar meaning, which is great for natural-language questions. It is surprisingly bad at exact things: error codes, product SKUs, function names, people's names. Keyword search (BM25) is the opposite. Hybrid search runs both and merges the results, and a re-ranker then picks the best few. Together they form the retrieval stack most production RAG systems end up with.

The concept

BM25 is a classic ranking function used by search engines like Elasticsearch, OpenSearch and PostgreSQL full-text search (with some differences). It scores documents by how often query terms appear, weighted by how rare those terms are across the corpus. It excels at precise lexical matches and needs no model.

Results from the two retrievers are merged, commonly with Reciprocal Rank Fusion (RRF), which adds 1/(k + rank) from each list and avoids comparing incompatible score scales. A cross-encoder re-ranker then reads the query and each candidate together and scores relevance much more accurately than embeddings alone, at higher cost, so it is applied only to the top 20-100 candidates.

The hybrid pipeline

Query"ERR_SSL_PROTOCOL_ERROR nginx"Hybrid retrieverrun bothBM25exact term matchesVectorsemantic matchesRRF mergecombined top 50Re-rankerbest 5 to the LLM
Two retrievers, one fused list, then precise re-ranking.

Who wins which query

Query typeBM25VectorHybrid
"How do I reset my password?"GoodGreatGreat
"Error E1042 on checkout"GreatWeakGreat
"function parseInvoiceDate"GreatWeakGreat
"cancel but keep my data"WeakGreatGreat

Typical gains

Vector onlynDCG@10 0.64BM25 only0.58Hybrid (RRF)0.71Hybrid + re-ranker0.79
Illustrative retrieval quality on a mixed technical corpus.

Practical examples

Reciprocal Rank Fusion

Merge ranked lists without normalising scores.

def rrf(*ranked_lists, k=60):
    scores = {}
    for results in ranked_lists:
        for rank, doc_id in enumerate(results, start=1):
            scores[doc_id] = scores.get(doc_id, 0) + 1 / (k + rank)
    return sorted(scores, key=scores.get, reverse=True)

fused = rrf(bm25_ids, vector_ids)[:50]

Hybrid search inside PostgreSQL

Full-text search and pgvector in one database.

WITH kw AS (
  SELECT id, row_number() OVER (ORDER BY ts_rank(tsv, q) DESC) AS r
  FROM chunks, websearch_to_tsquery('english', $1) q WHERE tsv @@ q LIMIT 50
), vec AS (
  SELECT id, row_number() OVER (ORDER BY embedding <=> $2) AS r
  FROM chunks ORDER BY embedding <=> $2 LIMIT 50
)
SELECT id, SUM(1.0 / (60 + r)) AS score
FROM (SELECT * FROM kw UNION ALL SELECT * FROM vec) s
GROUP BY id ORDER BY score DESC LIMIT 20;

Re-rank with a cross-encoder

Open-source cross-encoders run locally; hosted re-rank APIs also exist.

from sentence_transformers import CrossEncoder
reranker = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
scores = reranker.predict([(query, c.text) for c in candidates])
top = [c for _, c in sorted(zip(scores, candidates), key=lambda x: -x[0])][:5]

Common mistakes

  • Relying on vector search alone for technical content full of identifiers.
  • Adding raw BM25 and cosine scores together on different scales.
  • Re-ranking thousands of candidates and blowing the latency budget.
  • Not filtering by metadata (tenant, product, date) before ranking.

Key takeaways

  • Vector search finds meaning; BM25 finds exact terms. You usually need both.
  • Merge with Reciprocal Rank Fusion.
  • Re-rank the top candidates with a cross-encoder for precision.
  • Measure each stage on an evaluation set.

FAQ

Do I need Elasticsearch for hybrid search?

No. PostgreSQL full-text plus pgvector, or vector databases with built-in sparse/keyword support, can do hybrid search.

What is a sparse embedding?

A learned keyword-style vector (for example SPLADE) that captures term importance, an alternative to BM25 in some vector databases.

How much latency does re-ranking add?

Typically tens to a few hundred milliseconds for 20-100 candidates, depending on the model and hardware.

More AI articles