Retrieval · Approximate search

HNSW Visualised

See a search move down a 3D HNSW index and find nearby vectors. Change the query or search breadth, then compare the result with exact search.

There is a narrated film on this topic.Watch Vector search: HNSW, cosine similarity and BM25

Ready · 1 / 10

Enter the index · N01 · L2

Enter layer 2 at N01. Only a few vectors live this high.

HNSW search: layer 2, Enter layer 2 at N01. Only a few vectors live this high.L0 · 36 nodesL1 · 11 nodesL2 · 3 nodesN01Q
Query Current Retained Scored

Closest retained · 1/1 kept

N01 (2.49)

Newly scored: N01

Upper layers keep one closest candidate. The base layer uses your search breadth.

Distances use all four vector dimensions. The view plots two; height separates index layers. Fixed sparse links (M = 2), 36 vectors, entry N01. Playback shows steps, not measured timing.

HNSW stands for Hierarchical Navigable Small World. It searches a stack of proximity graphs: sparse upper layers guide the route, then the base layer searches among all indexed vectors. Choose a query and press Play to follow each expansion and descent.

Search breadth (efSearch) controls how many promising candidates the base-layer search retains. It is not a cap on distance calculations. A wider search can find better neighbours at the cost of more work, but cannot guarantee perfect recall. At the end, compare the three returned neighbours with exact search and see how many unique vectors were scored. The separate exact comparison is excluded from that work count.

This example uses 36 fixed four-dimensional vectors, a sparse index with M = 2 and entry point N01. Distances use all four dimensions. The plot uses two coordinates plus organisational height for layers, so screen proximity can be misleading. Repeated nodes across layers represent the same vector. Fixed levels and simple neighbour selection make this a teaching model, not a production benchmark.

The maths

How many true neighbours did we find?
recall@3=A3E33\operatorname{recall@}3=\frac{|A_3\cap E_3|}{3}

A is the returned set and E is the exact nearest-neighbour set. Finding two of the exact top three gives about 67% recall for this query. The final comparison shows both sets; exact search scores all 36 vectors separately.

Sources and model assumptions

Follow the original mechanism behind this explainer. The interactive examples identify their toy data and simplifying assumptions above.

Related terms

More visualisations

Building with language models?

These explainers come out of the work. If you want the same thinking applied to your own system, that is what I do.

See how I can help