short · 3 min
HNSW
A layered graph index for vector search: long hops at the top, short ones at the bottom. Fast and accurate, paid for in memory.
updated 27 Sept 2026
We have a million vectors and one query. How do we find the closest few without comparing the query to all million? That's the job of a vector index, and HNSW (Hierarchical Navigable Small World) is one of the most popular and best-performing answers. Let's build one.
A graph and a greedy walk
Draw every stored vector as a dot, and connect each one to a handful of nearby dots. Now to search, we start at a fixed entry point, look at its neighbours, and move to whichever one is closest to the query. Then again from there. When no neighbour is closer than where we're standing, we stop. That's a local minimum, and it's our answer.
If the graph has a mix of short and long links, a few hops can cross the whole collection. That's a "navigable small world" graph. The trouble is that on big graphs that weren't built carefully, this greedy walk breaks down.
Express lanes
The fix comes from an old structure called a skip list: several linked lists stacked up, the top one skipping many elements at a time, each one below skipping fewer. You search top-down, express train first, then the local.
HNSW does the same with graphs. Every vector sits in the bottom layer, linked to its close neighbours. Each vector also gets a random top layer, with odds that drop off fast as you go up. So most vectors live only at the bottom, a few reach the next layer, and very few reach the top. A vector that's on a layer is on every layer below it too. Upper layers have fewer vectors, so their links are longer.
A search starts at the top and does the greedy walk until it gets stuck. Then it drops to the same vector one layer down and walks again, with shorter links. At the bottom it finishes among the query's closest neighbours.
The top layers get us to the right neighbourhood in a few long hops, and the bottom layer does the fine work. That's why HNSW scales so well: grow the collection a lot and a search needs only a few more steps.
Building uses the same walk: each new vector gets a random top layer, searches down for its neighbours, and links in. No training step.
The knobs
There are three settings. M is how many links each vector gets. More links mean better recall and a bigger index, and it's the only setting that changes memory. efConstruction is how many candidate neighbours the build considers for each new vector. Higher builds a better graph, but it's fixed once the index exists. efSearch is how many candidates a query keeps as it walks. Higher means better recall and slower queries, and we can change it any time without rebuilding.
How big are these trade-offs? On one standard test set in Faiss, tuning moved a search from about 80% recall in 1 ms to 100% recall in 50 ms. The index took over half a gigabyte with M at 2, and almost 5 GB with M at 512. Other libraries and data land elsewhere, so measure on your own.
So touch efSearch first, since it's free to change, and rebuild with a bigger M or efConstruction only if that isn't enough.
Two things to keep in mind. HNSW stores all those links on top of every vector, so it's hungry for memory. Compressing the vectors helps, at some cost to recall and speed. And it's still approximate: the walk can stop at a local minimum that isn't the true nearest neighbour. A higher efSearch makes that rarer, but only checking every vector guarantees it. The Postgres version of all this, with its own names and defaults, is in pgvector.
Further reading
- Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs, Malkov and Yashunin, 2016 (revised 2018). The original paper. The abstract alone covers the random layers and the top-down search.
- Hierarchical Navigable Small Worlds (HNSW), Pinecone, undated. A visual walk from skip lists to HNSW, with the recall, speed and memory numbers. The easiest way in.