Skip to content

Commit 8388482

Browse files
committed
Acrolinx fixes
1 parent e8abef9 commit 8388482

File tree

1 file changed

+65
-54
lines changed

1 file changed

+65
-54
lines changed
Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,97 @@
11
---
22
title: Index and query vector data in JavaScript
33
titleSuffix: Azure Cosmos DB for NoSQL
4-
description: Add vector data Azure Cosmos DB for NoSQL and then query the data efficiently in your JavaScript application.
4+
description: Add vector data Azure Cosmos DB for NoSQL and then query the data efficiently in your JavaScript application
55
author: jcodella
66
ms.author: jacodel
7-
ms.reviewer: sidandrews
87
ms.service: azure-cosmos-db
98
ms.subservice: nosql
109
ms.topic: how-to
11-
ms.date: 08/01/2023
10+
ms.date: 08/08/2024
1211
ms.custom: query-reference, build-2024, devx-track-js
1312
---
1413

15-
# 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
1615

1716
[!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:
1917

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:
2719

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.
2833

2934
## Prerequisites
35+
3036
- An existing Azure Cosmos DB for NoSQL account.
3137
- If you don't have an Azure subscription, [Try Azure Cosmos DB for NoSQL free](https://cosmos.azure.com/try/).
3238
- If you have an existing Azure subscription, [create a new Azure Cosmos DB for NoSQL account](how-to-create-account.md).
3339
- Latest version of the Azure Cosmos DB [JavaScript](sdk-nodejs.md) SDK (Version 4.1.0 or later)
3440

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:
3744

3845
1. Navigate to your Azure Cosmos DB for NoSQL resource page.
39-
40-
2. Select the "Features" pane under the "Settings" menu item.
4146

42-
3. Select for “Vector Search in Azure Cosmos DB for NoSQL”.
47+
1. Select the "Features" pane under the "Settings" menu item.
4348

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."
4550

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.
4752

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.
5254

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.
5457
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
5659

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.
6061

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:
6269

6370
```json
6471
{
65-
"title": "book-title",
66-
"author": "book-author",
67-
"isbn": "book-isbn",
68-
"description": "book-description",
69-
"contentVector": [2, -1, 4, 3, 5, -2, 5, -7, 3, 1],
70-
"coverImageVector": [0.33, -0.52, 0.45, -0.67, 0.89, -0.34, 0.86, -0.78]
71-
}
72+
"title": "book-title",
73+
"author": "book-author",
74+
"isbn": "book-isbn",
75+
"description": "book-description",
76+
"contentVector": [2, -1, 4, 3, 5, -2, 5, -7, 3, 1],
77+
"coverImageVector": [0.33, -0.52, 0.45, -0.67, 0.89, -0.34, 0.86, -0.78]
78+
}
7279
```
7380

74-
## 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+
7685
The following information is included in the contained vector policy:
7786

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`) |
8293

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:
8495

8596
```javascript
8697
const vectorEmbeddingPolicy: VectorEmbeddingPolicy = {
@@ -99,10 +110,11 @@ const vectorEmbeddingPolicy: VectorEmbeddingPolicy = {
99110
},
100111
],
101112
};
102-
```
113+
```
103114

104-
## Creating a vector index in the indexing policy
105-
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:
106118

107119
```javascript
108120
const indexingPolicy: IndexingPolicy = {
@@ -138,21 +150,20 @@ const containerName = "vector embedding container";
138150
});
139151
```
140152

141-
142153
> [!IMPORTANT]
143154
> 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.
144155
145-
## Running vector similarity search query
156+
## Run a vector similarity search query
146157

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:
148159

149160
```sql
150-
SELECT c.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+
SELECT c.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])
153164
```
154165

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:
156167

157168
```javascript
158169
const { resources } = await container.items
@@ -166,8 +177,8 @@ for (const item of resources) {
166177
}
167178
```
168179

180+
## Related content
169181

170-
## Next steps
171182
- [VectorDistance system function](query/vectordistance.md)
172183
- [Vector indexing](../index-policy.md)
173184
- [Setup Azure Cosmos DB for NoSQL for vector search](../vector-search.md).

0 commit comments

Comments
 (0)