Hybrid Search: Keywords Plus Vectors
Combine BM25-style keyword search with semantic search so exact names, codes and rare terms are found along with paraphrases.
Intermediate · about 4 hours · difficulty 3/5
Learn first: RAG Fundamentals
Vector search is great at meaning and bad at exactness. Ask for "error E-4021" or a specific person's name and embeddings may return vaguely related chunks. Keyword search (BM25, the classic ranking algorithm behind search engines) nails exact matches but misses paraphrases. Use both.
How hybrid search works
- Run a keyword query and a vector query in parallel.
- Merge the two ranked lists. Reciprocal Rank Fusion (RRF) is the popular, tuning-free method: each result scores the sum of
1 / (k + rank)over the lists it appears in. - Optionally rerank the merged list (next topic).
Implementations
- Postgres:
tsvectorfull-text search plus pgvector, fused in SQL or in code. - Search engines: Elasticsearch, OpenSearch and Vespa support both natively.
- Vector databases like Qdrant and Weaviate offer hybrid modes, sometimes with sparse vectors.
Query-side improvements
- Metadata filters extracted from the question ("in 2024", "for the HR team").
- Query rewriting: use an LLM to turn a conversational follow-up ("what about for interns?") into a standalone query.
Good enough to move on
You can show a set of queries where vector-only search fails, keyword-only search fails, and hybrid gets both right.
Key concepts
- BM25 keyword search
- Reciprocal Rank Fusion
- Postgres full-text plus pgvector
- Sparse vectors (SPLADE-style)
- Metadata filter extraction
- Query rewriting for follow-ups
Mini project
Add BM25 or Postgres full-text search to your RAG app, fuse it with vector results using RRF, and build a 30-query comparison table of keyword, vector and hybrid hit rates.
Check yourself
Give an example query where semantic search is likely to fail but keyword search succeeds, and explain why.
Free resources
- Okapi BM25 (Wikipedia)
- Qdrant: hybrid queries (Qdrant)
- PostgreSQL full text search (postgresql.org)