AI ENGINEERING · RETRIEVAL AND RAG IN DEPTH · RETRIEVAL · 23 OF 35

Hybrid Search and Reranking: BM25 + Vectors

Where vector search misses exact ids, error codes and rare names, and where keyword search misses synonyms. BM25 term frequency, rarity and length with a worked score, hybrid search merged by reciprocal rank fusion with a worked k=60 example, cross-encoder reranking from 50 to 5 and its latency, metadata filters, freshness boosts, tuning on a labelled set, and common mistakes.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
1 / 18

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Keywords for exact strings
  • vectors for meaning
  • fuse by rank
  • rerank 50 to 5
  • filter inside
  • measure

Notes

Where vectors fail

Start with the weakness of vectors. An embedding captures what text is about, not its exact characters. So error E forty twelve and error E forty twenty one land almost on top of each other, and both look like any error page. The same goes for order ids, part numbers, version strings, rare product names and in-house acronyms. The query has the one string that matters, and the embedding treats it as noise.

Vector search misses
  • exact ids: ORD-1182, SKU 88-310
  • error codes: E4012 vs E4021
  • rare names: a product or person the model never saw
  • numbers and versions: "v2.3" vs "v3.2"
  • acronyms only your team uses
  • all look "about the same"
  • to an embedding

Where keywords fail

Keyword search has the opposite problem. It matches words, so it nails E forty twelve, but it has no idea that stop my plan means cancel subscription. Users write in their own words, with synonyms, paraphrases, typos and sometimes another language. If the query and the right page share no terms, keyword search simply cannot see it. Each method fails exactly where the other one shines, and that is the whole argument for using both.

Keyword search misses
  • synonyms: "stop my plan" vs "cancel subscription"
  • paraphrases and questions written in user language
  • typos and other languages
  • it finds the right page only if the words overlap

The idea

So hybrid search runs both. The same query goes to a keyword index and a vector index at the same time. Each returns its own ranked top fifty. A fusion step merges the two lists into one, rewarding chunks that both methods like. Because the searches run in parallel, the extra latency is small, usually a few milliseconds. The cost is keeping a second index in sync with the first.

Hybrid search
  • send the same query to: keyword index (BM25) vector index (embeddings)
  • each returns its top ~50
  • merge into one list
  • that rewards agreement
  • cost: one extra index, searched in parallel

BM25 intuition

The classic keyword scorer is B M twenty five. Three ideas. First, term frequency. Saying reset three times beats saying it once, but the gain flattens fast. Second, rarity, called inverse document frequency. A word in very few documents tells you a lot, while common words like and or of count almost nothing. Third, length. A match in a short chunk counts more than the same match in a long one.

BM25 in three ideas
  • 1 term frequency: more hits help, but with diminishing returns (k1 ≈ 1.2)
  • 2 rarity (IDF): a word in few documents counts a lot; "the" counts almost nothing
  • 3 length: a hit in a short chunk beats a hit in a long one (b ≈ 0.75)
  • score = sum over query terms

A worked score

Score two chunks for the query reset E forty twelve, among a thousand chunks. Reset appears in a hundred of them, so its rarity weight is about two point three. E forty twelve appears in only two, so its weight is about six. Chunk A says reset three times and never mentions the code. That scores about three point six. Chunk B says each word once and scores about eight point three. The rare code decides the ranking, which is exactly what we want.

Query: "reset E4012"
1,000 chunks, length effect ignored
IDF reset (in 100)  ≈ 2.3
IDF E4012 (in 2)    ≈ 6.0
tf part, k1=1.2: tf·2.2/(tf+1.2)
  tf=1 → 1.00   tf=3 → 1.57

A: reset ×3, no E4012
   2.3 × 1.57         ≈ 3.6
B: reset ×1, E4012 ×1
   2.3×1 + 6.0×1      ≈ 8.3
B wins: rarity dominates

Why fuse by rank

Now, how to merge. You might add the two scores together, but they live on different scales. B M twenty five scores run from zero to twenty or more, while cosine scores sit between roughly zero point two and zero point nine. Add them and keywords always win. Reciprocal rank fusion ignores the raw scores and uses only positions. For each list a chunk appears in, add one divided by sixty plus its rank. Sixty is the usual constant.

Reciprocal rank fusion
BM25 scores:   0 to ~25, unbounded
cosine scores: ~0.2 to ~0.9
adding them = BM25 always wins

RRF ignores scores, uses ranks:
  score(d) = Σ 1 / (k + rank)
  k = 60 by convention

a doc missing from a list
adds nothing from that list

A worked RRF

Here is a small example. B M twenty five ranks D one, D two, D three, D four. Vector search ranks D three, D one, D five. D one gets one over sixty one plus one over sixty two, about zero point zero three two five. D three gets almost the same. D two, top two in only one list, gets half that. So the chunks both methods agree on rise to the top. The large k keeps rank one from dominating.

RRF with k = 60
BM25:   D1  D2  D3  D4
vector: D3  D1  D5

D1: 1/61 + 1/62  = 0.0325
D3: 1/63 + 1/61  = 0.0323
D2: 1/62         = 0.0161
D5: 1/63         = 0.0159
D4: 1/64         = 0.0156

fused: D1 D3 D2 D5 D4

Weighted fusion

R R F is the safe default because it needs no tuning. Two variations are common. Weighted R R F multiplies each list's contribution, so you might trust vectors at zero point seven and keywords at zero point three. Score fusion rescales each list to between zero and one and blends them with a weight. Both can beat plain R R F, but only when you have a labelled set that proves it. Otherwise a weight is a guess.

Other ways to merge
  • weighted RRF: give one list more say, e.g. 0.7 / 0.3
  • score fusion: min-max normalise each list to 0..1, then a × vector + (1 − a) × BM25
  • RRF: no tuning, robust default
  • weights: only if a labelled set proves they help

Reranking

Retrieval is fast because the chunk vectors were computed in advance, and the query never meets the chunk until a cosine at the end. That loses detail. A reranker, usually a cross encoder, reads the query and one chunk together as a single input, so every query word can attend to every chunk word. It outputs one relevance score. It is far more accurate, but nothing can be precomputed, so it costs a model pass for every pair.

Two kinds of model
  • bi-encoder (retrieval): query and chunk embedded separately, compared by cosine fast, chunks precomputed
  • cross-encoder (reranker): reads query + chunk as one input, outputs a relevance score; sees word-level detail slow: one pass per pair

Retrieve 50, keep 5

So we build a funnel. Hybrid search is cheap and casts a wide net, bringing back fifty candidates from millions in a few milliseconds. The reranker then scores just those fifty pairs and keeps the best five for the prompt. Ask can I get a refund after thirty days, and the actual refund policy chunk might sit at rank seven after fusion. The reranker reads it properly and moves it to rank one. Retrieval is tuned for recall, reranking for precision.

The funnel
  • millions of chunks → hybrid search, ~10 ms
  • 50 candidates → cross-encoder, 50 pairs
  • 5 chunks in the prompt
  • query: refund after 30 days? policy chunk was rank 7, reranker puts it at rank 1

Reranker latency

The price is latency. Scoring fifty pairs with a small cross encoder usually takes tens to a few hundred milliseconds, depending on hardware and chunk length. The cost grows with the number of candidates times their length, so rerank twenty to a hundred, not a thousand, truncate long chunks, and send the pairs in one batch. You can also ask a language model to rank the candidates. That is often better still, but slower and more expensive per query.

What it costs
  • ~50 pairs on a GPU: tens to a few hundred ms
  • grows linearly with the number of candidates and their token length
  • keep it fast: 20–100 candidates, truncate long chunks, small reranker, batch the pairs
  • LLM as reranker: better, slower

Filters and freshness

Two more tools. Metadata filters restrict results to the right tenant, product, language or permission level. Apply them inside both searches, not after. If you filter the top fifty afterwards, you might throw all fifty away. Freshness boosts help when newer content is usually right, like release notes. Multiply the score by a gentle decay. With a ninety day half life on part of the score, a six month old chunk keeps about seventy eight percent. Gentle, so relevance still wins.

Filters and boosts
  • filter: tenant, product, language, access rights apply inside the search; a post-filter on top 50 can leave zero results
  • freshness: score × decay e.g. 0.7 + 0.3 × 0.5^(age/90d) new doc: × 1.0 180 days old: × 0.775

Tune with a labelled set

None of this should be tuned by gut feel. Build a labelled set: a few hundred real user queries, each with the ids of the chunks that answer it. Then measure every setup. Keyword only, vector only, hybrid, hybrid plus rerank. Track recall at fifty before reranking, because the reranker cannot rescue a chunk that never arrived, and recall at five after it. Then tune one knob at a time. The final episode covers evaluation in depth.

Measure, then tune
100-300 real queries, each with
the ids of the chunks that answer it

setup (illustrative)  recall@5
  BM25 only            0.55
  vector only          0.62
  hybrid RRF           0.71
  hybrid + rerank      0.83

also: recall@50 before rerank,
MRR or nDCG for ordering

Numbers worth knowing

Some numbers worth knowing. B M twenty five defaults are about one point two for k one and zero point seven five for b. R R F uses k of sixty. Retrieve twenty to a hundred candidates per list and rerank down to three to ten. Hybrid search usually takes five to fifty milliseconds, reranking fifty pairs fifty to three hundred. On many corpora, hybrid adds roughly five to fifteen points of recall over vectors alone, and reranking adds a similar amount again.

Ballparks
BM25 k1 / b          ~1.2 / ~0.75
RRF k                60
candidates per list  ~20 - 100
rerank down to       ~3 - 10
hybrid search        ~5 - 50 ms
rerank 50 pairs      ~50 - 300 ms

recall gain, often:
  hybrid vs vector   +5-15 pts
  rerank on top      +5-15 pts

Common mistakes

The common mistakes. Adding raw B M twenty five and cosine scores. Shipping vectors only, then discovering users paste error codes. Retrieving only five and reranking them, which leaves nothing to reorder. A keyword tokenizer that splits E forty twelve or a part number into pieces, so exact matches stop working. Filtering after top k. Letting the keyword index fall out of sync with the vector index. And changing weights without a labelled set to tell you if it helped.

Common mistakes
  • adding raw BM25 + cosine
  • vectors only, then users paste error codes
  • reranking 5, not 50: nothing left to reorder
  • a tokenizer that splits "E4012" or "88-310" apart
  • filtering after top-k
  • keyword index out of sync
  • tuning without a labelled set

Say it like this

How to say it. We run hybrid retrieval. B M twenty five catches exact ids and error codes, vectors catch paraphrases, each returns fifty, and we merge them with reciprocal rank fusion with k of sixty. The tenant filter runs inside both searches. A cross encoder reranks fifty down to five in about a hundred milliseconds. On two hundred labelled queries, recall at five went from zero point six two to zero point eight three. Quoting the measurement is what convinces.

In the interview
  • "Hybrid retrieval: BM25 for exact ids and codes, vectors for paraphrases, each top 50, merged with RRF, k = 60. Tenant filter inside both. A cross-encoder reranks 50 to 5 in ~100 ms. Recall@5 went from 0.62 to 0.83 on 200 labelled queries."

Interview questions

Q1Why is pure vector search not enough for RAG?

Embeddings capture meaning, not exact strings, so error codes, ids, part numbers, version strings and rare names get blurred together. Keyword search matches them exactly, which is why production systems usually add a BM25 index alongside the vector index.

Q2How does BM25 score a document?

It sums, over the query terms, a rarity weight (inverse document frequency) times a saturating term-frequency factor, discounted for long documents. Repeated terms help with diminishing returns, rare terms count far more than common ones, and short documents are favoured.

Q3What is reciprocal rank fusion and why use it?

RRF merges ranked lists by giving each document the sum of 1 / (k + rank) over the lists it appears in, with k usually 60. It ignores raw scores, so it works even though BM25 and cosine scores live on different scales, and it needs no tuning.

Q4What is the difference between a bi-encoder and a cross-encoder?

A bi-encoder embeds the query and each document separately, so document vectors can be precomputed and compared by cosine at speed. A cross-encoder reads the query and one document together and outputs a relevance score; it is more accurate but needs a model pass per pair, so it is used only to rerank a short list.

Q5Why retrieve 50 and rerank to 5 instead of retrieving 5?

First-stage retrieval is tuned for recall and often puts the best chunk at rank 7 or 20. Retrieving a wider set gives the reranker something to reorder; it then picks the most precise few for the prompt. A reranker can never recover a chunk that was not retrieved.

Q6What does reranking cost and how do you keep it fast?

Latency grows with candidates times chunk length, typically tens to a few hundred milliseconds for about 50 pairs. Cap candidates at 20 to 100, truncate long chunks, batch the pairs and use a small reranker model.

Q7Should metadata filters run before or after the vector search?

Inside or before it. Filtering the top-k afterwards can discard every candidate and return nothing, or leak results across tenants if forgotten. Most vector databases and search engines support filtered search directly.

Q8How would you tune a hybrid retrieval pipeline?

Build a labelled set of real queries with the ids of the chunks that answer them, then compare BM25 only, vectors only, hybrid and hybrid plus rerank on recall at 50 and recall at 5 or nDCG. Change one knob at a time: candidate count, fusion weights, filters or freshness decay.