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
@@ -25,9 +25,72 @@ Vector search is a method that helps you find similar items based on their data
25
25
26
26
By integrating vector search capabilities natively, you can unlock the full potential of your data in applications that are built on top of the [OpenAI API](../../../ai-services/openai/concepts/understand-embeddings.md). You can also create custom-built solutions that use vector embeddings.
27
27
28
-
## Use the createIndexes template to create a vector index
28
+
## Create a vector index
29
+
To perform vector similiarity search over vector properties in your documents, you'll have to first create a _vector index_.
29
30
30
-
To create a vector index, use the following `createIndexes` template:
31
+
### Create a vector index using HNSW
32
+
33
+
You can create (Hierarchical Navigable Small World) indexes on M40 cluster tiers and higher. To use create the HSNW index, you need to create a vector index with the `"kind"` parameter set to `"vector-hnsw"` following the template below:
34
+
35
+
```javascript
36
+
{
37
+
"createIndexes":"<collection_name>",
38
+
"indexes": [
39
+
{
40
+
"name":"<index_name>",
41
+
"key": {
42
+
"<path_to_property>":"cosmosSearch"
43
+
},
44
+
"cosmosSearchOptions": {
45
+
"kind":"vector-hnsw",
46
+
"m":<integer_value>,
47
+
"efConstruction":<integer_value>,
48
+
"similarity":"<string_value>",
49
+
"dimensions":<integer_value>
50
+
}
51
+
}
52
+
]
53
+
}
54
+
```
55
+
56
+
|Field |Type |Description |
57
+
|---------|---------|---------|
58
+
|`kind`| string | Type of vector index to create. Type of vector index to create. Primarily, `vector-ivf` is supported. `vector-hnsw` is available as a preview feature that requires enablement via [Azure Feature Enablement Control](../../../azure-resource-manager/management/preview-features.md).|
59
+
|`m`|integer |The max number of connections per layer (`16` by default, minimum value is `2`, maximum value is `100`). Higher m is suitable for datasets with high dimensionality and/or high accuracy requirements. |
60
+
|`efConstruction`|integer |the size of the dynamic candidate list for constructing the graph (`64` by default, minimum value is `4`, maximum value is `1000`). Higher `efConstruction` will result in better index quality and higher accuracy, but it will also increase the time required to build the index. `efConstruction` has to be at least `2 * m`|
61
+
|`similarity`|string |Similarity metric to use with the index. Possible options are `COS` (cosine distance), `L2` (Euclidean distance), and `IP` (inner product). |
62
+
|`dimensions`|integer |Number of dimensions for vector similarity. The maximum number of supported dimensions is `2000`. |
63
+
64
+
65
+
> [!WARNING]
66
+
> Using the HSNW vector index (preview) with large datasets can result in resource running out of memory, or reducing the performance of other operations running on your database. To reduce the chance of this happening, we recommend to:
67
+
> - Only use HNSW indexes on a cluster tier of M40 or higher.
68
+
> - Scale to a higher cluster tier or reduce the size of the database if your encounter errors.
69
+
70
+
### Perform a vector search with HNSW
71
+
To perform a vector search, use the `$search` aggregation pipeline stage the query with the `cosmosSearch` operator.
72
+
```javascript
73
+
{
74
+
"$search": {
75
+
"cosmosSearch": {
76
+
"vector":<query_vector>,
77
+
"path":"<path_to_property>",
78
+
"k":<num_results_to_return>,
79
+
"efSearch":<integer_value>
80
+
},
81
+
}
82
+
}
83
+
}
84
+
85
+
```
86
+
|Field |Type |Description |
87
+
|---------|---------|---------|
88
+
|`efSearch`|integer |The size of the dynamic candidate list for search (`40` by default). A higher value provides better recall at the cost of speed. |
89
+
|`k`|integer |The number of results to return. it should be less than or equal to `efSearch`|
90
+
91
+
### Create an vector index using IVF
92
+
93
+
To create a vector index using the IVF (Inverted File) algorithm, use the following `createIndexes` template and set the `"kind"` paramter to `"vector-ivf"`:
31
94
32
95
```json
33
96
{
@@ -65,40 +128,68 @@ To create a vector index, use the following `createIndexes` template:
65
128
>
66
129
> If you're experimenting with a new scenario or creating a small demo, you can start with `numLists` set to `1` to perform a brute-force search across all vectors. This should provide you with the most accurate results from the vector search, however be aware that the search speed and latency will be slow. After your initial setup, you should go ahead and tune the `numLists` parameter using the above guidance.
67
130
131
+
### Perform a vector search with IVF
132
+
133
+
To perform a vector search, use the `$search` aggregation pipeline stage in a MongoDB query. To use the `cosmosSearch` index, use the new `cosmosSearch` operator.
To retrieve the similarity score (`searchScore`) along with the documents found by the vector search, use the `$project` operator to include `searchScore` and rename it as `<custom_name_for_similarity_score>` in the results. Then the document is also projected as nested object. Note that the similarity score is calculated using the metric defined in the vector index.
154
+
155
+
68
156
> [!IMPORTANT]
69
157
> Vectors must be a `number[]` to be indexed. Using another type, such as `double[]`, prevents the document from being indexed. Non-indexed documents won't be returned in the result of a vector search.
70
158
71
159
## Examples
72
160
73
161
The following examples show you how to index vectors, add documents that have vector properties, perform a vector search, and retrieve the index configuration.
74
162
75
-
### Create a vector index
163
+
```javascript
164
+
use test;
165
+
166
+
db.createCollection("exampleCollection");
76
167
77
168
```javascript
78
169
use test;
79
170
80
171
db.createCollection("exampleCollection");
81
172
82
-
db.runCommand({
83
-
createIndexes:'exampleCollection',
84
-
indexes: [
85
-
{
86
-
name:'vectorSearchIndex',
87
-
key: {
88
-
"vectorContent":"cosmosSearch"
89
-
},
90
-
cosmosSearchOptions: {
91
-
kind:'vector-ivf',
92
-
numLists:3,
93
-
similarity:'COS',
94
-
dimensions:3
95
-
}
96
-
}
97
-
]
173
+
db.runCommand({
174
+
"createIndexes": "exampleCollection",
175
+
"indexes": [
176
+
{
177
+
"name": "VectorSearchIndex",
178
+
"key": {
179
+
"contentVector": "cosmosSearch"
180
+
},
181
+
"cosmosSearchOptions": {
182
+
"kind": "vector-hnsw",
183
+
"m": 16,
184
+
"efConstruction": 64,
185
+
"similarity": "COS",
186
+
"dimensions": 3
187
+
}
188
+
}
189
+
]
98
190
});
99
191
```
100
-
101
-
This command creates a `vector-ivf` index against the `vectorContent` property in the documents that are stored in the specified collection, `exampleCollection`. The `cosmosSearchOptions` property specifies the parameters for the IVF vector index. If your document has the vector stored in a nested property, you can set this property by using a dot notation path. For example, you might use `text.vectorContent` if `vectorContent` is a subproperty of `text`.
192
+
This command creates an HNSW index against the `contentVector` property in the documents that are stored in the specified collection, `exampleCollection`. The`cosmosSearchOptions` property specifies the parameters for the HNSW vector index. If your document has the vector stored in a nested property, you can set this property by using a dot notation path. For example, you might use `text.contentVector`if`contentVector` is a subproperty of`text`.
To perform a vector search, use the `$search` aggregation pipeline stage in a MongoDB query. To use the `cosmosSearch` index, use the new `cosmosSearch` operator.
To retrieve the similarity score (`searchScore`) along with the documents found by the vector search, use the `$project` operator to include `searchScore` and rename it as `<custom_name_for_similarity_score>` in the results. Then the document is also projected as nested object. Note that the similarity score is calculated using the metric defined in the vector index.
139
-
140
-
### Query vectors and vector distances (aka similarity scores) using $search"
141
-
142
-
Continuing with the last example, create another vector, `queryVector`. Vector search measures the distance between `queryVector` and the vectors in the `vectorContent` path of your documents. You can set the number of results that the search returns by setting the parameter `k`, which is set to `2` here. You can also set `nProbes`, which is an integer that controls the number of nearby clusters that are inspected in each search. A higher value may improve accuracy, however the search will be slower as a result. This is an optional parameter with a default value of 1 and cannot be larger than the `numLists` value specified in the vector index.
209
+
Continuing with the last example, create another vector, `queryVector`. Vector search measures the distance between `queryVector` and the vectors in the `contentVector` path of your documents. You can set the number of results that the search returns by setting the parameter `k`, which is set to `2` here. You can also set `efSearch`, which is an integer that controls the size of the candidate vector list. A higher value may improve accuracy, however the search will be slower as a result. This is an optional parameter with a default value of 40.
143
210
144
211
145
212
```javascript
146
213
const queryVector = [0.52, 0.28, 0.12];
147
214
db.exampleCollection.aggregate([
148
215
{
149
-
$search: {
150
-
"cosmosSearch": {
151
-
"vector": queryVector,
152
-
"path":"vectorContent",
153
-
"k":2
154
-
},
155
-
"returnStoredSource":true }},
156
-
{
157
-
"$project": { "similarityScore": {
158
-
"$meta":"searchScore" },
159
-
"document":"$$ROOT"
160
-
}
216
+
"$search": {
217
+
"cosmosSearch": {
218
+
"vector": "queryVector",
219
+
"path": "contentVector",
220
+
"k": 2,
221
+
"efSearch": 40
222
+
},
223
+
}
161
224
}
225
+
}
162
226
]);
163
227
```
164
228
@@ -202,11 +266,12 @@ In this example, `vectorIndex` is returned with all the `cosmosSearch` parameter
@@ -215,74 +280,57 @@ In this example, `vectorIndex` is returned with all the `cosmosSearch` parameter
215
280
]
216
281
```
217
282
218
-
## HNSW vector index (preview)
219
-
220
-
HNSW stands for Hierarchical Navigable Small World, a graph-based data structure that partitions vectors into clusters and subclusters. With HNSW, you can perform fast approximate nearest neighbor search at higher speeds with greater accuracy.
283
+
## Filtered vector search (preview)
284
+
You can now execute vector searches with any supported query filter such as `$lt, $lte, $eq, $neq, $gte, $gt, $in, $nin, and $regex`. Enable the "filtering vector search" feature in the "Preview Features" tab of your Azure Subscription. Learn more about preview features [here](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/preview-features?tabs=azure-portal).
221
285
222
-
As a preview feature, this must be enabled using Azure Feature Enablement Control (AFEC) by selecting the "mongoHnswIndex" feature. For more information, see [enable preview features](../../../azure-resource-manager/management/preview-features.md).
286
+
First, you'll need to define an index for your filter in addition to a vector index. Forexample, you can define the filter index on a property
223
287
224
-
### Create an HNSW vector index
288
+
```javascript
289
+
db.runCommand({
290
+
"createIndexes": "<collection_name",
291
+
"indexes": [ {
292
+
"key": {
293
+
"<property_to_filter>": 1
294
+
},
295
+
"name": "<name_of_filter_index>"
296
+
}
297
+
]
298
+
});
299
+
```
225
300
226
-
To use HNSW as your index algorithm, you need to create a vector index with the `kind` parameter set to "vector-hnsw" following the template below:
301
+
Next, you can add the `"filter"` term to your vector search as shown below. Inthis example the filter is looking for documents where the `"title"` property is not in the list of`["not in this text", "or this text"]`.
227
302
228
303
```javascript
229
-
{
230
-
"createIndexes":"<collection_name>",
231
-
"indexes": [
232
-
{
233
-
"name":"<index_name>",
234
-
"key": {
235
-
"<path_to_property>":"cosmosSearch"
236
-
},
237
-
"cosmosSearchOptions": {
238
-
"kind":"vector-hnsw",
239
-
"m":<integer_value>,
240
-
"efConstruction":<integer_value>,
241
-
"similarity":"<string_value>",
242
-
"dimensions":<integer_value>
243
-
}
244
-
}
245
-
]
304
+
305
+
db.exampleCollection.aggregate([
306
+
{
307
+
'$search': {
308
+
"cosmosSearch": {
309
+
"vector": "<query_vector>",
310
+
"path": <path_to_vector>,
311
+
"k": num_results,
312
+
"filter": {<property_to_filter>: {"$nin": ["not in this text", "or this text"]}}
> While in preview, filtered vector search may require you to adjust your vector index parameters to achieve higher accuracy. For example, increasing `m`, `efConstruction`, or `efSearch` when using HNSW, or `numLists`, or `nProbes` when using IVF, may lead to better results. You should test your configuration before use to ensure that the results are satisfactory.
248
321
249
-
|Field |Type |Description |
250
-
|---------|---------|---------|
251
-
|`kind`| string | Type of vector index to create. Type of vector index to create. Primarily, `vector-ivf` is supported. `vector-hnsw` is available as a preview feature that requires enablement via [Azure Feature Enablement Control](../../../azure-resource-manager/management/preview-features.md).|
252
-
|`m`|integer |The max number of connections per layer (`16` by default, minimum value is `2`, maximum value is `100`). Higher m is suitable for datasets with high dimensionality and/or high accuracy requirements. |
253
-
|`efConstruction`|integer |the size of the dynamic candidate list for constructing the graph (`64` by default, minimum value is `4`, maximum value is `1000`). Higher `efConstruction` will result in better index quality and higher accuracy, but it will also increase the time required to build the index. `efConstruction` has to be at least `2 * m`|
254
-
|`similarity`|string |Similarity metric to use with the index. Possible options are `COS` (cosine distance), `L2` (Euclidean distance), and `IP` (inner product). |
255
-
|`dimensions`|integer |Number of dimensions for vector similarity. The maximum number of supported dimensions is `2000`. |
256
-
322
+
## Use LLM Orchestration tools such
257
323
258
-
> [!WARNING]
259
-
> Using the HSNW vector index (preview) with large datasets can result in resource running out of memory, or reducing the performance of other operations running on your database. To reduce the chance of this happening, we recommend to:
260
-
> - Only use HNSW indexes on a cluster tier of M40 or higher.
261
-
> - Scale to a higher cluster tier or reduce the size of the database if your encounter errors.
324
+
### Use as a vector database with Semantic Kernel
325
+
Use Semantic Kernel to orchestrate your information retrieval from Azure Cosmos DBfor MongoDB vCore and your LLM. Learn more [here]([https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db](https://github.com/microsoft/semantic-kernel/tree/main/python/semantic_kernel/connectors/memory/azure_cosmosdb)).
262
326
263
-
### Perform a vector search with HNSW
264
-
To perform a vector search, use the `$search` aggregation pipeline stage the query with the `cosmosSearch` operator.
|`efSearch`|integer |The size of the dynamic candidate list for search (`40` by default). A higher value provides better recall at the cost of speed. |
282
-
|`k`|integer |The number of results to return. it should be less than or equal to `efSearch`|
329
+
### Use as a vector database with LangChain
330
+
Use LangChain to orchestrate your information retrieval from Azure Cosmos DBfor MongoDB vCore and your LLM. Learn more [here](https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db).
283
331
284
-
## Use as a vector database with LangChain
285
-
You can now use LangChain to orchestrate your information retrieval from Azure Cosmos DB for MongoDB vCore and your LLM. Learn more [here](https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db).
332
+
### Use as a semantic cachewith LangChain
333
+
Use LangChain and Azure Cosmos DBforMongoDB (vCore) to orchestrate Semantic Caching, using previously recocrded LLM respones that can save you LLMAPI costs and reduce latency forresponses. Learn more [here](https://python.langchain.com/docs/integrations/llms/llm_caching#azure-cosmos-db-semantic-cache)
0 commit comments