Hybrid Search and Rerank
Vector search alone often fails to find contract numbers, product model numbers and proper nouns. Hybrid search + rerank is standard for production RAG.
Core concepts
| Method | Good at | Bad at |
|---|---|---|
| Vector search (semantic) | Synonyms, questions phrased differently | Exact IDs, code, rare proper nouns |
| Keyword search (BM25 / full text) | Exact words, IDs, terminology | Paraphrases |
| Rerank (cross-encoder reranking) | Precise ranking of a few candidates, high accuracy | Slow, can't run over the whole corpus |
Typical pipeline: vector top-50 + keyword top-50 → fusion → rerank to top-5 → LLM.
How it works
RRF (Reciprocal Rank Fusion)
The two retrievers score on different scales (cosine similarity vs BM25 score), so adding them directly is meaningless. RRF uses only the rank:
RRF_score(d) = Σ 1 / (k + rank_i(d)) k is usually 60- From the 2009 SIGIR paper by Cormack, Clarke and Buettcher; the official pgvector README also recommends RRF or a cross-encoder for combining hybrid search results.
- Advantages: no normalization, no weight tuning, simple to implement.
Bi-encoder vs Cross-encoder
- Bi-encoder (embedding retrieval): the question and the document are encoded separately, so document vectors can be precomputed, which makes it fast.
- Cross-encoder (rerank model): "question + document" go into the model together for scoring, so it sees the interaction between them. It's more accurate but must be computed online pair by pair, so it is only used on a small set of candidates.
In practice: RRF in PostgreSQL
sql
WITH semantic AS (
SELECT id, ROW_NUMBER() OVER (ORDER BY embedding <=> $1) AS rnk
FROM chunk ORDER BY embedding <=> $1 LIMIT 50
),
keyword AS (
SELECT id, ROW_NUMBER() OVER (ORDER BY ts_rank_cd(tsv, q) DESC) AS rnk
FROM chunk, plainto_tsquery('simple', $2) q
WHERE tsv @@ q ORDER BY ts_rank_cd(tsv, q) DESC LIMIT 50
)
SELECT id, SUM(1.0 / (60 + rnk)) AS score
FROM (SELECT * FROM semantic UNION ALL SELECT * FROM keyword) t
GROUP BY id ORDER BY score DESC LIMIT 20;Then pass these 20 to a rerank model (such as Cohere Rerank, Voyage rerank or the open-source bge-reranker) and keep the top 5.
Common pitfalls
- Too many rerank candidates: latency grows linearly; keep it to a few dozen at most.
- Tuning fusion without looking at parsing: poor retrieval is usually a chunking and parsing problem.
- No evaluation: without recall numbers you can't tell whether adding rerank is worth it (see RAG Evaluation).
Common interview questions
- Why do you need hybrid search? Give an example where pure vector search fails.
- What is the RRF formula and what are its advantages? What does k=60 mean?
- What's the difference between a bi-encoder and a cross-encoder? Why can rerank only be used on a few candidates?
- How much latency do hybrid search + rerank add? How do you weigh that?
How it relates to my projects
- AI Document Review Pipeline W4: the SQL above can be used directly for searching the rule library.
Sources
- Cormack, Clarke, Buettcher · Reciprocal Rank Fusion outperforms Condorcet and individual Rank Learning Methods (SIGIR 2009) — the original RRF paper
- pgvector README: Hybrid Search — recommends RRF or a cross-encoder to combine results
- Anthropic: Contextual Retrieval — experiments combining BM25 + embeddings + rerank
- Sentence Transformers Docs: Cross-Encoders — the difference between bi-encoders and cross-encoders
