pgvector adds a vector data type and similarity search to PostgreSQL. Store embeddings alongside metadata in the same row. Two index types: IVFFlat (inverted file, approximate, good recall with proper lists count) and HNSW (hierarchical navigable small world, faster queries, more memory). Choose distance metric at index creation: cosine for normalised embeddings (text/images), L2 for unnormalised. Hybrid search combines vector similarity with BM25 full-text search via RRF (Reciprocal Rank Fusion).
Combining vector and full-text search via Reciprocal Rank Fusion.
HNSW has faster query performance and no need to retrain (IVFFlat must be rebuilt as data grows). Use HNSW for production RAG systems.
Vector search alone misses exact keyword matches. BM25 alone misses semantic similarity. RRF combination consistently outperforms either alone in RAG retrieval benchmarks.
Add WHERE tenant_id = :tid AND category = :cat before the ORDER BY <=> clause. Reduces candidate set, improves accuracy, and speeds up search on large collections.
Sign in to share your feedback and join the discussion.