HNSW
Hierarchical Navigable Small World
ELI5 - The Vibe Check
HNSW is how a vector database finds your nearest neighbors without checking every single point in a haystack of millions. It builds a multi-layer shortcut graph, kind of like a highway system: start on the sparse top layer to get in the right neighborhood fast, then drop down through denser layers to pinpoint the exact closest matches. It's why semantic search over millions of embeddings still feels instant.
Real Talk
HNSW is a graph-based approximate nearest neighbor algorithm that organizes vectors into multiple layers of proximity graphs, with sparser graphs at higher layers enabling fast coarse navigation and denser graphs at lower layers enabling fine-grained search. It offers logarithmic-scale search complexity with strong recall, and is the default index type in most vector databases including pgvector, Qdrant, and Weaviate.
Show Me The Code
-- pgvector: create an HNSW index for cosine distance
CREATE INDEX ON documents
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
When You'll Hear This
"We switched from IVF to HNSW and recall jumped at the same latency." / "Tune ef_search if HNSW results feel imprecise."
Related Terms
Cosine Similarity
Cosine similarity measures how similar two things are by comparing the angles of their vectors.
Embedding
An embedding is turning words, sentences, or entire documents into lists of numbers (vectors) that capture their meaning.
Semantic Search
Semantic search finds results based on meaning, not just keyword matching.
Vector
In AI, a vector is just a list of numbers. But it's a list of numbers that means something — like [0.23, -0.91, 0.44, ...
Vector Database
A vector database is a special database built to store and search embeddings.