Skip to content

Commit 4cb67e9

Browse files
committed
Update vectordistance
1 parent 25be8ac commit 4cb67e9

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed
Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
22
title: VectorDistance
33
titleSuffix: Azure Cosmos DB for NoSQL
4-
description: An Azure Cosmos DB for NoSQL system function that return the similarity score between two vectors.
4+
description: An Azure Cosmos DB for NoSQL system function that return the similarity score between two vectors for one or more items in a container.
55
author: jcodella
66
ms.author: jacodel
77
ms.reviewer: sidandrews
88
ms.service: azure-cosmos-db
99
ms.subservice: nosql
1010
ms.topic: reference
1111
ms.devlang: nosql
12-
ms.date: 5/5/2024
12+
ms.date: 08/06/2024
1313
ms.custom: query-reference, build-2024
1414
---
1515

@@ -25,54 +25,68 @@ Returns the similarity score between two specified vectors.
2525
## Syntax
2626

2727
```nosql
28-
VECTORDISTANCE(<vector_expr1>, <vector_expr2>, [<bool_expr>], [<obj_expr>])
28+
VectorDistance(<vector_expr1>, <vector_expr2>, [<bool_expr>], [<obj_expr>])
2929
```
3030

3131
## Arguments
3232

3333
| | Description |
3434
| --- | --- |
35-
| **`spatial_expr_1`** | An array of `float32` or smaller.|
36-
| **`spatial_expr_2`** | An array of `float32` or smaller.|
37-
| **`bool_expr`** | A boolean specifying how the computed value is used in an ORDER BY expression. If `true`, then brute force is used. A value of `false` leverages any index defined on the vector property, if it exists. Default value is `false`.|
38-
|**`obj_expr`**| A JSON formatted object literal used to specify options for the vector distance calculation. Valid items include `distanceFunction` and `dataType`.|
39-
| **`distanceFunction`** | The metric used to compute distance/similarity.
35+
| **`spatial_expr_1`** | An array of `float32` or smaller. |
36+
| **`spatial_expr_2`** | An array of `float32` or smaller. |
37+
| **`bool_expr`** | A boolean specifying how the computed value is used in an ORDER BY expression. If `true`, then brute force is used. A value of `false` uses any index defined on the vector property, if it exists. Default value is `false`. |
38+
|**`obj_expr`**| A JSON formatted object literal used to specify options for the vector distance calculation. Valid items include `distanceFunction` and `dataType`. |
39+
| **`distanceFunction`** | The metric used to compute distance/similarity. |
4040
| **`dataType`** | The data type of the vectors. `float32`, `int8`, `uint8` values. Default value is `float32`. |
4141

42+
Supported metrics for `distanceFunction` are:
4243

43-
Supported metrics for `distanceFunction` are:
44-
* [cosine](https://en.wikipedia.org/wiki/Cosine_similarity), which has values from -1 (least similar) to +1 (most similar).
45-
* [dotproduct](https://en.wikipedia.org/wiki/Dot_product), which has values from -inf (least similar) to +inf (most similar).
46-
* [euclidean](https://en.wikipedia.org/wiki/Euclidean_distance), which has values from 0 (most similar) to +inf (least similar).
44+
- [`cosine`](https://en.wikipedia.org/wiki/Cosine_similarity), which has values from `-1` (least similar) to `+1` (most similar).
45+
- [`dotproduct`](https://en.wikipedia.org/wiki/Dot_product), which has values from `-∞` (`-inf`) (least similar) to `+∞` (`+inf`) (most similar).
46+
- [`euclidean`](https://en.wikipedia.org/wiki/Euclidean_distance), which has values from `0` (most similar) to `+∞` (`+inf`) (least similar).
4747

4848
## Return types
4949

5050
Returns a numeric expression that enumerates the similarity score between two expressions.
5151

5252
## Examples
5353

54-
### With required arguments
54+
This first example includes only the required arguments.
55+
5556
```sql
56-
SELECT c.name, VectorDistance(c.vector1, c.vector2) AS SimilarityScore
57-
FROM c
58-
ORDER BY VectorDistance(c.vector1, c.vector2)
57+
SELECT VALUE {
58+
name: s.name,
59+
similarityScore: VectorDistance(s.vector1, s.vector2)
60+
}
61+
FROM
62+
source s
63+
ORDER BY
64+
VectorDistance(s.vector1, s.vector2)
5965
```
6066

61-
### With optional arguments
67+
This next example also includes optional arguments.
68+
6269
```sql
63-
SELECT c.name, VectorDistance(c.vector1, c.vector2, true, {'distanceFunction':'cosine', 'dataType':'float32',}) AS SimilarityScore
64-
FROM c
65-
ORDER BY VectorDistance(c.vector1, c.vector2)
70+
SELECT VALUE {
71+
name: s.name,
72+
similarityScore: VectorDistance(s.vector1, s.vector2, true, {'distanceFunction':'cosine', 'dataType':'float32',})
73+
}
74+
FROM
75+
source s
76+
ORDER BY
77+
VectorDistance(s.vector1, s.vector2)
6678
```
6779

6880
## Remarks
81+
6982
- This function requires enrollment in the [Azure Cosmos DB NoSQL Vector Search preview feature](../vector-search.md#enroll-in-the-vector-search-preview-feature).
7083
- This function benefits from a [vector index](../../index-policy.md#vector-indexes)
71-
- if `false` is given as the optional `bool_expr`, then the vector index defined on the path is used, if one exists. If no index is defined on the vector path, then this reverts to full scan and incurs higher RU charges and higher latency than if using a vector index.
84+
- if `false` is given as the optional `bool_expr`, then the vector index defined on the path is used, if one exists. If no index is defined on the vector path, then this function reverts to full scan and incurs higher RU charges and higher latency than if using a vector index.
7285
- When `VectorDistance` is used in an `ORDER BY` clause, no direction needs to be specified for the `ORDER BY` as the results are always sorted in order of most similar (first) to least similar (last) based on the similarity metric used.
7386
- The result is expressed as a similarity score.
7487

7588
## Related content
89+
7690
- [System functions](system-functions.yml)
7791
- [Setup Azure Cosmos DB for NoSQL for vector search](../vector-search.md).
7892
- [vector index](../../index-policy.md#vector-indexes)

0 commit comments

Comments
 (0)