← Back to concepts
8 min read

Vector distance and similarity metrics

A vector comparison answers a practical question: how alike are these two numerical representations? The answer depends on the rule used to compare them.

Vector representations turn text, images, and other inputs into lists of numbers. Search and ranking systems then use a distance or similarity measure to decide which stored vectors are closest to a query.

Four common choices are cosine distance, Euclidean distance, Manhattan distance, and dot product. They use the same numbers but pay attention to different geometric properties.

The direction of the score matters. For Euclidean, Manhattan, and cosine distance, lower means closer. For cosine similarity and dot product, higher means more similar.

One pair of vectors, four views

Use the same two-dimensional vectors throughout this article:

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

The diagrams below show what each comparison measures. Production embeddings usually have hundreds or thousands of dimensions, but the calculations extend in the same way.

Euclidean distance

3.61
Euclidean distance from vector A to vector B A straight diagonal line connects A at one comma one to B at four comma three. A B straight line

Measures the shortest straight-line gap between the points.

Manhattan distance

5
Manhattan distance from vector A to vector B A right-angle route moves three units horizontally and two units vertically from A to B. A B 3 2

Adds the movement along each axis: 3 across plus 2 up.

Cosine distance

0.010
Cosine distance between vector A and vector B Both vectors start at the origin and point in nearly the same direction, creating a small angle. A B 8.1 degrees

Compares direction. Different lengths matter very little here.

Dot product

7
Dot product comparison between vector A and vector B The projection of B onto the direction of A shows how alignment and vector length combine. A B projection onto A

Combines alignment with magnitude. Longer aligned vectors score higher.

All four panels compare A = [1, 1] with B = [4, 3]. Lower means closer for the three distances; higher means more similar for the dot product.

Euclidean distance: the straight-line gap

Euclidean distance is the ordinary distance between two points. Subtract each coordinate, square the differences, add them, and take the square root.

distance(A, B)
= sqrt((4 - 1)^2 + (3 - 1)^2)
= sqrt(9 + 4)
= sqrt(13)
= 3.61

Euclidean distance pays attention to both direction and magnitude. If two vectors point in the same direction but one is much longer, they can still be far apart.

This measure is useful when absolute position and scale carry meaning. It is also common in clustering and nearest-neighbor methods. However, it can behave poorly if some dimensions use much larger numeric scales than others. Standardize feature columns when each dimension should contribute on a comparable scale. That is different from vector normalization, which rescales each whole vector to length 1.

Manhattan distance: movement along the axes

Manhattan distance adds the absolute difference in every dimension. Its name comes from moving through a street grid instead of cutting diagonally through buildings.

distance(A, B)
= abs(4 - 1) + abs(3 - 1)
= 3 + 2
= 5

In a larger vector, the same rule simply continues across every coordinate. Manhattan distance can be useful when changes happen independently along dimensions, or when axis-by-axis movement is a better model than a straight line.

Euclidean and Manhattan distance can rank candidates differently. Euclidean distance rewards a direct diagonal route. Manhattan distance charges for every coordinate change separately.

Manhattan distance is common in some tabular and operations problems, but it is rarely the first choice for dense text embeddings. Text embedding systems usually document cosine, dot product, or Euclidean-style indexing instead.

Cosine distance: the angle between directions

Cosine similarity compares the angle between two nonzero vectors. It divides the dot product by both vector lengths:

cosine_similarity(A, B)
= (1 * 4 + 1 * 3) / (sqrt(2) * 5)
= 7 / 7.071
= 0.990

cosine_distance(A, B)
= 1 - cosine_similarity(A, B)
= 0.010

A cosine similarity near 1 means the vectors point in nearly the same direction. A cosine distance near 0 therefore means they are close by angle.

This is often useful for text embeddings because direction can represent meaning while vector length may be less important. Multiplying a vector by a positive number does not change its cosine similarity:

[1, 1] and [10, 10] have cosine distance 0

Cosine comparison is undefined for a zero vector because a zero vector has no direction. A production system should handle that case explicitly instead of returning a confident score.

Dot product: alignment multiplied by length

The dot product multiplies matching coordinates and adds the results:

dot(A, B)
= (1 * 4) + (1 * 3)
= 7

Despite phrases such as “dot product distance,” the dot product itself is a similarity score, not a mathematical distance. Larger values usually rank as more similar. Some search systems convert it into a value to minimize, such as the negative dot product, but that converted score still does not have all the properties of a true distance metric.

The dot product rewards both:

  • Alignment: vectors pointing in similar directions.
  • Magnitude: longer vectors can produce larger scores.

That makes it useful when the embedding model intentionally stores information in vector length. It can also create surprising rankings when magnitude is not meaningful. The score can be negative when vectors point against each other, and it is unbounded: longer aligned vectors can keep producing larger values.

In the diagram above, “projection” means the shadow one vector casts along another vector’s direction. The dot product grows when that shadow points the same way and gets longer.

Why normalization changes the answer

Normalization rescales every nonzero vector to length 1 without changing its direction. After normalization:

  • Dot product and cosine similarity are equal.
  • Cosine ranking and dot product ranking are equal.
  • Euclidean distance produces the same nearest-neighbor ordering as cosine distance only when every vector, including the query, is unit-normalized.

For unit vectors, the relationship is:

euclidean_distance^2 = 2 * cosine_distance

This does not mean the metrics are always interchangeable. It means they become closely related under the specific condition that every vector has length 1.

Normalization makes cosine, dot product, and Euclidean rankings align The article's vectors A equals one comma one and B equals four comma three are shown before and after normalization. Before normalization, A has length 1.414 and B has length 5. After normalization, A becomes 0.707 comma 0.707 and B becomes 0.800 comma 0.600. Their dot product equals cosine similarity 0.990, cosine distance is 0.010, and squared Euclidean distance is 0.020, which equals two times the cosine distance. Using the article's A and B after normalization Original A = [1, 1] B = [4, 3] |A| = 1.414 |B| = 5.000 normalize Unit vectors A' = [0.707, 0.707] B' = [0.800, 0.600] dot(A', B') = 0.990 same as cosine similarity Relationship cos dist = 0.010 euclid^2 = 0.020 0.020 = 2 * 0.010 Rounded values from A = [1, 1] and B = [4, 3]; rankings align only because both vectors are unit length.
Normalization changes the length information, which is exactly why cosine, dot product, and Euclidean comparisons become closely related for unit vectors.

Choosing a metric

Do not pick a metric only because it is familiar or fast. Start with the embedding model’s documentation and the behavior your application needs.

  • Choose cosine distance when direction matters and magnitude should mostly be ignored.
  • Choose Euclidean distance when absolute geometric separation matters.
  • Choose Manhattan distance when independent coordinate changes or grid-like movement fit the problem.
  • Choose dot product when the model was trained for it or vector magnitude carries useful signal.

In practice, use this checklist:

  • Use the metric the embedding model documents or evaluates with.
  • Make the vector database index metric match that choice.
  • Normalize vectors consistently at index time and query time, or do not normalize at all.
  • Evaluate with recall@k on labeled queries before trusting demo results.
  • Check the database’s names: “L2” usually means Euclidean distance, “inner product” usually means dot product, and “cosine” may mean either similarity or distance depending on the API.
A practical path for choosing a vector metric A decision flow starts with the embedding model documentation. If the model names a metric, use that first. If magnitude carries signal, prefer dot product. If direction should matter more than length, prefer cosine. If absolute coordinate gaps matter, choose Euclidean or Manhattan depending on whether straight-line or axis-by-axis movement matches the task. Every path ends by evaluating the choice with real queries. Metric choice starts with model behavior Embedding model docs recommended metric? Use that metric first Magnitude signal? dot product What geometry matches the task? direction: cosine straight gap: Euclidean Evaluate with real queries math validity is not product fit
A metric is an engineering choice: start with the model's training target, then verify the ranking behavior on real examples.

Then evaluate the choice with real queries and expected results. A mathematically valid metric can still be wrong for a particular model, dataset, or product.

The key idea

Distance metrics do not discover meaning by themselves. They define what “close” means after a model has created the vectors. Euclidean distance measures a straight line, Manhattan distance adds axis-by-axis movement, cosine distance compares direction, and the dot product combines direction with length.

The best metric is the one that matches how the vectors were produced and what the application needs to rank.