HNSW, one approximate index, walks a graph in layers
Finding the nearest vectors among millions needs an approximate index. HNSW is one widely used kind, and IVF, in the next step, is another.
| HNSW layer | Points |
|---|---|
| 2 | A, F |
| 1 | A, C, F, H |
| 0 | A, B, C, D, E, F, G, H, I |
A search starts at the top, hops toward the query, and drops a layer when it can't get any closer.
Retrieval asks for the 2,000 listing vectors closest to the query vector, among the eligible ones, in a few milliseconds. Comparing the query with every vector takes too long. An approximate nearest neighbor (ANN) index finds most of the closest vectors, not always all of them, in far less time. Three designs are worth knowing.
HNSW (hierarchical navigable small world) builds a graph over the vectors, in layers. The top layer has few points and long links. Each layer below has more points and shorter links. Its authors report that starting at the top, where links are longest, lets search time grow only with the logarithm of the number of points.
The graph costs memory: HNSW keeps the full vectors plus the links. And Faiss, a widely used library for vector search, notes that HNSW can't remove a vector, because that would break the graph.