Vector databases store and search high-dimensional embeddings efficiently. The difference between a toy demo and a production system often comes down to the vector database: indexing strategy, filtering, multi-tenancy, and operational reliability.
ANN Algorithms: How Fast Search Works
Exact nearest neighbor search (comparing against every vector) is O(n) — too slow for millions of vectors. Approximate Nearest Neighbor (ANN) algorithms trade a tiny accuracy loss for dramatic speed gains.
- HNSW: Hierarchical Navigable Small World graph. The most popular. O(log n) search, 95-99% recall. Memory-intensive but fast.
- IVF: Inverted File Index. Clusters vectors, searches only relevant clusters. Less memory but lower recall.
- Flat: Exact search. Perfect recall but O(n). Use for small datasets only.
Qdrant: Production Vector Search
from qdrant_client import QdrantClient, models
client = QdrantClient(url="http://localhost:6333")
# Create collection with HNSW index
client.create_collection(
collection_name="documents",
vectors_config=models.VectorParams(size=384, distance=models.Distance.COSINE),
)
# Insert vectors with metadata
client.upsert(collection_name="documents", points=[
models.PointStruct(id=1, vector=embedding, payload={"title": "Doc 1", "category": "engineering"}),
])
# Search with metadata filtering
results = client.search(
collection_name="documents",
query_vector=query_embedding,
query_filter=models.Filter(must=[
models.FieldCondition(key="category", match=models.MatchValue(value="engineering"))
]),
limit=5,
)
pgvector: Vector Search in PostgreSQL
-- Enable extension
CREATE EXTENSION vector;
-- Create table with vector column
CREATE TABLE documents (
id SERIAL PRIMARY KEY,
content TEXT,
embedding vector(384)
);
-- HNSW index for fast search
CREATE INDEX ON documents USING hnsw (embedding vector_cosine_ops);
-- Search
SELECT id, content, embedding <=> query_vector AS distance
FROM documents ORDER BY distance LIMIT 5;