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






