Hybrid Search and Rank Fusion
Combine keyword and vector search with Reciprocal Rank Fusion and judge when hybrid search pays off.
Two searches are better than one
Keyword search (BM25) and vector search fail in opposite places. Keyword search matches exact words, so it misses paraphrases and questions asked in another language. Vector search matches meaning, so it can blur exact codes, names and numbers: to an embedding model, CS3101 and CS3102 look almost the same.
A student asking for the "syllabus for CS3101" needs the exact course code. A student asking "can I get my paper rechecked" needs a match on meaning, because the document says "re-evaluation of answer scripts". One search box receives both kinds of question all day.
Hybrid search runs both searches on every query and merges the two result lists. If either method finds the right passage, it has a good chance of reaching the top of the merged list. Search platforms such as Elasticsearch, OpenSearch and Azure AI Search offer it directly, and published evaluations generally find that it beats either method alone on mixed, real-world queries.
Both searches run on the same query, and a fusion step merges their rankings.
The merging problem
Merging sounds easy: add the two scores for each passage and sort. The trouble is that the scores are on completely different scales. BM25 scores might run from 0 to 25, while cosine similarities run from about 0.2 to 0.9.
Imagine two examiners marking the same answer scripts, one out of 100 and the other out of 10. If you add their marks, the second examiner's opinion barely counts. The program below shows the same thing happening to search scores.
Expected output:
21.40 CS3101 Syllabus
19.71 CS3101 Re-evaluation Notice
17.20 CS3101 Timetable
0.86 Re-evaluation of Answer Scripts
0.64 Exam Results
The top three places simply repeat the BM25 order, and the passage that vector search ranked first comes fourth. The vector scores were too small to matter. Rescaling both lists to the range 0 to 1 helps, but the result then depends on how spread out the scores happen to be for each query.
Reciprocal Rank Fusion
The fair way to combine the two examiners is to ignore their marks and use their ranks: who came first, second and third according to each. Reciprocal Rank Fusion (RRF) does exactly that. Each passage earns points from every list it appears in:
Here rank is the passage's position in that list (1 for first), and k is a constant, commonly 60. A passage that appears near the top of both lists collects points twice, so it rises above passages that only one method liked.
Worked example: RRF with k = 60
The CS3101 Re-evaluation Notice is ranked 2nd by keyword search and 2nd by vector search.
Its score is 1/(60 + 2) + 1/(60 + 2) = 0.0161 + 0.0161 = 0.0323.
The CS3101 Syllabus is ranked 1st by keyword search and does not appear in the vector list.
Its score is 1/(60 + 1) = 0.0164.
Second place in both lists beats first place in one.
The constant k controls how much the very top ranks matter. With k = 1, first place is worth one and a half times second place (1/2 against 1/3). With k = 60 they are worth almost the same (1/61 against 1/62), so appearing in both lists matters far more than topping one of them. RRF was described by Gordon Cormack and colleagues in 2009, and it remains popular because it needs no tuning of score scales and works with any number of lists.
Try it below. The query is "CS3101 re-evaluation fee". Keyword search is good at the course code; vector search is good at "re-evaluation".
RRF in code
RRF needs only the two ranked lists. No scores are passed in at all.
Expected output:
0.0323 CS3101 Re-evaluation Notice
0.0312 Fee Structure
0.0164 CS3101 Syllabus
0.0164 Re-evaluation of Answer Scripts
0.0159 CS3101 Timetable
0.0159 Exam Results
| Line | What it does |
|---|---|
enumerate(ranking, start=1) |
Walks down one list, giving each passage its rank: 1, 2, 3 and so on. |
scores.get(doc, 0) + 1 / (k + rank) |
Adds this list's points to whatever the passage has earned so far, starting from 0. |
sorted(..., key=lambda kv: -kv[1]) |
Sorts the passages by total points, highest first. |
The notice that mentions both the course code and re-evaluation was only second in each list, but it wins after fusion because both methods liked it. Fee Structure, fourth in both, comes second for the same reason.
Weighted fusion
Sometimes one method deserves more trust on your data. Weighted fusion multiplies each list's contribution by a weight, for example 0.7 for vector search and 0.3 for keywords. In the code above, that means replacing 1 / (k + rank) with weight / (k + rank). The weights should come from testing on your own questions.
A hybrid system needs both engines. Elasticsearch and OpenSearch include both. PostgreSQL can also supply both, with its built-in full-text search for keywords and pgvector for vectors, merged by a function like the one above.
When hybrid search is worth it
Hybrid search costs a second search per query. It pays off most when queries mix exact identifiers with natural language, which describes almost every college and company system: course codes, roll numbers, form numbers, product names, error messages. To decide for your own data, measure Recall@K on the same test questions with keyword search only, vector search only and hybrid, and let the numbers choose.