You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`EUCLIDEAN`| Measures the straight-line distance between two vectors (L2 distance). The result is non-negative. A value of `0` means the vectors are identical. Lower values indicate greater similarity.|
37
+
| Metric | Description |
38
+
| --- | --- |
39
+
|`EUCLIDEAN`| Measures the straight-line distance between two vectors (L2 distance). The result is non-negative. A value of `0` means the vectors are identical. Lower values indicate greater similarity. |
39
40
|`EUCLIDEAN_SQUARED`| Calculates the square of the Euclidean distance without the square root calculation. Use this metric when you need to rank vectors by distance but do not need the actual Euclidean distance. The result is non-negative. A value of `0` means the vectors are identical. Lower values indicate greater similarity. |
40
-
|`COSINE`| Measures the difference in direction between two vectors rather than their magnitude. For real-valued vectors, the result ranges from `0` to `2`. A value of `0` indicates the same direction, while `2` indicates opposite directions. |
41
-
|`DOT`| Calculates the negative inner product (dot product). The dot product multiplies corresponding elements from the two vectors and adds the results. Lower returned values indicate greater similarity. |
42
-
|`MANHATTAN`| Adds the absolute differences between corresponding vector elements (L1 distance). The result is non-negative. A value of `0` means the vectors are identical. Lower values indicate greater similarity. |
41
+
|`COSINE`| Calculates cosine distance as `1 - cosine similarity`. Cosine similarity ranges from `-1` to `1`, where `1` indicates the same direction, `0` indicates orthogonal vectors, and `-1` indicates opposite directions. Therefore, cosine distance ranges from `0` to `2`, where `0` indicates the same direction, `1` indicates orthogonal vectors, and `2` indicates opposite directions. |
42
+
|`DOT`| Calculates the negative inner product (dot product). The dot product multiplies corresponding elements from the two vectors and adds the results. `DISTANCE()` multiplies the inner product by `-1` so that lower returned values represent more closely aligned vectors. |
43
+
|`MANHATTAN`| Adds the absolute differences between corresponding vector elements (L1 distance). The result is non-negative. A value of `0` means the vectors are identical. Lower values indicate greater similarity. |
44
+
45
+
The `COSINE` and `DOT` metrics transform their underlying similarity values into distance values so that a lower value consistently represents a closer match across all supported metrics. As a result, you can sort distance values in ascending order regardless of the selected metric:
46
+
47
+
```sql
48
+
SELECT id,
49
+
DISTANCE(embedding, TO_VECTOR('[0.1, 0.2, 0.3]'), 'COSINE') AS distance
50
+
FROM products
51
+
ORDER BY distance ASC
52
+
LIMIT5;
53
+
```
54
+
55
+
The query returns the five closest vectors without requiring a different sort direction for `COSINE` or `DOT`.
43
56
44
57
## Return value
45
58
@@ -109,7 +122,7 @@ SELECT id,
109
122
'COSINE'
110
123
) AS distance
111
124
FROM documents
112
-
ORDER BY distance
125
+
ORDER BY distanceASC
113
126
LIMIT5;
114
127
```
115
128
@@ -123,10 +136,38 @@ SELECT id,
123
136
'COSINE'
124
137
) AS distance
125
138
FROM documents
126
-
ORDER BY distance
139
+
ORDER BY distanceASC
127
140
LIMIT5;
128
141
```
129
142
143
+
### Calculate similarity values
144
+
145
+
`DISTANCE()` returns distance-oriented values so that lower values consistently represent closer vectors. You can convert the result back to the underlying similarity value for the `COSINE` and `DOT` metrics.
146
+
147
+
For `COSINE`, subtract the distance from `1` to calculate cosine similarity:
148
+
149
+
```sql
150
+
SELECT id,
151
+
1- DISTANCE(
152
+
embedding,
153
+
TO_VECTOR('[0.1, 0.2, 0.3]'),
154
+
'COSINE'
155
+
) AS similarity
156
+
FROM documents;
157
+
```
158
+
159
+
For `DOT`, multiply the returned distance by `-1` to calculate the inner product:
0 commit comments