Skip to content

Commit 57a92b7

Browse files
Update the doc after the review V2
1 parent e8e60ef commit 57a92b7

1 file changed

Lines changed: 49 additions & 8 deletions

File tree

docs/distance-function.md

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ A distance metric defines how the function calculates this value. Different metr
1212

1313
```sql
1414
DISTANCE(vector_a, vector_b, metric)
15+
1516
VECTOR_DISTANCE(vector_a, vector_b, metric)
1617
```
1718

@@ -33,13 +34,25 @@ The `metric` argument cannot be `NULL`. An unsupported metric name causes an err
3334

3435
The following distance metrics are supported:
3536

36-
| Metric | Description |
37-
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
38-
| `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. |
3940
| `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+
LIMIT 5;
53+
```
54+
55+
The query returns the five closest vectors without requiring a different sort direction for `COSINE` or `DOT`.
4356

4457
## Return value
4558

@@ -109,7 +122,7 @@ SELECT id,
109122
'COSINE'
110123
) AS distance
111124
FROM documents
112-
ORDER BY distance
125+
ORDER BY distance ASC
113126
LIMIT 5;
114127
```
115128

@@ -123,10 +136,38 @@ SELECT id,
123136
'COSINE'
124137
) AS distance
125138
FROM documents
126-
ORDER BY distance
139+
ORDER BY distance ASC
127140
LIMIT 5;
128141
```
129142

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:
160+
161+
```sql
162+
SELECT id,
163+
-1 * DISTANCE(
164+
embedding,
165+
TO_VECTOR('[0.1, 0.2, 0.3]'),
166+
'DOT'
167+
) AS similarity
168+
FROM documents;
169+
```
170+
130171
## See also
131172

132173
* [The VECTOR Type](vector.md)

0 commit comments

Comments
 (0)