Prompt
How do I add vector search to a RAG app without breaking keyword search?
Latest observation
A good way to add vector search to a RAG app without losing keyword search is to use a hybrid retrieval design rather than replacing your current search.
Recommended approach: hybrid search
Use both:
- Keyword search for exact matches, names, IDs, rare terms, and precision
- Vector search for semantic similarity, paraphrases, and concept matching
Then combine the results with a rank fusion or weighted scoring step.
Practical architecture
1) Keep your existing keyword index
If you already have:
- Elasticsearch / OpenSearch
- Postgres full-text search
- Lucene / Solr / Meilisearch
don’t remove it. It still handles:
- Exact product names
- Error codes
- Acronyms
- Rare proper nouns
- Compliance/legal phrases
2) Add embeddings and a vector index
For each document chunk:
- Generate an embedding
- Store it alongside the chunk text and metadata
Use:
- pgvector
- Pinecone
- Weaviate
- Milvus
- Elasticsearch/OpenSearch vector fields
- FAISS for local prototypes
3) Retrieve from both systems
For each query:
- Run keyword retrieval
- Run vector retrieval
Then merge results.
Best ways to merge results
Option A: Reciprocal Rank Fusion (RRF)
This is often the safest default.
It works by giving higher scores to docs that appear near the top in either list.
Why it’s good:
- Simple
- Robust
- Doesn’t require score normalization
- Preserves strong keyword hits
Option B: Weighted score combination
Combine normalized keyword and vector scores.
Example:
- Final score =
0.6 * keyword_score + 0.4 * vector_score
Useful when you have well-calibrated scores, but harder to tune.
Option C: Two-stage retrieval
- Use keyword + vector to get top 50–200 candidates
- Re-rank with a cross-encoder or LLM-based reranker
This usually gives the best quality, while preserving keyword precision.
A safe default pipeline
A strong practical setup is:
- Keyword search top 20
- Vector search top 20
- Merge with RRF
- Optional rerank top 20
- Send top chunks to the LLM
This ensures keyword results still surface even if vector search is noisy.
Important design tips
Preserve exact-match boosting
Boost documents when they contain:
- Query terms in title
- Exact phrase matches
- Rare tokens
- IDs / codes / names
This prevents vector search from burying precise results.
Chunk carefully
Too-large chunks hurt both keyword and vector search. A common sweet spot:
- 200–500 tokens per chunk
- Some overlap
Store metadata for filtering
Use filters like:
- document type
- date
- tenant/user permissions
- source
- language
Filtering before ranking improves both retrieval methods.
Normalize scores if combining
Keyword scores and vector similarity scores are usually on different scales. If you combine them directly, one can dominate. Use:
- min-max normalization
- z-score normalization
- or just RRF to avoid the issue
If you already have Elastic/OpenSearch
You can often do hybrid search in one engine:
- BM25 for keyword retrieval
- kNN/vector search for embeddings
Then combine with built-in hybrid ranking or your own fusion.
This is often the easiest migration path.
Common mistake to avoid
Don’t replace keyword search with vector search alone.
Why not:
- Vector search can miss exact strings
- It can return semantically related but wrong documents
- It struggles with short, numeric, or highly specific queries
Keyword search remains essential in RAG systems.
Simple implementation pattern
Pseudo-flow:
keyword_results = keyword_search(query, top_k=20)
vector_results = vector_search(query_embedding, top_k=20)
combined = reciprocal_rank_fusion(keyword_results, vector_results)
reranked = rerank(query, combined[:20])
context = build_context(reranked[:5])
answer = llm.generate(query, context)
Rule of thumb
If your queries are a mix of:
- exact identifiers
- natural language questions
- domain jargon
then hybrid retrieval is almost always the right choice.
If you want, I can also give you:
- a reference architecture,
- a Postgres/pgvector implementation, or
- an Elasticsearch/OpenSearch hybrid search example.