Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124

When I first came across this algorithm I was intrigued. What did it mean? Working/playing with vector database like Milvus, Pinecone, Weaviate, or Vespa, I saw HNSW mentioned in the index settings.
I discovered this algorithm, Hierarchical Navigable Small World graphs, is the reason modern AI search can find semantically similar items in billions of vectors within a few milliseconds.
In this post, we’ll break down:
Vector search is about finding the nearest neighbors to a query vector in high-dimensional space.
Naïve approach:
Need an index structure that reduces comparisons while keeping accuracy high → Approximate Nearest Neighbor (ANN).
HNSW builds a multi-layer graph where:
Key idea:
Instead of exhaustively checking every vector, walk the graph from a high-level overview down to fine-grained neighborhoods.
HNSW organizes data into layers:
When searching:
This navigable small-world property means you jump across large areas quickly, then zoom in.
When inserting a vector:
M)Parameters:
M: number of bi-directional connections per node (graph connectivity)efConstruction: size of candidate list during insertion (higher = more accurate but slower build)Parameters:
efSearch: size of candidate list during search (higher = better recall, slower)Steps:
efSearch candidates are evaluatedTime Complexity:
# Milvus index config:
index_params = {
"index_type": "HNSW",
"metric_type": "cosine",
"params": {"M": 16, "efConstruction": 200}
}
collection.create_index("embedding", index_params)
# Search config
search_params = {
"metric_type": "cosine",
"params": {"ef": 64}
}
results = collection.search([query_vec], "embedding", search_params, limit=10)M=16: 16 connections per nodeefConstruction=200: Higher build-time accuracyef=64: Good balance between recall and latency in searchWhile HNSW dominates in open-source vector DBs, alternatives exist:
Many production systems use hybrid strategies — e.g., IVF for partitioning + HNSW for local search.
HNSW is the workhorse behind modern semantic search — powering recommendations, RAG pipelines, and image search across billions of vectors.
It’s not perfect (memory-heavy, CPU-bound), but for in-memory high-recall search, it’s unmatched.
The next wave will likely be disk-backed hybrid HNSW + compression, making it feasible to handle trillions of vectors without sacrificing latency.