---
title: "HNSW"
type: "glossary term"
url: "https://aifordevs.org/glossary#hnsw"
markdown: "https://aifordevs.org/glossary/hnsw.md"
publisher: "AI for Devs"
license: "https://aifordevs.org/reuse"
attribution: "Credit AI for Devs and link to https://aifordevs.org/glossary#hnsw"
also_known_as: "Hierarchical Navigable Small World"
---

# HNSW (Hierarchical Navigable Small World)

HNSW (Hierarchical Navigable Small World) is a graph index for approximate nearest neighbor search that stacks proximity graphs, with fewer points in each higher layer. Queries start at the top layer and descend, which gives logarithmic scaling with dataset size.

Each vector gets a random top layer with an exponentially decaying probability, so upper layers stay sparse. M limits links per node, ef_construction sets the candidate list at build time, and ef_search sets it at query time, which trades recall for latency. pgvector's docs list IVFFlat as faster to build and lighter on memory than HNSW, with lower query performance at the same recall.

## Example

pgvector defaults to m = 16, ef_construction = 64, and hnsw.ef_search = 40. Raise ef_search for a query when recall matters more than latency.

## Source

[Malkov and Yashunin, Hierarchical Navigable Small World graphs (2016)](https://arxiv.org/abs/1603.09320)

## Related terms

- [Vector database](https://aifordevs.org/glossary#vector-database)
- [Embedding](https://aifordevs.org/glossary#embedding)
- [Hybrid search](https://aifordevs.org/glossary#hybrid-search)

## Read the article

[What is a vector database? HNSW, IVF, and when to skip one](https://aifordevs.org/articles/what-is-a-vector-database)

## Cite this term

AI for Devs. "HNSW." https://aifordevs.org/glossary#hnsw

- Page: https://aifordevs.org/glossary#hnsw
- Markdown: https://aifordevs.org/glossary/hnsw.md
- Reuse: Quote or summarize with a link to this page. Terms: https://aifordevs.org/reuse
- Publisher: AI for Devs
