Prompts Forms Resume Reports ★ Saved ★ Saved★ + Submit a tool
Techniques

HNSW Graph Index

About 3511 characters Updated Nov 30, 2026 21 views AI wiki

HNSW (Hierarchical Navigable Small World graph) is currently the most widely used graph index for approximate nearest neighbor search, proposed by Malkov and Yashunin. It has almost become the default index option for vector databases; mainstream systems such as Milvus, Qdrant, Weaviate, pgvector and Faiss all implement it, and it has long held an advantage along the axis of "high recall, low latency".

Core Principles

The core of HNSW is a multi-layer skip-list-style proximity graph. The bottom layer, layer zero, contains all vector nodes, and each layer above has exponentially fewer nodes, forming a skeleton sparser layer by layer. Each node has several edges to neighbors within the layer it belongs to, and edges are established following the proximity principle and kept bidirectional. When inserting a new vector, a level is first assigned to it at random, then greedy search starts from the entry point of the highest layer, finding at each layer the node closest to the target as the entry for the next layer; after reaching its own level, neighbor connections are built within that layer and excess neighbors are pruned.

A query likewise starts from the top entry, using greedy edge traversal at high layers to approach the region where the target lies, refining the search once per layer descended; at the bottom layer it switches to beam search, maintaining a candidate pool of size efSearch that controls how many nodes the bottom layer must visit. It is precisely this structure, "sparse upper layers for navigation, dense lower layers for accuracy", that brings query complexity close to logarithmic while retaining high recall. Its idea traces back to small-world networks and navigable graphs: even without a global index, local edges alone can efficiently cross the graph to reach distant neighbors.

Key Points

There are three core parameters. M is the upper limit of edges each node keeps per layer, usually sixteen to sixty-four; a larger M makes the graph denser and recall higher, but raises memory usage and build time in step. efConstruction is the candidate pool size for bottom-layer search during construction, often one hundred to four hundred; larger values build a better graph but build more slowly. efSearch is the candidate pool size at query time and the only parameter adjustable at runtime; it directly defines the trade-off curve between recall and latency: raising it increases recall and slows latency. In practice, for the recall required by the business, measure this curve on a validation set and take the value at the knee. For memory, HNSW must store both the graph structure and the raw vectors; hundreds of millions of items often need tens of GB, usually combined with product quantization to compress the vectors themselves. Deletion support is weak: most implementations use tombstone markers plus periodic rebuilds, and after heavy deletion it is best to rebuild the whole library.

Common Misconceptions

People often treat efSearch as a fixed value to copy, whereas it is strongly related to M, the data distribution and query difficulty and must be tuned empirically. Another misconception is that HNSW is always better than an inverted index; when the data volume is only in the hundreds of thousands, memory is tight, or frequent insertion and deletion are needed, IVF or even brute-force search may be more cost-effective. Some ignore the fatal impact of metadata filtering: under filter conditions, edge traversal breaks the graph's connectivity and causes a cliff in recall, requiring dedicated filtering algorithms or filtering before retrieval. In addition, during incremental writes the graph quality degrades over time; if the library only grows and is never rebuilt, rebuilding is a necessary maintenance action. A common operational rule is to trigger a rebuild when the deletion ratio exceeds twenty percent, or when recall falls five points below the level at launch.

Application Scenarios

RAG semantic retrieval, image-to-image and text-to-image search, vector recall in recommender systems, deduplication and clustering, similarity association of logs and security events, multimodal retrieval.