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
Copy file name to clipboardExpand all lines: docs/distance-function.md
+11-12Lines changed: 11 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
# DISTANCE() Function
2
2
3
-
The `DISTANCE()` function calculates the distance between two `VECTOR` values. Use this function to compare vectors for similarity search and other vector-based operations.
3
+
The `DISTANCE()` function computes the distance between two `VECTOR` valuesto enable similarity searches and other vector-based operations.
4
4
5
-
A vector is an ordered list of numeric values, such as `[1, 2, 3]`. The distance between two vectors is a numeric value that indicates how different the vectors are.
5
+
A vector is an ordered list of numeric values, such as `[1, 2, 3]`. The distance between two vectors is a numeric value that indicates how similar the vectors are.
6
6
7
7
A distance metric defines how the function calculates this value. Different metrics compare properties such as vector values, direction, or magnitude. In general, a smaller distance indicates greater similarity.
8
8
@@ -21,7 +21,8 @@ Both functions require exactly three arguments.
21
21
22
22
`DISTANCE()` accepts the following arguments:
23
23
24
-
*`vector_a` and `vector_b` specify the vectors to compare. Both arguments must be `VECTOR` values or binary strings that represent float vectors. Other data types cause an error. The vectors must have the same number of dimensions. A dimension mismatch causes an error.
24
+
*`vector_a` and `vector_b` specify the vectors to compare. Both arguments must be values of `VECTOR` data type or binary strings that represent float vectors. Other data types cause an error. The vectors must have the same dimension. A dimension mismatch causes an error.
25
+
25
26
*`metric` specifies the distance metric. Use a constant string or hex literal that resolves to a supported metric. Metric names are case-insensitive. You cannot use a column reference or a computed expression, such as `CONCAT()`, for this argument.
26
27
27
28
For a binary-string vector, the byte length must be a multiple of 4 because each vector element is a 4-byte single-precision floating-point value. An invalid byte length causes an error.
@@ -70,15 +71,13 @@ The result is a `DOUBLE` value:
The `VECTOR` data type stores an ordered list of numeric values, called a vector, in a table column. For example, `[0.1, 0.2, 0.3]` represents a vector with three values.
3
+
The `VECTOR` data type stores an ordered list of numeric values, called a vector, in a table column. For example, `[0.1, 0.3, 0.2]` represents a vector with three elements.
4
4
5
5
Vectors can represent numerical features, such as embeddings used for similarity search and machine learning. Each element in a `VECTOR` value uses a single-precision floating-point number.
6
6
@@ -10,9 +10,9 @@ Vectors can represent numerical features, such as embeddings used for similarity
10
10
VECTOR(N)
11
11
```
12
12
13
-
`N` specifies the number of elements, or dimensions, in the vector. All values stored in the column must have the same number of dimensions.
13
+
`N` specifies the dimension of the `VECTOR` column and defines the maximum number of elements that a stored vector can contain.
14
14
15
-
For example, the following statement creates an `embedding` column that stores three-dimensional vectors:
15
+
For example, the following statement creates an `embedding` column with a dimension of three:
16
16
17
17
```sql
18
18
CREATETABLEdocuments (
@@ -22,24 +22,26 @@ CREATE TABLE documents (
22
22
);
23
23
```
24
24
25
-
A `VECTOR(3)` value contains exactly three elements, such as `[0.1, 0.2, 0.3]`. A vector with two or four elements does not match the column dimension.
25
+
A `VECTOR(3)` column can store a vector with up to three elements. For example, it accepts vectors with two or three elements but rejects a vector with four elements.
26
+
27
+
Using the same number of elements as the declared dimension is strongly recommended.
26
28
27
29
## Store vector values
28
30
29
31
Use `TO_VECTOR()` to convert the string representation of a vector to a `VECTOR` value.
30
32
31
-
The following statement inserts a three-dimensional vector into the `embedding` column:
33
+
The following statement inserts a three-element vector into the `embedding` column:
32
34
33
35
```sql
34
36
INSERT INTO documents (id, title, embedding)
35
37
VALUES (
36
38
1,
37
39
'Example document',
38
-
TO_VECTOR('[0.1, 0.2, 0.3]')
40
+
TO_VECTOR('[0.1, 0.3, 0.2]')
39
41
);
40
42
```
41
43
42
-
The number of elements passed to `TO_VECTOR()` must match the dimension of the `VECTOR` column.
44
+
The number of elements in the vector must not exceed the dimension specified for the `VECTOR` column.
43
45
44
46
## Retrieve vector values
45
47
@@ -54,19 +56,21 @@ FROM documents;
54
56
55
57
Vector applications often compare vectors to determine how similar they are. The result of a comparison is a numeric distance between the vectors.
56
58
57
-
Use `DISTANCE()` to calculate this distance with a supported distance metric. For example:
59
+
Use `DISTANCE()` to calculate the distance between two vectors with a supported distance metric. The two vector values passed to `DISTANCE()` must contain the same number of elements. A mismatch causes an error.
60
+
61
+
For example:
58
62
59
63
```sql
60
64
SELECT DISTANCE(
61
65
embedding,
62
-
TO_VECTOR('[0.1, 0.2, 0.3]'),
66
+
TO_VECTOR('[0.1, 0.3, 0.2]'),
63
67
'COSINE'
64
68
) AS distance
65
69
FROM documents;
66
70
```
67
71
68
-
Different distance metrics compare vectors in different ways. For details about the supported metrics, arguments, and return values, see [DISTANCE() Function](distance-function.md).
72
+
Different distance metrics compare vectors in different ways. For details about supported metrics, arguments, and return values, see [DISTANCE() Function](distance-function.md).
0 commit comments