Data & databases

HNSW

AI-assisted editorial review:

HNSW (Hierarchical Navigable Small World) is a graph-based index for approximate nearest neighbor (ANN) search over vectors. It stores vectors in several layers of proximity graphs and starts each search in the sparse top layer, narrowing down as it moves to denser lower layers; the original paper reports logarithmic complexity scaling.[1] The balance between accuracy and speed is tuned mainly with M (connections per node), efConstruction (candidate list size while building) and ef / efSearch (candidate list size while searching).[1][5] HNSW works best when the graph and vectors sit in memory, so memory use is its main cost.[7]

What does HNSW mean in simple terms?

The name describes the idea: a "small world" graph you can navigate, stacked in layers. Think of finding an address. You take the highway to get the general direction, switch to local roads to narrow the area, and finish on side streets to reach the door. The upper layers have few, long-distance links that let the search jump far; lower layers are denser and refine the result. This way the system finds close neighbors without comparing the query against every stored vector.[1]

How can HNSW be explained with an analogy?

A set of maps at different scales: the highway map sets the direction, the road map narrows the area, and the street map takes you to the door.

Where is HNSW used?

Case 1

The original paper — Proposing HNSW and benchmarking it

The paper was first posted to arXiv in March 2016 and revised to v4 in August 2018.[1] In vector benchmarks it compared HNSW with NSW, FLANN, Annoy, VP-tree and FALCONN on datasets such as SIFT (1M vectors, 128 dimensions), GloVe (1.2M, 100 dimensions) and CoPhIR (2M, 272 dimensions), and separately with the FAISS library on a 200M-vector SIFT subset.[1] The authors call 5–48 a reasonable range for M and suggest 2·M as the maximum number of connections on the bottom layer.[1] They listed support for element updates and removal as future work.[1]
Result: Index memory (excluding the vectors themselves) of about 60–450 bytes per object for typical M values of 6–48.[1]

Case 2

pgvector on Amazon RDS for PostgreSQL — Measuring ingest and query performance

In a November 2023 AWS Database Blog post, AWS compared pgvector 0.5.0 and 0.5.1 using HNSW indexes on Amazon RDS for PostgreSQL; the post notes the tests can also be run on Aurora PostgreSQL.[2] It used pgvector 0.5.1's defaults of m=16 and ef_construction=64 and varied ef_search from 20 to 800 in query tests.[2] With 10 million 1,536-dimensional vectors, the table took 39 GB and the HNSW index 38 GB, so the index was about as large as the data itself — a useful reference point for capacity planning.[2]
Result: In 15-minute ingest tests with 128-dimensional vectors and 64 concurrent clients on db.m7g.16xlarge, throughput rose from 2,758 TPS (0.5.0) to 8,960 TPS (0.5.1).[2]

Case 3

Elasticsearch — HNSW combined with quantization by default

In the current Elasticsearch mapping documentation, HNSW defaults to m=16 and ef_construction=100.[3] Defaults depend on the Elasticsearch version and license. The documentation includes a float-vector configuration where the default index type is int8_hnsw below 384 dimensions and bbq_hnsw at 384 dimensions or more (the same page notes that in Elastic Stack 9.0 float vectors were always indexed as int8_hnsw).[3] It also documents bbq_disk as a default where available under the current license. Check the version-specific defaults for your installation.[3] The original float values stay on disk for reranking and reindexing, so disk use grows by about 25% for int8, 12.5% for int4 and 3.1% for bbq.[3]
Result: Memory footprint reduced about 4x with int8, 8x with int4 and 32x with bbq, compared with unquantized vectors.[3]

Case 4

MiriCanvas (Miridih) — Running dual-vector search on Amazon OpenSearch

In July 2026 the AWS Korea tech blog published a post co-written with Miridih, which runs the design platform MiriCanvas, about vector search with the OpenSearch k-NN plugin.[4] The index held about 40 million documents with five vector fields, and each dual-vector query searched two vector fields with k=200.[4] The post notes that raising HNSW's ef_search or m improves recall at the cost of latency and memory. The bottleneck turned out not to be the vector graph itself: k-NN native memory use was about 16%, while OS memory use reached 99% because vectors stored in _source enlarged the index and exhausted the page cache.[4] The team moved from 8 general-purpose to 12 memory-optimized instances, raising total memory from 512 GB to 768 GB at about the same monthly cost.[4]
Result: Average search latency fell from 18.2 ms to 14.4 ms (−21%) and peak read IOPS from 5,130 to 3,410 (−34%), before vs after the instance change.[4]

Try it today

  1. Count your vectors and their dimensions first. In AWS's public test, 10 million 1,536-dimensional vectors needed a 38 GB HNSW index on top of a 39 GB table.[2]
  2. Check your engine's defaults. Elasticsearch uses m=16 and ef_construction=100, Qdrant m=16 and ef_construct=100, and pgvector 0.5.1 used m=16 and ef_construction=64.[3][5][2]
  3. Start by changing only the search-time setting (ef_search / ef) and record recall and latency in the same table. In AWS's test this setting was varied per query run without changing the build parameters.[2][5]
  4. If you use filtered search in Qdrant, create payload indexes right after creating the collection, before loading data; otherwise you need to rebuild the HNSW index to benefit from them.[5]
  5. If your data is deleted or updated often, review how deleted entries are cleaned up. Weaviate cleans up tombstones every 300 seconds by default.[6]

Limits and direction

HNSW has three main limits. The first is memory. The original paper estimates 60–450 bytes per object for the index alone, excluding the data,[1] and Elastic explains that HNSW needs all vectors in RAM to work well, with performance dropping sharply once vectors fall out of memory.[7] The second is deletion. The original paper left element updates and removal as future work,[1] and engines handle deletes with tombstones or soft deletes. Weaviate notes that tombstone cleanup can take longer and use more resources as an index grows,[6] and Qdrant notes that many soft-deleted points can split the graph into disconnected parts during filtered search.[5] The third is build cost: adding vectors requires searching the existing graph, so memory pressure also slows indexing.[7] In Elastic's 1M-vector test, building an HNSW index over BBQ-quantized vectors took 1,054,319 ms.[7]

Development is moving in two directions: disks and compression. DiskANN (NeurIPS 2019) stores the graph and full vectors on an SSD and keeps compressed vectors in memory. It indexed a billion vectors on one workstation with 64 GB of RAM and, on SIFT1B, served over 5,000 queries per second at under 3 ms mean latency with 95%+ 1-recall@1 on a 16-core machine.[8] Quantization is now common too: Elasticsearch combines HNSW with int8, int4 and binary quantization for about 4x, 8x and 32x memory savings.[3] In October 2025 Elastic introduced DiskBBQ, which uses hierarchical k-means clustering and binary quantization as an alternative to HNSW.[7] In the same 1M-vector test, DiskBBQ indexed in 94,075 ms with about 4.0 ms latency and 91% recall, versus 1,054,319 ms, about 3.4 ms and 92% for HNSW with BBQ. Elastic still recommends HNSW when very high recall is needed and memory is available.[7]

Which sources explain HNSW?

  1. arXiv (Malkov, Yashunin) — Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs (v1 2016-03-30, v4 2018-08-14) — https://arxiv.org/abs/1603.09320
  2. AWS Database Blog — Accelerate HNSW indexing and searching with pgvector on Amazon Aurora PostgreSQL-compatible edition and Amazon RDS for PostgreSQL (2023-11-06) — https://aws.amazon.com/blogs/database/accelerate-hnsw-indexing-and-searching-with-pgvector-on-amazon-aurora-postgresql-compatible-edition-and-amazon-rds-for-postgresql/
  3. Elastic Docs — Dense vector field type (checked 2026-10-13) — https://www.elastic.co/docs/reference/elasticsearch/mapping-reference/dense-vector
  4. AWS Korea Tech Blog — Amazon OpenSearch Service로 미리캔버스의 듀얼 벡터 검색 도입과 성능 최적화 (2026-07-05) — https://aws.amazon.com/ko/blogs/tech/miricanvas-dual-vector-search-opensearch-3-3-upgrade-1/
  5. Qdrant Docs — Indexing (checked 2026-10-13) — https://qdrant.tech/documentation/manage-data/indexing/
  6. Weaviate Docs — Vector index (checked 2026-10-13) — https://docs.weaviate.io/weaviate/config-refs/indexing/vector-index
  7. Elasticsearch Labs — Introducing a new vector storage format: DiskBBQ (2025-10-23) — https://www.elastic.co/search-labs/blog/diskbbq-elasticsearch-introduction
  8. NeurIPS 2019 (Subramanya et al.) — DiskANN: Fast Accurate Billion-point Nearest Neighbor Search on a Single Node (2019) — https://suhasjs.github.io/files/diskann_neurips19.pdf