Hybrid search: why keywords still matter next to vectors
Vector search finds what people mean and misses what they typed. Keyword search does the opposite. How to run both, merge the results with reciprocal rank fusion, rerank the top, and check that it actually helped.
Almost every "chat with your documents" system starts the same way: chunk the documents, embed the chunks, embed the question, return the nearest neighbours. It demos beautifully. Then someone pastes in an error code, or a part number, or a colleague's surname, and the answer comes back confidently about something else.
Nothing is broken. That's just what vector search does. The fix isn't a better embedding model. It's adding back the old-fashioned search you skipped, and learning how to combine the two.
Two kinds of search, two kinds of blind spot
Keyword search (usually BM25) scores a chunk by the words it shares with the query. Rare words count for more than common ones, and repeating a word helps less each time. It knows nothing about meaning. "Car" and "vehicle" are as different to it as "car" and "banana".
Vector search turns the query and every chunk into embeddings and returns the chunks whose embeddings are closest. It's very good at meaning and paraphrase. It's surprisingly bad at the exact, specific strings people often type.
| Query | Keyword search | Vector search |
|---|---|---|
ERR_CONN_RESET 0x80072efe | Finds the exact page | Returns general pages about network errors |
| "How do I get my money back?" | Misses the page titled "Refund policy" | Finds it |
INV-2024-08813 | Exact match | Returns other invoice numbers that look similar |
| "Laptop won't turn on" | Needs those words on the page | Finds "device fails to power up" |
| A person's surname | Finds every mention | Often finds people with similar names |
| A question in Arabic over English docs | Nothing | Works with a multilingual model |
The pattern is consistent. Vectors win when people describe something in their own words. Keywords win when people type the exact thing: identifiers, codes, names, quoted phrases, and terms that were rare or didn't exist when the embedding model was trained. Real users do both, often in the same query.
Why not just pick the better score?
The obvious move is to run both searches and add up the scores. That doesn't work, because the scores aren't measured in the same units.
- A BM25 score has no upper limit. It depends on query length, document length and how rare the words are. A score of 14 can be excellent for one query and weak for another.
- A cosine similarity sits between −1 and 1, and most embedding models squeeze real results into a narrow band, say 0.72 to 0.86.
You could normalise each list to 0–1 and take a weighted sum. That works, but the weights are fragile: they depend on the score distributions, which change with the query, the corpus and every time you swap the embedding model.
Reciprocal rank fusion: ignore the scores, use the positions
Reciprocal rank fusion (RRF) sidesteps the whole problem. It throws away the scores and looks only at where each result ranked in each list:
score(d) = Σ over each list 1 / (k + rank of d in that list)k is a constant, usually 60, that stops the top position from dominating. A document ranked
first in one list and missing from the other scores 1/61. A document ranked fifth in both scores
2/65, which is higher. Showing up near the top of both lists beats winning one of them, and
that's almost always what you want.
def rrf(*ranked_lists: list[str], k: int = 60, top: int = 10) -> list[str]:
scores: dict[str, float] = {}
for ranked in ranked_lists:
for rank, doc_id in enumerate(ranked, start=1):
scores[doc_id] = scores.get(doc_id, 0.0) + 1 / (k + rank)
return sorted(scores, key=scores.get, reverse=True)[:top]
keyword = ["refund-policy", "invoice-faq", "pricing"]
vector = ["returns-guide", "refund-policy", "shipping"]
print(rrf(keyword, vector))
# ['refund-policy', 'returns-guide', 'invoice-faq', 'pricing', 'shipping']That's the whole algorithm. It has one setting, it needs no training data, and it doesn't care which embedding model you use. It's the default in most search engines that support hybrid queries for good reason. Start with it, and only move to weighted scores if your evaluation shows a clear win.
It probably fits in the database you already have
You don't need a separate search engine to do this. Postgres can do both halves: full-text
search with tsvector, and vector search with the pgvector extension. The fusion is one query.
with keyword as (
select id, row_number() over (order by ts_rank_cd(tsv, q) desc) as rank
from chunks, websearch_to_tsquery('english', :query) as q
where tsv @@ q
order by rank
limit 50
),
vector as (
select id, row_number() over (order by embedding <=> :query_embedding) as rank
from chunks
order by embedding <=> :query_embedding
limit 50
)
select id,
coalesce(1.0 / (60 + keyword.rank), 0) +
coalesce(1.0 / (60 + vector.rank), 0) as score
from keyword
full outer join vector using (id)
order by score desc
limit 10;Two notes. Postgres's ts_rank_cd isn't true BM25; for most corpora it's close enough to start,
and there are extensions if you need the real thing. And the full outer join matters: a
document found by only one search still gets a score.
Elasticsearch, OpenSearch, Vespa, Weaviate, Qdrant and most other search engines now offer hybrid queries with RRF built in. Use theirs if you're already on one.
Then rerank the top
Fusion gives you a good shortlist. A reranker makes the order right.
Both searches above compare a query and a chunk that were turned into numbers separately. A cross-encoder reranker reads the query and the chunk together, as one input, and outputs a single relevance score. That's far more accurate, because the model can see exactly how the two relate. It's also far slower, which is why you only run it on the shortlist.
query ─┬─ keyword search → top 50 ─┐
└─ vector search → top 50 ─┴─ RRF → top 50 → reranker → top 8 → modelRetrieve wide and cheap, then narrow down with something slow and accurate. The numbers are starting points: 50 candidates from each search, rerank the fused top 30 to 50, pass the best 5 to 10 to the model. Measure, then adjust.
Things that bite in production
- Tokenisation decides what keyword search can find. An English stemmer will happily split
INV-2024-08813into pieces or drop the hyphens. Keep a second field that indexes identifiers exactly, or the queries that most need keyword search won't get it. - Both searches must apply the same permissions. It's easy to add a permission filter to the vector query and forget the keyword one, and now the fused results leak. Filter inside each search, never after fusion. I've written more about this in retrieval that respects permissions.
- Filters before the limit, not after. If you take the top 50 and then filter by tenant or date, you can end up with three results. Push filters into each query.
- Short queries behave differently. A one-word query gives vector search very little meaning to work with. Hybrid helps most on exactly these.
- Chunks need enough words. A chunk that's just a table of numbers has almost nothing for BM25 to match on. Adding the section heading and document title to each chunk at index time helps both searches. There's more on chunking in inside a knowledge base.
Prove it helped
Hybrid search is usually better, but "usually" isn't a reason to ship. Before switching, build a small labelled set: a hundred or two real queries, each with the chunks that should come back. Make sure it includes the awkward ones: identifiers, names, short queries, misspellings.
Then compare vector-only, keyword-only and hybrid on two numbers:
- Recall@k: of the chunks that should come back, how many are in the top k? This tells you whether the right answer reaches the model at all.
- MRR (mean reciprocal rank): how high does the first right answer appear? This tells you whether reranking is earning its cost.
Look at the queries that got worse as well as the average. A change that helps on average and breaks every part-number lookup is a change your support team will notice on day one. The same labelled set becomes a regression test for every future change to chunking, embeddings or ranking, which is the idea behind evals are the product.
The short answer
Vectors find what people mean. Keywords find what they typed. Run both, fuse by rank, rerank the top.
It's one extra query, one small function and one model call on a short list. For most knowledge bases, that's the cheapest real improvement in answer quality available.