Database / pgvector basics Interview Questions
How do you combine pgvector with full-text search (hybrid keyword + semantic search)?
Combining vector semantic search with keyword full-text search (BM25/tsvector) produces better results than either alone. This hybrid search pattern handles both cases: queries that need exact keyword matches and queries that need semantic understanding.
-- Hybrid search: combine semantic similarity with full-text ranking -- using Reciprocal Rank Fusion (RRF) to merge the two ranked lists CREATE TABLE documents ( id BIGSERIAL PRIMARY KEY, content TEXT NOT NULL, embedding VECTOR(1536), content_ts TSVECTOR GENERATED ALWAYS AS (to_tsvector('english', content)) STORED -- auto-updated FTS index ); CREATE INDEX ON documents USING gin(content_ts); -- FTS index CREATE INDEX ON documents USING hnsw(embedding vector_cosine_ops); -- vector index -- Reciprocal Rank Fusion hybrid search: WITH semantic AS ( SELECT id, ROW_NUMBER() OVER (ORDER BY embedding <=> '[...]') AS rank FROM documents ORDER BY embedding <=> '[...]' LIMIT 50 ), keyword AS ( SELECT id, ROW_NUMBER() OVER (ORDER BY ts_rank(content_ts, query) DESC) AS rank, ts_rank(content_ts, query) AS ts_score FROM documents, to_tsquery('english', 'pgvector & PostgreSQL') AS query WHERE content_ts @@ query LIMIT 50 ), fused AS ( SELECT COALESCE(s.id, k.id) AS id, COALESCE(1.0 / (60 + s.rank), 0) + COALESCE(1.0 / (60 + k.rank), 0) AS rrf_score FROM semantic s FULL OUTER JOIN keyword k USING (id) ) SELECT d.id, d.content, f.rrf_score FROM fused f JOIN documents d ON d.id = f.id ORDER BY f.rrf_score DESC LIMIT 10;
Why hybrid search outperforms either alone: semantic search handles paraphrasing and concept matching but can miss exact technical terms; keyword search is precise for exact terms but cannot understand synonyms. RRF merges both ranked lists without needing to tune a mixing weight parameter.
More Related questions...