Skip to content

HNSW

Hierarchical Navigable Small World

Spicy — senior dev territoryDatabase

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.