HNSW Index: Architecture for Fast Vector Search › Distance Metrics and HNSW

Michael BrenndoerferJanuary 26, 20263 min read
Language AI HandbookAll chapters
Part 44 of 67Retrieval-Augmented Generation
Chapter 339HNSW Index: Architecture for Fast Vector Search › Distance Metrics and HNSW
Reading section 7 of 14 · Distance Metrics and HNSW · 3 minSection 7 of 14 · 3 min

Book contents

Section 7 of 14 · 3 min read

HNSW Index: Architecture for Fast Vector Search › Distance Metrics and HNSWOne section · Sections in this chapter7 / 14 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

HNSW is metric-agnostic: the algorithm works with any distance function, and the choice of metric has a significant impact on search quality and speed. Understanding which metric to use, and why, is an important practical consideration.

The three most common distance metrics in vector search are:

  • Euclidean distance (L2L_2): measures the straight-line distance between two vectors. If u\mathbf{u} and v\mathbf{v} are vectors in Rd\mathbb{R}^d, then dL2(u,v)=∑i=1d(ui−vi)2d_{L2}(\mathbf{u}, \mathbf{v}) = \sqrt{\sum_{i=1}^{d} (u_i - v_i)^2}. Euclidean distance is sensitive to the magnitude of vectors, not just their direction. It is the natural choice when the absolute position in the embedding space carries meaning.
  • Inner product (dot product): dIP(u,v)=−uTv=−∑i=1duivid_{IP}(\mathbf{u}, \mathbf{v}) = -\mathbf{u}^T \mathbf{v} = -\sum_{i=1}^{d} u_i v_i. The negative sign converts similarity into a distance (higher inner product means smaller "distance"). Inner product is maximized when two vectors point in the same direction and have large magnitudes.
  • Cosine distance: dcos(u,v)=1−uTv∥u∥∥v∥d_{cos}(\mathbf{u}, \mathbf{v}) = 1 - \frac{\mathbf{u}^T \mathbf{v}}{\|\mathbf{u}\| \|\mathbf{v}\|}. This measures the angle between two vectors, ignoring their magnitudes. Cosine distance is equivalent to inner product on unit-normalized vectors, so normalizing your embeddings before indexing with inner product gives you cosine similarity.

For RAG systems with sentence embeddings (from models like all-MiniLM-L6-v2 or text-embedding-3-small), cosine similarity is almost always the right choice. Sentence embedding models are trained to encode semantic meaning in the direction of the vector, not its magnitude. Two sentences about the same topic should have similar directions even if they have different lengths (and thus different embedding magnitudes). Using inner product on unnormalized embeddings would conflate semantic similarity with embedding magnitude, giving misleading results. The standard practice is to normalize all embeddings to unit length before indexing, then use inner product (which equals cosine similarity on unit vectors) for maximum speed.

Euclidean distance is more appropriate in settings where the raw vector coordinates carry meaning, such as item embeddings trained with specific geometric objectives, or audio features where amplitude matters. For most NLP applications, cosine or inner product is preferred.

One important subtlety: HNSW's navigability relies on the distance metric being consistent with the graph's connectivity. The algorithm works best when the metric produces a smooth embedding space where nearby vectors are semantically similar and distant vectors are semantically different. Metrics that produce poorly separated embeddings (where many unrelated vectors end up at similar distances) degrade HNSW's performance by making it harder for the greedy search to make directional progress.