What is HNSW?

HNSW is an approximate nearest neighbour search algorithm that organises vectors in a layered proximity graph, quickly finding those closest to a query by descending through the layers without scanning all data.

The name stands for Hierarchical Navigable Small World. It is one of the common graph-based solutions to approximate nearest neighbour search. Methods such as Inverted File Index instead narrow the search by clustering vectors.

Topic
Embeddings, document processing and RAG
Also known as
Hierarchical Navigable Small World, HNSW graph
Origin
2016, Malkov and Yashunin

How HNSW works

  1. Each vector becomes a graph node linked to its nearest neighbours; the number of links per node is a parameter.
  2. Nodes are also randomly added to upper layers; upper layers are sparse with long jumps while the bottom layer holds all vectors.
  3. Search starts at the top layer, moves to neighbours closer to the query and drops one layer at a time.
  4. At the bottom layer a candidate list of set width is kept and the nearest vectors are returned.

In more detail

Core parameters: M, efConstruction and efSearch

In practice, HNSW behaviour is governed by three parameters: M (maximum links per node, typically 16 to 64, controlling memory and connectivity), efConstruction (beam width during index build, affecting build time and graph quality) and efSearch (candidate list size at query time, trading latency for recall). Because upper layers use greedy single-path traversal (ef = 1) while the bottom layer performs beam search, tuning efSearch can adjust recall dynamically at query time without rebuilding the index.

Filtered search and the selectivity problem

Enterprise RAG queries often include metadata filters such as department, date range or user permissions. Naive post-filtering can return fewer than k results when few top-ranked vectors pass the filter, while naive pre-filtering may bypass the graph; modern engines therefore implement filter-aware HNSW traversal or fall back to exact scan when selectivity is high.

Memory compression through quantisation and the cost of tombstones

Because raw 32-bit HNSW indexes scale linearly in RAM, production deployments routinely pair HNSW with scalar quantisation (SQ8/int8, ~4x memory reduction), half-precision (FP16, 2x), Product Quantisation (PQ) or Binary Quantisation (up to 32x reduction with two-stage rescoring). Additionally, frequent deletions create tombstone nodes that degrade graph navigability and require periodic compaction or background re-indexing.

Commonly confused terms

TermHow it differs
IVFHNSW navigates a multi-layer proximity graph in memory without partitioning space into clusters, whereas IVF (Inverted File Index) clusters vectors via k-means into Voronoi cells and scans only the nearest nprobe clusters, trading slightly higher latency or lower recall for much smaller memory overhead.
DiskANNHNSW is an in-memory graph index that stores full vectors and neighbour lists in RAM, whereas DiskANN (Vamana) is a flat single-layer graph algorithm engineered specifically for NVMe SSD storage using Product Quantisation in RAM and disk reads for billion-scale archives.

Why HNSW matters for on-premises AI

HNSW is a common route to low-latency search in in-house vector databases and sets the speed of dense retrieval. It runs in memory, so it weighs on hardware plans: an archive of 1 million chunks with 1,024-dimensional 32-bit vectors needs 1,000,000 × 1,024 × 4 bytes = about 4.1 GB for vectors alone, with graph links on top. Search width and link count trade speed, memory and accuracy, tuned on the organisation's own queries.

ON-PREMISES EXAMPLE

An architecture firm keeps its project archive's embeddings in an HNSW index, widening the candidate list for accuracy while tracking response time.

Common questions

Does HNSW give exact results?

No, it is approximate and may miss some nearest vectors. Raising search width improves accuracy but slows search.

Why does HNSW use so much memory?

All vectors plus each node's neighbour links are kept in memory. For very large archives, disk-based or compressed methods are considered.

Sources

In the glossary: Glossary, E to L

What does this term mean for your infrastructure?

We can work out together in a technical assessment how it maps onto your data, hardware and security requirements.

Request a technical assessment →

This site sets no cookies. Visitor numbers are counted in aggregate using a cookieless method that does not identify anyone. See the Privacy Policy for details.