Euclidean distance
L2 distance
The straight-line distance between two vectors: square the difference in each dimension, add them up and take the square root.
In DocSlurpGlossary entry
On this page
Euclidean distance is the length of the straight line between the tips of two vectors:
d(a, b) = √( (a₁ − b₁)² + (a₂ − b₂)² + … )
Smaller means more similar. Many vector indexes compute the squared distance and skip the square root, since it doesn’t change the order.
The link to cosine
For unit-length vectors, squared distance and cosine similarity are tied exactly:
d² = 2 − 2 × cos
Check it with a = (1, 0) and b = (0.6, 0.8), both length 1. Their cosine is 0.6. The squared distance is (1 − 0.6)² + (0 − 0.8)² = 0.16 + 0.64 = 0.8, and 2 − 2 × 0.6 = 0.8. Because one is a decreasing function of the other, sorting by smallest distance and sorting by largest cosine give the same results. An index built for L2 distance serves normalized embeddings perfectly well.
To report a similarity instead of a distance, a common transformation is 1 / (1 + d²). For the pair above that is 1 / 1.8 = 0.556. It sorts correctly but is not a cosine; the cosine can be recovered as 1 − (1/score − 1) / 2.
The gotcha: without normalization, distance and cosine disagree. With q = (1, 0), the passage (3, 0) points exactly at q (cosine 1) but sits 2 away, while (0.8, 0.6) is at a 37° angle (cosine 0.8) and only 0.632 away. Euclidean distance prefers the second, cosine the first. Know which your model was trained for, normalize if it expects cosine, and don’t compare distances across differently scaled vectors.
In DocSlurp
In DocSlurp
The LanceDB vector index is built with distanceType: "l2" (core/worker/src/stages/lancedb-maintenance.ts). LanceDB reports squared L2 distance d and the search adapter turns it into 1 / (1 + d) (core/server/src/search/lancedb.ts); since vectors are unit length, ranking matches cosine.