liveBook · Manning

HNSW

As explained in MongoDB 8.0 in Action, Third Edition

HNSW is a graph-based approximate nearest-neighbor search algorithm that organizes data points into multiple navigable layers.

HNSW (Hierarchical Navigable Small Worlds) is a graph-based approximate nearest-neighbor algorithm for retrieving semantically similar vectors quickly. It organizes data points as nodes connected by similarity-based edges across multiple navigable layers. section 12.2

Search begins at an entry point in the sparse upper layer, greedily moves toward nearer neighbors, and descends through increasingly dense layers. Upper layers provide speed and broad navigation; lower layers refine accuracy. section 12.2

Use HNSW when vector retrieval must scale for real-time applications:

  • Choose approximate nearest-neighbor search when faster, scalable retrieval provides a useful balance between speed and precision. section 12.2
  • Choose exact nearest-neighbor search when comparing the query with every vector and guaranteeing the closest match matters more than computational cost. section 12.2

HNSW with Atlas Vector Search

Create an Atlas Vector Search index for fields containing embeddings, then use MongoDB's $vectorSearch aggregation stage with a query vector embedded by the same model used for the stored data. Set "exact": false for HNSW-based approximate search; set it to true for ENN. section 12.8

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains layered graph construction and greedy descent, but does not specify index-tuning parameters or implementation code.
Examples
Uses layered vector graphs and Atlas $vectorSearch queries with exact set to false as concrete examples.
What it assumes
Assumes familiarity with vector embeddings and requires queries embedded with the same model used for stored data.
What you can do after
Apply HNSW through Atlas Vector Search indexes and $vectorSearch for fast approximate retrieval of semantically similar documents.
How it connects
Connects approximate nearest-neighbor search, exact nearest-neighbor search, vector embeddings, Apache Lucene, and Atlas Vector Search.

Also explained in

Compare how 7 books explain HNSW →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.