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
# Index and query vectors in Azure Cosmos DB for NoSQL in JavaScript.
14
+
# Index and query vectors in Azure Cosmos DB for NoSQL in JavaScript
16
15
17
16
[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)]
18
-
Vector search in Azure Cosmos DB for NoSQL is currently a preview feature. You're required to register for the preview before use. This article covers the following steps:
19
17
20
-
1. Registering for the preview of Vector Search in Azure Cosmos DB for NoSQL
21
-
2. Setting up the Azure Cosmos DB container for vector search
22
-
3. Authoring vector embedding policy
23
-
4. Adding vector indexes to the container indexing policy
24
-
5. Creating a container with vector indexes and vector embedding policy
25
-
6. Performing a vector search on the stored data.
26
-
7. This guide walks through the process of creating vector data, indexing the data, and then querying the data in a container.
18
+
The Azure Cosmos DB for NoSQL vector search feature is in preview. Before you use this feature, you must first register for the preview. This article covers the following steps:
27
19
20
+
1. Registering for the preview of Vector Search in Azure Cosmos DB for NoSQL
21
+
22
+
1. Setting up the Azure Cosmos DB container for vector search
23
+
24
+
1. Authoring vector embedding policy
25
+
26
+
1. Adding vector indexes to the container indexing policy
27
+
28
+
1. Creating a container with vector indexes and vector embedding policy
29
+
30
+
1. Performing a vector search on the stored data
31
+
32
+
This guide walks through the process of creating vector data, indexing the data, and then querying the data in a container.
28
33
29
34
## Prerequisites
35
+
30
36
- An existing Azure Cosmos DB for NoSQL account.
31
37
- If you don't have an Azure subscription, [Try Azure Cosmos DB for NoSQL free](https://cosmos.azure.com/try/).
32
38
- If you have an existing Azure subscription, [create a new Azure Cosmos DB for NoSQL account](how-to-create-account.md).
33
39
- Latest version of the Azure Cosmos DB [JavaScript](sdk-nodejs.md) SDK (Version 4.1.0 or later)
34
40
35
-
## Registering for the preview
36
-
Vector search for Azure Cosmos DB for NoSQL requires preview feature registration. Follow the below steps to register:
41
+
## Register for the preview
42
+
43
+
Vector search for Azure Cosmos DB for NoSQL requires preview feature registration. Follow the below steps to register:
37
44
38
45
1. Navigate to your Azure Cosmos DB for NoSQL resource page.
39
-
40
-
2. Select the "Features" pane under the "Settings" menu item.
41
46
42
-
3. Select for “Vector Search in Azure Cosmos DB for NoSQL”.
47
+
1. Select the "Features" pane under the "Settings" menu item.
43
48
44
-
5. Read the description of the feature to confirm you want to enroll in the preview.
49
+
1. Select for "Vector Search in Azure Cosmos DB for NoSQL."
45
50
46
-
6. Select "Enable" to enroll in the preview.
51
+
1. Read the description of the feature to confirm you want to enroll in the preview.
47
52
48
-
> [!NOTE]
49
-
> The registration request will be autoapproved, however it may take several minutes to take effect.
50
-
51
-
## Understanding the steps involved in vector search
53
+
1. Select "Enable" to enroll in the preview.
52
54
53
-
The following steps assume that you know how to [setup a Cosmos DB NoSQL account and create a database](quickstart-portal.md). The vector search feature is currently only supported on new containers, not existing container. You need to create a new container and then specify the container-level vector embedding policy and the vector indexing policy at the time of creation.
55
+
> [!NOTE]
56
+
> The registration request will be autoapproved, however it may take several minutes to take effect.
54
57
55
-
Let’s take an example of creating a database for an internet-based bookstore and you're storing Title, Author, ISBN, and Description for each book. We also define two properties to contain vector embeddings. The first is the “contentVector” property, which contains [text embeddings](../../ai-services/openai/concepts/models.md#embeddings) generated from the text content of the book (for example, concatenating the “title” “author” “isbn” and “description” properties before creating the embedding). The second is “coverImageVector”, which is generated from [images of the book’s cover](../../ai-services/computer-vision/concept-image-retrieval.md).
58
+
## Understand the steps involved in vector search
56
59
57
-
1. Create and store vector embeddings for the fields on which you want to perform vector search.
58
-
2. Specify the vector embedding paths in the vector embedding policy.
59
-
3. Include any desired vector indexes in the indexing policy for the container.
60
+
The following steps assume that you know how to [setup a Cosmos DB NoSQL account and create a database](quickstart-portal.md). The vector search feature is currently only supported on new containers, not existing container. You need to create a new container and then specify the container-level vector embedding policy and the vector indexing policy at the time of creation.
60
61
61
-
For subsequent sections of this article, we consider the below structure for the items stored in our container:
62
+
Let’s take an example of creating a database for an internet-based bookstore and you're storing Title, Author, ISBN, and Description for each book. We also define two properties to contain vector embeddings. The first is the "contentVector" property, which contains [text embeddings](../../ai-services/openai/concepts/models.md#embeddings) generated from the text content of the book (for example, concatenating the "title" "author" "isbn" and "description" properties before creating the embedding). The second is "coverImageVector," which is generated from [images of the book’s cover](../../ai-services/computer-vision/concept-image-retrieval.md).
63
+
64
+
1. Create and store vector embeddings for the fields on which you want to perform vector search.
65
+
2. Specify the vector embedding paths in the vector embedding policy.
66
+
3. Include any desired vector indexes in the indexing policy for the container.
67
+
68
+
For subsequent sections of this article, we consider this structure for the items stored in our container:
## Creating a vector embedding policy for your container.
75
-
Next, you need to define a container vector policy. This policy provides information that is used to inform the Azure Cosmos DB query engine how to handle vector properties in the VectorDistance system functions. This also informs the vector indexing policy of necessary information, should you choose to specify one.
81
+
## Create a vector embedding policy for your container
82
+
83
+
Next, you need to define a container vector policy. This policy provides information that is used to inform the Azure Cosmos DB query engine how to handle vector properties in the VectorDistance system functions. This policy also informs the vector indexing policy of necessary information, should you choose to specify one.
84
+
76
85
The following information is included in the contained vector policy:
77
86
78
-
* “path”: The property path that contains vectors
79
-
* “datatype”: The type of the elements of the vector (default Float32)
80
-
* “dimensions”: The length of each vector in the path (default 1536)
81
-
* “distanceFunction”: The metric used to compute distance/similarity (default Cosine)
87
+
|| Description |
88
+
| --- | --- |
89
+
|**`path`**| The property path that contains vectors |
90
+
|**`datatype`**| The type of the elements of the vector (default `Float32`) |
91
+
|**`dimensions`**| The length of each vector in the path (default `1536`) |
92
+
|**`distanceFunction`**| The metric used to compute distance/similarity (default `Cosine`) |
82
93
83
-
For our example with book details, the vector policy can look like the example JSON:
94
+
For our example with book details, the vector policy can look like the example JSON:
Once the vector embedding paths are decided, vector indexes need to be added to the indexing policy. Currently, the vector search feature for Azure Cosmos DB for NoSQL is supported only on new containers so you need to apply the vector policy during the time of container creation and it can’t be modified later. For this example, the indexing policy would look like this:
115
+
## Create a vector index in the indexing policy
116
+
117
+
Once the vector embedding paths are decided, vector indexes need to be added to the indexing policy. You must apply the vector policy during the time of container creation and it can’t be modified later. For this example, the indexing policy would look like this:
> Currently vector search in Azure Cosmos DB for NoSQL is supported on new containers only. You need to set both the container vector policy and any vector indexing policy during the time of container creation as it can’t be modified later. Both policies will be modifiable in a future improvement to the preview feature.
144
155
145
-
## Running vector similarity search query
156
+
## Run a vector similarity search query
146
157
147
-
Once you create a container with the desired vector policy, and insert vector data into the container, you can conduct a vector search using the [Vector Distance](query/vectordistance.md) system function in a query. Suppose you want to search for books about food recipes by looking at the description, you first need to get the embeddings for your query text. In this case, you might want to generate embeddings for the query text – “food recipe”. Once you have the embedding for your search query, you can use it in the VectorDistance function in the vector search query and get all the items that are similar to your query as shown here:
158
+
Once you create a container with the desired vector policy, and insert vector data into the container, you can conduct a vector search using the [Vector Distance](query/vectordistance.md) system function in a query. Suppose you want to search for books about food recipes by looking at the description. You first need to get the embeddings for your query text. In this case, you might want to generate embeddings for the query text – "food recipe." Once you have the embedding for your search query, you can use it in the VectorDistance function in the vector search query and get all the items that are similar to your query as shown here:
148
159
149
160
```sql
150
-
SELECTc.title, VectorDistance(c.contentVector, [1,2,3,4,5,6,7,8,9,10]) AS SimilarityScore
151
-
FROM c
152
-
ORDER BY VectorDistance(c.contentVector, [1,2,3,4,5,6,7,8,9,10])
161
+
SELECTc.title, VectorDistance(c.contentVector, [1,2,3,4,5,6,7,8,9,10]) AS SimilarityScore
162
+
FROM c
163
+
ORDER BY VectorDistance(c.contentVector, [1,2,3,4,5,6,7,8,9,10])
153
164
```
154
165
155
-
This query retrieves the book titles along with similarity scores with respect to your query. Here is an example in JavaScript:
166
+
This query retrieves the book titles along with similarity scores with respect to your query. Here's an example in JavaScript:
156
167
157
168
```javascript
158
169
const { resources } =awaitcontainer.items
@@ -166,8 +177,8 @@ for (const item of resources) {
166
177
}
167
178
```
168
179
180
+
## Related content
169
181
170
-
## Next steps
171
182
-[VectorDistance system function](query/vectordistance.md)
172
183
-[Vector indexing](../index-policy.md)
173
184
-[Setup Azure Cosmos DB for NoSQL for vector search](../vector-search.md).
0 commit comments