Module 4 of 16

Vector Databases Engineering

ANN algorithms, indexing, metadata filtering, and production deployment with Qdrant and pgvector

4 hours2 labsFree

Start here

Learning objectives

  • Understand ANN algorithms (HNSW, IVF) and their tradeoffs
  • Deploy and operate Qdrant for production vector search
  • Use pgvector for PostgreSQL-integrated vector search
  • Design metadata filtering and multi-tenancy strategies
VECTOR DATABASE ARCHITECTUREQuery VectorHNSW IndexApproximate Nearest NeighborO(log n) searchMetadata Filtercategory, date, tenantTop-K ResultsVector Database OptionsQdrant (production)pgvector (PostgreSQL)Pinecone (managed)Weaviate (multi-modal)Milvus (billion-scale)

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;

Common mistakes

What usually breaks

  • Using ChromaDB in production (designed for prototyping, not operations)
  • Not creating HNSW indexes (falls back to brute-force search)
  • Storing vectors without metadata (cannot filter by category, tenant, date)
  • Not monitoring collection size and search latency as data grows

Tradeoffs

Design choices you should be able to defend

Qdrant

Pros

  • High performance
  • Rich filtering
  • Multi-tenant
  • Open source

Cons

  • Separate infrastructure
  • Ops overhead

pgvector

Pros

  • Reuses existing PostgreSQL
  • SQL interface
  • No new infrastructure

Cons

  • Lower performance at scale
  • Limited filtering
  • Not designed for billions of vectors

Key terms

Vocabulary used in this module

ANN

Approximate Nearest Neighbor — fast but slightly imprecise vector search

HNSW

Hierarchical Navigable Small World — graph-based ANN algorithm

Qdrant

Open-source vector database optimized for production workloads

pgvector

PostgreSQL extension for vector similarity search

Labs

Hands-on labs

35 minIntermediate

Setup Qdrant and Build Semantic Search

Deploy Qdrant and build a search API.

  1. Deploy Qdrant with Docker
  2. Create a collection with HNSW index
  3. Ingest 1000 documents with metadata
  4. Build a FastAPI search endpoint with filtering
View lab on GitHub
30 minIntermediate

Build Search APIs with pgvector

Use PostgreSQL for vector search.

  1. Setup PostgreSQL with pgvector extension
  2. Create table with vector column and HNSW index
  3. Insert embeddings and query with SQL
  4. Compare performance with Qdrant
View lab on GitHub

Recap

Key takeaways

  • HNSW is the dominant ANN algorithm — O(log n) with 95-99% recall
  • Qdrant is production-grade: fast, filterable, multi-tenant, open source
  • pgvector works if you already use PostgreSQL and need moderate scale
  • Metadata filtering is critical — vector similarity alone is not enough
  • Choose your vector DB based on scale, ops model, and existing infrastructure

Related resources

Keep learning across CodersSecret