lesson 4 of 8 · rag
Hybrid search.
Keyword search finds an exact code or name. Vector search finds a meaning in other words. Hybrid search runs both and merges the rankings.
new words
- Keyword search
- A ranking by the words shared with the question, weighted by how rare each word is. It finds INV-2041 and a person's name; it misses a synonym.
- Vector search
- A ranking by cosine between the question's embedding and each chunk's. AI basics, lesson 5.
- Rank fusion
- One way to merge two rankings: each chunk scores 1 divided by 60 plus its rank, in each list, added up.
the lesson
The two searches fail in opposite places. A question in other words finds nothing by keyword, as lesson 4 of AI basics showed. A code, an invoice number or a surname is blurred by vectors: the embedding of "INV-2041" lands near every other invoice. So run both, and merge.
The merge needs no common scale. Rank fusion takes only each chunk's rank in each list:
score = 1 / (60 + rank in the keyword list)
+ 1 / (60 + rank in the vector list)
chunk A: 1st by keyword, 5th by vector
1/61 + 1/65 = 0.0164 + 0.0154 = 0.0318
chunk B: 2nd by keyword, 2nd by vector
1/62 + 1/62 = 0.0161 + 0.0161 = 0.0323
B wins: it is good in both lists. A chunk missing from one list gets only its other term. The 60 keeps a first place from drowning everything below it; the ranking, not the number, is what matters.
| The question holds | Found by |
|---|---|
| An exact code, a name, a number | Keyword search |
| A meaning, in any words | Vector search |
| Both: "the notice period in the Acme contract" | Hybrid, with the code narrowing and the meaning ranking |
try it
Type this and press Enter, with a page you know:
Find the page about <thing> twice: once with the exact words of its title, once with other words that mean the same. Say which search found it and why.
you should see
- The exact words find it. The other words find it only when they appear on the page.
- That gap is the vector side of hybrid search; the brain's search is the keyword side.