Vector Distance Metrics

A distance metric is the formula a vector database uses to measure how similar or different two vectors are. Choosing the right metric directly affects the quality of your search results. This topic covers the three metrics you will encounter most often.

Why Distance Metrics Matter

Think of three ways to measure how far two cities are from each other:

  • Straight-line (as the crow flies)
  • Road distance (actual driving route)
  • Direction only (are they heading the same way?)

Each method gives a different answer and suits different purposes. Distance metrics in vector databases work the same way — different formulas, different use cases.

Cosine Similarity

Cosine similarity measures the angle between two vectors. It ignores how long the vectors are and only cares about the direction they point.

        Vector B
        ↑
        │  ← small angle = very similar
        │ /
        │/_____ Vector A

Score: 1.0 = identical direction (perfect match)
Score: 0.0 = perpendicular (no relation)
Score: -1.0 = opposite directions (opposite meaning)

Cosine similarity works best for text data because a short sentence and a long paragraph about the same topic point in the same direction, even though their lengths differ.

ScenarioCosine ScoreMeaning
"cat food" vs "cat food"1.0Identical
"cat food" vs "pet nutrition"0.87Very similar
"cat food" vs "car engine"0.11Unrelated

Best for: Text similarity, document search, semantic search, NLP tasks.

Euclidean Distance (L2)

Euclidean distance measures the straight-line distance between two points in vector space — exactly like measuring distance on a ruler.

2D Example:

  Vector A = [1, 2]
  Vector B = [4, 6]

  │
6 │          ● B
  │         /│
  │        / │  distance = 5
  │       /  │
2 │  ● A     │
  │          │
  └───────────────
     1    4

  Formula: √((4-1)² + (6-2)²) = √(9+16) = √25 = 5

Lower Euclidean distance means greater similarity. A distance of 0 means the vectors are identical.

Best for: Image embeddings, spatial data, clustering tasks where actual magnitude matters.

Dot Product

The dot product multiplies matching dimensions of two vectors and adds the results together. It combines both the direction and the magnitude of the vectors.

  Vector A = [2, 3]
  Vector B = [4, 1]

  Dot Product = (2×4) + (3×1) = 8 + 3 = 11

  Higher dot product = more similar
  (when vectors are normalized to length 1, dot product equals cosine similarity)

Best for: Recommendation systems, ranking tasks, and scenarios where vector magnitude carries meaningful information (such as "how popular" an item is).

Side-by-Side Comparison

MetricMeasuresPerfect Match ValueBest Use Case
Cosine SimilarityAngle between vectors1.0Text and NLP
Euclidean (L2)Straight-line distance0.0Images, spatial data
Dot ProductDirection + magnitudeHighest possibleRecommendations

How to Choose the Right Metric

START HERE
    │
    ▼
Are you working with TEXT data?
    │
   YES → Use COSINE SIMILARITY
    │
   NO
    │
    ▼
Does vector SIZE (magnitude) carry meaning?
    │
   YES → Use DOT PRODUCT
    │
   NO
    │
    ▼
Are you working with IMAGES or SPATIAL data?
    │
   YES → Use EUCLIDEAN (L2)
    │
   NO → Default to COSINE SIMILARITY

A Practical Note

Most embedding models produce normalized vectors (length = 1). When vectors are normalized, cosine similarity and dot product give identical results. Many developers use dot product by default in this case because it is computationally faster.

Always check your embedding model's documentation to see which metric it recommends. Using the wrong metric with a given model can dramatically reduce search quality — the vectors were trained to be compared in a specific way.

Leave a Comment

Your email address will not be published. Required fields are marked *