Understand HNSW: Why Your Vector Search Returns Garbage (Build your own minimalist HNSW from…

· Source: Towards AI - Medium · Field: Technology & Digital — Artificial Intelligence & Machine Learning, Data Science & Analytics, Software Development & Engineering · Depth: Advanced, long

Summary

The article explains HNSW (Hierarchical Navigable Small World) graphs, a common Approximate Nearest Neighbor (ANN) algorithm used in vector databases for scalable vector search. It highlights that poor search results in RAG systems are often attributed to embedding models but are more frequently caused by misconfigured HNSW parameters. The author builds a minimalist HNSW index from scratch using Python and NumPy to demonstrate how M (node connections), ef_construction (build-time search width), and ef_search (query-time search width) impact recall. Experiments with a synthetic dataset of 2050 vectors (five clusters, fifty outliers, 64 dimensions) show that a "weak" index (M=4, ef_construction=10) caps outlier recall at 0.21, while a "strong" index (M=32, ef_construction=200) achieves 0.655. The article emphasizes that build-time parameters (M, ef_construction) set the recall ceiling, while ef_search determines how close to that ceiling the system gets, often with diminishing returns on latency. It also provides direct mappings to parameters in production vector databases like Pinecone, Qdrant, and FAISS.

Key takeaway

For MLOps Engineers optimizing RAG systems, if you encounter consistently poor or inconsistent search results, especially for uncommon queries, prioritize HNSW index configuration over embedding model changes. Your M and ef_construction settings critically define the graph's recall ceiling; ef_search only determines how close you get. Measure recall against latency to avoid over-tuning ef_search for negligible gains, saving real money and user-facing delay.

Key insights

Misconfigured HNSW parameters, not embedding models, are often the root cause of poor vector search recall in RAG systems.

Principles

Method

The article describes building a minimalist HNSW index in Python/NumPy, involving a _random_level function for layering, a two-phase insertion process with greedy descent and neighbor selection/pruning, and a layered descent search.

In practice

Topics

Best for: AI Engineer, Machine Learning Engineer, MLOps Engineer

Related on AIssential

Open in AIssential →

Editorial summary, takeaway, and curation by AIssential. Original article published by Towards AI - Medium.