$ SELECT name FROM products ORDER BY embedding <=> :query LIMIT 3;
Vector Databases
A vector database stores embeddings, lists of numbers (typically 384 to 3,072 of them) that a machine-learning model produces for a piece of text, an image or a product, and answers one question fast: which stored vectors are closest to this one? Items with similar meaning end up close together, so “closest vectors” means “most similar items”. This powers semantic search, recommendations, deduplication and retrieval-augmented generation (RAG), where an LLM is given the most relevant document chunks before it answers.
How similarity search works
$ search('waterproof trail shoe', k=3)
Nearest-neighbour search, exact and approximate
- An embedding model turns every product into a vector. Similar meanings land near each other: footwear here, kitchen items there.
- The search text is embedded with the same model and becomes the query vector.
- Exact search compares the query with every stored vector. Correct, but O(N): fine for 100k vectors, too slow for 100 million.
- The k nearest vectors are the answer, ranked by similarity. This is what you feed to a RAG prompt or show as "similar items".
- An ANN index such as HNSW links each vector to a few neighbours. Search starts at an entry point and greedily hops closer, touching a tiny fraction of the data and finding the true top-k most of the time.
Key concepts
- Distance metric. Cosine similarity (angle between vectors) is the default for text embeddings; dot product and Euclidean (L2) are also common. Use the metric your embedding model was trained for.
- Exact vs approximate (ANN). Approximate nearest-neighbour indexes trade a little recall (say 95 to 99 % of the true top-k) for queries that are orders of magnitude faster.
- Index types. HNSW (layered proximity graph; fast, memory-hungry), IVF (cluster vectors, search only the nearest clusters), PQ / quantization (compress vectors to save memory), DiskANN (graph index that lives on SSD).
- Filtering. Real queries combine similarity with conditions:
category = 'shoes' AND in_stock. How well an engine handles filtered search differs a lot between products. - Hybrid search. Combine vector similarity with keyword search (BM25) and merge the rankings. Usually beats either alone for search over documents.
Popular options
| Option | Type | Notes |
|---|---|---|
| pgvector | PostgreSQL extension | HNSW and IVFFlat indexes inside Postgres, with joins and transactions. The pragmatic default up to tens of millions of vectors. |
| Qdrant | Open-source server (Rust) | Strong filtering, quantization, easy to self-host. |
| Milvus | Open-source, distributed | Built for billions of vectors; many index types; managed as Zilliz Cloud. |
| Weaviate | Open-source server | Built-in embedding modules and hybrid search. |
| Pinecone | Managed service | Serverless, no infrastructure to run. |
| Elasticsearch / OpenSearch, Cassandra 5, MongoDB Atlas, Redis | Vector search added to existing systems | Useful when the data already lives there. |
For data engineers
A vector store is fed by a pipeline: extract documents, chunk them, embed each chunk, upsert vectors with metadata (source id, timestamp, permissions). The hard parts are operational:
- Changing the embedding model means re-embedding everything. Vectors from different models are not comparable. Store the model name and version with each vector.
- Freshness and deletes. When a source document changes or is deleted, its chunks must be updated or removed, or the RAG system will cite stale data.
- Memory. 100 million 1,024-dimension float32 vectors are about 400 GB before index overhead. Quantization helps.
Common mistakes
- Adding a dedicated vector database for a few hundred thousand vectors that pgvector would handle.
- Embedding queries and documents with different models, or forgetting to normalize vectors when the metric assumes it.
- Judging quality by eye instead of measuring recall and relevance on a labeled set.
Learning Resources
- Efficient and robust approximate nearest neighbor search using HNSW graphs: Malkov and Yashunin, the HNSW paper
- Pinecone Learn: vector database guides
- pgvector README
- Qdrant documentation and Milvus documentation
- ANN-Benchmarks: recall vs speed for ANN algorithms
- A fun and absurd introduction to Vector Databases: video