Was this helpful?
VECTOR_DISTANCE
VECTOR_DISTANCE(<v1>, <v2>, <metric>)
The VECTOR_DISTANCE() function returns the distance between two Vectors according to a specified distance metric. The distance is returned as a double-precision FLOAT.
Both Vectors must have the same dimension and coordinate type. If either Vector argument is NULL, the result is NULL.
The supported distance metrics are EUCLIDEAN, EUCLIDEAN_SQUARED, MANHATTAN, COSINE, DOT and HAMMING, described in the table below.
Distance Metric
Description
Distance between Vectors
v1=[x1, y1, z1] and v2=[x2, y2, z2]
EUCLIDEAN
The Pythagorean distance, generalized to N-dimensional space. This is the square root of the sum of the squares of the coordinate-wise differences between the vectors.
sqrt((x1 - x2)² + (y1 - y2)² + (z1 - z2)²)
EUCLIDEAN_SQUARED
It is the same as the Euclidean distance, but without the square-root step.
 
 
(x1 - x2)² + (y1 - y2)² + (z1 - z2)²
MANHATTAN
The “taxicab” or L1 distance, equal to the sum of the absolute coordinate-wise differences.
 
|x1 - x2| + |y1 - y2| + |z1 - z2|
COSINE
Derived from the cosine of the angle between the two vectors, calculated by the dot product of the vectors divided by the product of their lengths.
To produce a useful “distance” metric from this, where lesser distance values represent more similar vectors, the cosine of the angle is subtracted from 1 to produce a result between 0 and 2.
Note:  If either of the two Vector arguments has all-zero coordinates, the COSINE metric is undefined and will return a division by zero error.
Let |v| = sqrt(x² + y² + z²)
 
Let v1 . v2 = x1x2 + y1y2 + z1z2
 
Distance = 1.0 - (v1 . v2) / (|v1||v2|)
 
DOT
The dot product between the two vectors, negated so that similar vectors give lesser distance results.
 
-(x1x2 + y1y2 + z1z2)
HAMMING
The number of coordinates in which one vector differs from another. This returns a whole number ranging from 0 (if the vectors are equal) and the dimension of the vector (if the vectors differ in all coordinates).
This is a count of how many coordinates are different between the vectors.
For example, suppose v1=[1, 2, 3] and v2=[1, 3, 5] .The result is 2, because the y and z coordinates are different between the vectors but the x coordinates are the same.
Last modified date: 09/11/2026