Skip to main content

HNSW

To find someone in a huge city, you don't knock on every door — you ask a well-connected friend who points you a few hops closer, then a local who knows the exact street. HNSW (Hierarchical Navigable Small World) works the same way: it builds a network of shortcuts over your vectors so a search reaches the neighbourhood in a few jumps instead of scanning everything.

Layer 1 · sparseLayer 0 · denseentrytarget
Start sparse and hop toward the answer, then drop into a denser layer to refine — a few hops instead of a full scan.

How it works

HNSW stacks several layers. The top layer is sparse with long-range links — you travel far, fast. Each layer down is denser and more local. A search starts at the top, hops toward the target, then drops into lower layers to refine — arriving at the nearest neighbours in a handful of steps.

What it's like

  • Fast at scale — searches stay quick even with millions of vectors.
  • 📐 Built for high dimensions — the hundreds/thousands of numbers that language and vision models produce.
  • 🎯 Approximate — it may occasionally miss a true nearest neighbour in exchange for a huge speed-up. Fine for almost all semantic search.
  • 🎛️ Tunable — parameters trade accuracy against speed and memory.

When you'd use it

  • Large stores of high-dimensional embeddings where a full scan is too slow.
  • Semantic search where approximate results are acceptable — the default choice for most AI workloads.

Real-world examples

  • Semantic search / RAG — searching millions of document embeddings for a chatbot or knowledge base.
  • Recommendations — "users who liked this also liked…" over item embeddings.
  • Image search — finding visually similar products across a big catalogue.

Adding one

Create it with Create non-linear index using an HnswConfig (tune ef_construction, maximum_connections, and the distance metric).