How do you implement hybrid search combining keyword and vector retrieval in RAG?
Hybrid search combines BM25 keyword matching with vector similarity search for better retrieval. Rationale: vector search excels at semantic understanding (finding conceptually related content) but can miss exact keyword matches; BM25 excels at exact term matching but misses semantic relationships. Together they cover both cases. Implementation with PostgreSQL: use pgvector for vector similarity search and pgtrgm or tsvector/tsquery for full-text search. Query both indexes and merge results using Reciprocal Rank Fusion (RRF): for each document, calculate 1/(k + rankbm25) + 1/(k + rankvector) where k is typically 60, then sort by combined score. Alternative fusion: weighted linear combination (alpha vectorscore + (1-alpha) bm25score) with alpha tuned on evaluation data, typically 0.5-0.7 favoring vector search. Implementation with dedicated search: Weaviate and Qdrant natively support hybrid search, combining their vector index with BM25. Pinecone supports sparse-dense hybrid via sparse vector representation of BM25 scores. Advanced hybrid: implement query routing that detects query type (keyword-heavy queries like error codes route primarily to BM25, conceptual questions route primarily to vector search), use learned sparse representations (SPLADE) instead of traditional BM25 for better term weighting, and apply query expansion to generate related terms before keyword search. Evaluation: compare pure vector, pure keyword, and hybrid retrieval on your evaluation set. Hybrid typically improves recall@10 by 10-20% over either method alone. Monitor the contribution of each method and adjust fusion weights over time.