Vector search misses exact error codes. How do you fix retrieval?
Question
A support bot helps shop staff with card readers. A cashier asks: "What does error E1043 mean?"
Vector search returns these pages, best first: errors-overview, e1034-card-declined, e1043-pin-locked, pin-reset-guide. The bot answers from the top 2, so it explains E1034, a declined card.
- Why does vector search put the wrong code above the right one?
- A keyword search (BM25) for the same question returns
e1043-pin-locked,pin-reset-guide,e1043-release-notes. Merge the two lists with Reciprocal Rank Fusion, k = 60. What is the new order?
Show hints (3)
- "MEANING OR LETTERS"
An embedding stores what a text means. What does "E1043" mean, and how different does it look from "E1034"?
- "RANKS, NOT SCORES"
Each list gives a page a score of 1 / (60 + its rank), counting ranks from 1. Add the scores from both lists.
- "BOTH LISTS WIN"
e1043-pin-lockedis 1st in one list and 3rd in the other. Compare 1/61 + 1/63 with a page that tops only one list: 1/61.
Show checked explanation
Embeddings capture meaning, not exact characters, so E1043 and E1034 can land close together. Add keyword search (BM25), which matches the exact code, and run both. Merge the lists with RRF. Pages found by both searches rise: e1043-pin-locked comes first with 1/61 + 1/63 ≈ 0.0323.
keyword = ["e1043-pin-locked", "pin-reset-guide", "e1043-release-notes"] vector = ["errors-overview", "e1034-card-declined", "e1043-pin-locked", "pin-reset-guide"] def rrf(rankings, k=60): scores = {} for ranking in rankings: for rank, doc in enumerate(ranking, start=1): scores[doc] = scores.get(doc, 0) + 1 / (k + rank) return sorted(scores.items(), key=lambda item: (-item[1], item[0])) for doc, score in rrf([keyword, vector]): print(f"{score:.4f} {doc}")
0.0323 e1043-pin-locked 0.0318 pin-reset-guide 0.0164 errors-overview 0.0161 e1034-card-declined 0.0159 e1043-release-notes
Code run on CPython 3.12 against the example above. Checked by an editor other than the author.
Walkthrough
An embedding turns text into a vector that stores its meaning. "The card machine shows an error" lands close to "terminal fault", which most questions need. But a code like E1043 has almost no meaning, and E1034 differs only by two swapped digits. So the question's vector can sit closer to the wrong page.
Keyword search with BM25 works the other way. It scores pages by the words they share with the question, and a rare word counts much more than a common one. E1043 is on only a few pages, so the exact match wins. But keyword search misses paraphrases: "card machine" doesn't match a page that only says "terminal".
So run both and merge the results. You can't simply add their scores, because the scales differ. A BM25 score has no fixed range: it grows with each matching query word and depends on the collection. Cosine similarity stays between −1 and 1. Reciprocal Rank Fusion (Cormack, Clarke and Büttcher, SIGIR 2009) uses only the ranks. Each list adds 1 / (k + rank) to a page, ranks start at 1, and k = 60.
| page | keyword rank | vector rank | RRF score |
|---|---|---|---|
| e1043-pin-locked | 1 | 3 | 1/61 + 1/63 = 0.0323 |
| pin-reset-guide | 2 | 4 | 1/62 + 1/64 = 0.0318 |
| errors-overview | none | 1 | 1/61 = 0.0164 |
| e1034-card-declined | none | 2 | 1/62 = 0.0161 |
| e1043-release-notes | 3 | none | 1/63 = 0.0159 |
A page that both searches find beats a page that tops only one. A page missing from a list gets nothing from it. Now the top 2 are the right error page and the guide to fix it.
Why 60? The paper's authors chose it in a pilot study and found the exact value was not critical.
Common mistakes
- Adding the raw scores. The scales differ, so one search drowns the other. Fuse by rank, or normalise both scores first.
- Hoping a bigger embedding model will fix it. Matching exact codes, order numbers and product ids is what keyword search is for.
- Indexing "E-1043" but searching for "E1043". Normalise codes the same way when you index and when you search, or keyword search misses too.
- Fusing only the top 3 of each list. A page at rank 8 in both lists scores 2/68 = 0.0294, more than any page that tops one list (0.0164).
Follow-up questions
- "What does k change?" A small k lets top ranks dominate. With k = 1, rank 1 scores 1/2 and rank 10 scores 1/11: 5.5 times less. With k = 60 the ratio is only 70/61 ≈ 1.15, so agreement between lists counts more.
- "Where does a reranker fit?" After fusion. Take the top 20 fused pages and score each (question, page) pair with a cross-encoder, a model that reads both texts together. It is more accurate but much slower per pair, so it only sees the short list.
What a strong answer covers
Explains why embeddings blur exact codes and BM25 misses paraphrases. Fuses by rank, computes RRF with ranks from 1 and k = 60, and normalises codes the same way at index and query time.
Open in interactive practice deck →