# Hierarchical Navigable Small World (HNSW)

> Canonical: https://skillalmanac.com/glossary/hnsw | Last verified: 2026-11-24 | Index status: not offered to search engines

HNSW (Hierarchical Navigable Small World) is a graph-based method for approximate nearest neighbor search: it finds vectors close to a query without comparing against all of them, by searching through layered graphs from sparse to dense.

Also called: HNSW, HNSW index, HNSW algorithm, HNSW graph, hierarchical navigable small world graphs.

## In plain language

Imagine finding a street in an unfamiliar country. You do not check every house: you look at a country map, then a regional map, then a city map, then the street. HNSW does the same for lists of numbers that stand for the meaning of text or images. It builds several maps of the data, from coarse to detailed, and starts at the coarse one. This lets a search system return items very similar to your query quickly. The catch is that the result is approximate, so the true closest item can be missed, and the graph is held in memory, which costs more as the data grows.

## Technical definition

HNSW, proposed by Malkov and Yashunin (arXiv 1603.09320, first submitted 30 March 2016), builds a multi-layer set of proximity graphs over nested subsets of the data. Each element's top layer is drawn at random with an exponentially decaying probability, so upper layers are sparse. A query enters at the top layer, moves greedily to the neighbour closest to the query until no neighbour is closer, then drops a layer and repeats down to layer 0, which holds all vectors. Build and query behaviour is tuned by parameters such as M (links per vertex), efConstruction and efSearch, which trade recall against build and search time; only M affects index memory, per Pinecone.

## How it shows up when you use agents

If you build an agent or a [RAG](/glossary/retrieval-augmented-generation) pipeline, your documents are turned into [embeddings](/glossary/embedding), lists of numbers whose closeness reflects similarity of meaning. A [vector database](/glossary/vector-database) then has to find the stored vectors nearest to a question's vector. Comparing against all of them gets slow as the collection grows, so many systems build an approximate index. HNSW is one such index. IBM lists it as an index type that vector databases use, and Weaviate documents it as its default index type.

You usually do not call HNSW yourself. You meet it as settings in a vector store: a link count (often called M), a build-time candidate list size (efConstruction) and a query-time one (efSearch). Pinecone's write-up says a higher M improves recall but raises memory and search time, and that efSearch can be changed after the index is built.

What breaks when it is misunderstood: people treat the top result as exact, then blame the language model when a relevant passage was simply missed by the approximate search. The next action is to test recall on a small set of questions with known answers, and raise the query-time setting before changing prompts. Also plan memory, because the graph must be stored alongside the vectors. This matters for [semantic search](/glossary/semantic-search) too: measure retrieval first.

## Common confusions

- **HNSW versus a vector database.** HNSW is an algorithm and index structure. A vector database is a system that stores vectors and may use HNSW, among other index types.
- **Approximate versus exact search.** HNSW returns neighbours that are very likely close, not a guarantee of the closest. Exact search compares against all vectors and gets slower as data grows.
- **HNSW versus embeddings.** Embeddings are the vectors. HNSW is how you search among them.
- **Myth.** Higher recall settings are free. In Pinecone's tests, raising M costs memory and raising efSearch costs search time.

## Worked examples in this catalogue

### [rag-chunking-planner](https://skillalmanac.com/skills/skill-almanac/rag-chunking-planner)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Your documents chunked by three strategies, with defects and offline recall measured for each, so chunk settings are chosen with numbers.

Why it exercises the concept: An index such as HNSW can only return the chunks you stored. This skill measures the chunking step that decides what is available to find.

### [library-docs-lookup](https://skillalmanac.com/skills/skill-almanac/library-docs-lookup)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: An answer to a library question drawn from the installed package's own files, with file and line cited.

Why it exercises the concept: It is retrieval for a coding agent by reading files. It shows the lookup problem that vector indexes solve at larger scale, without using one.

### [What Is RAG? Retrieval-Augmented Generation Explained](https://skillalmanac.com/learn/what-is-rag)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The guide walks through indexing and querying, then a worked chunking example.

Why it exercises the concept: The query phase of RAG is where a nearest neighbor index such as HNSW is usually used.

## Related terms

- [Vector database](https://skillalmanac.com/glossary/vector-database.md)
- [Embedding](https://skillalmanac.com/glossary/embedding.md)
- [Semantic search](https://skillalmanac.com/glossary/semantic-search.md)
- [Retrieval-augmented generation (RAG)](https://skillalmanac.com/glossary/retrieval-augmented-generation.md)
- [Cosine similarity](https://skillalmanac.com/glossary/cosine-similarity.md)

## Sources

- [Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs](https://arxiv.org/abs/1603.09320), arXiv (Malkov and Yashunin) (accessed 2026-11-24)
- [Hierarchical Navigable Small Worlds (HNSW)](https://www.pinecone.io/learn/series/faiss/hnsw/), Pinecone (accessed 2026-11-24)
- [What is a vector database?](https://www.ibm.com/think/topics/vector-database), IBM (accessed 2026-11-24)
- [Vector Indexing](https://docs.weaviate.io/weaviate/concepts/vector-index), Weaviate (accessed 2026-11-24)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-24. [Glossary index](https://skillalmanac.com/glossary.md).
