Skip to content

Commit 22fd56d

Browse files
committed
DOCSP-41555: Atlas vector search (#84)
* DOCSP-41555: Atlas vector search * edits * RR feedback * tech review (cherry picked from commit 682d093)
1 parent 0aaba0b commit 22fd56d

File tree

2 files changed

+154
-42
lines changed

2 files changed

+154
-42
lines changed

source/includes/indexes/indexes.c

Lines changed: 101 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,26 +149,78 @@ main (void)
149149
// end-create-search-index
150150
}
151151
{
152-
// start-create-search-indexes
153-
bson_t cmd;
154-
bson_error_t error;
155-
char *cmd_str = bson_strdup_printf (
156-
BSON_STR ({
157-
"createSearchIndexes" : "%s",
158-
"indexes" : [ {"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<first index name>"},
159-
{"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<second index name>"} ]
160-
}),
161-
"<collection name>");
162-
bson_init_from_json (&cmd, cmd_str, -1, &error);
163-
bson_free (cmd_str);
152+
// start-create-vector-search-index
153+
bson_t cmd;
154+
bson_error_t error;
155+
char * cmd_str = bson_strdup_printf(
156+
BSON_STR({
157+
"createSearchIndexes": "%s",
158+
"indexes": [{
159+
"name": "<index name>",
160+
"type": "vectorSearch",
161+
"definition": {
162+
"fields": [{
163+
"type": "vector",
164+
"path": "plot_embedding",
165+
"numDimensions": 1536,
166+
"similarity": "euclidean"
167+
}]
168+
}
169+
}]
170+
}),
171+
"<collection name>"
172+
);
173+
bson_init_from_json(&cmd, cmd_str, -1, &error);
174+
bson_free(cmd_str);
164175

165-
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
166-
printf ("Successfully created search indexes\n");
167-
} else {
168-
fprintf (stderr, "Failed to create search indexes: %s", error.message);
169-
}
170-
bson_destroy (&cmd);
171-
// end-create-search-indexes
176+
if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) {
177+
printf("Successfully created Vector Search index\n");
178+
} else {
179+
fprintf(stderr, "Failed to create Vector Search index: %s", error.message);
180+
}
181+
bson_destroy(&cmd);
182+
// end-create-vector-search-index
183+
}
184+
{
185+
// start-create-search-indexes
186+
bson_t cmd;
187+
bson_error_t error;
188+
char * cmd_str = bson_strdup_printf(
189+
BSON_STR({
190+
"createSearchIndexes": "%s",
191+
"indexes": [{
192+
"definition": {
193+
"mappings": {
194+
"dynamic": false
195+
}
196+
},
197+
"name": "<Atlas Search index name>"
198+
},
199+
{
200+
"name": "<Vector Search index name>",
201+
"type": "vectorSearch",
202+
"definition": {
203+
"fields": [{
204+
"type": "vector",
205+
"path": "plot_embedding",
206+
"numDimensions": 1536,
207+
"similarity": "euclidean"
208+
}]
209+
}
210+
}
211+
]
212+
}),
213+
"<collection name>");
214+
bson_init_from_json(&cmd, cmd_str, -1, &error);
215+
bson_free(cmd_str);
216+
217+
if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) {
218+
printf("Successfully created search indexes\n");
219+
} else {
220+
fprintf(stderr, "Failed to create search indexes: %s", error.message);
221+
}
222+
bson_destroy(&cmd);
223+
// end-create-search-indexes
172224
}
173225
{
174226
// start-list-search-indexes
@@ -212,6 +264,36 @@ main (void)
212264
bson_destroy (&cmd);
213265
// end-update-search-index
214266
}
267+
{
268+
// start-update-vector-search-index
269+
bson_t cmd;
270+
bson_error_t error;
271+
char * cmd_str = bson_strdup_printf(
272+
BSON_STR({
273+
"updateSearchIndex": "%s",
274+
"name": "<index name>",
275+
"definition": {
276+
"fields": [{
277+
"type": "vector",
278+
"path": "plot_embedding",
279+
"numDimensions": 1536,
280+
"similarity": "cosine"
281+
}]
282+
}
283+
}),
284+
"<collection name>"
285+
);
286+
bson_init_from_json(&cmd, cmd_str, -1, &error);
287+
bson_free(cmd_str);
288+
289+
if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) {
290+
printf("Successfully updated search index\n");
291+
} else {
292+
fprintf(stderr, "Failed to create search index: %s", error.message);
293+
}
294+
bson_destroy(&cmd);
295+
// end-update-vector-search-index
296+
}
215297
{
216298
// start-drop-search-index
217299
bson_t cmd;

source/indexes/atlas-search-index.txt

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,30 @@ Atlas Search Indexes
2020
Overview
2121
--------
2222

23-
:atlas:`Atlas Search </atlas-search>` enables you to perform full-text searches on
24-
collections hosted on MongoDB Atlas. Atlas Search indexes specify the behavior of
25-
the search and which fields to index.
23+
In this guide, you can learn how to programmatically manage your Atlas
24+
Search and Atlas Vector Search indexes by using the {+driver-short+}.
25+
26+
The Atlas Search feature enables you to perform full-text searches on
27+
collections hosted on MongoDB Atlas. To learn more about Atlas Search,
28+
see the :atlas:`Atlas Search Overview </atlas-search/atlas-search-overview/>`
29+
in the Atlas documentation.
30+
31+
Atlas Vector Search enables you to perform semantic searches on vector
32+
embeddings stored in MongoDB Atlas. To learn more about Atlas Vector Search,
33+
see the :atlas:`Atlas Vector Search Overview </atlas-vector-search/vector-search-overview/>`
34+
in the Atlas documentation.
2635

2736
The following sections provide code examples that demonstrate how to create, list, update,
28-
and delete Atlas Search indexes.
37+
and delete Atlas Search and Vector Search indexes.
2938

3039
.. _c-atlas-search-index-create:
3140

3241
Create a Search Index
3342
---------------------
3443

35-
You can pass the ``createSearchIndexes`` command to the ``mongoc_collection_command_simple()``
36-
function to create one or more Atlas Search indexes.
37-
38-
You can also use this function to create Atlas Vector Search indexes.
39-
Atlas Vector Search enables you to perform semantic searches on vector
40-
embeddings stored in MongoDB Atlas. To learn more about this feature,
41-
see the :atlas:`Atlas Vector Search Overview
42-
</atlas-vector-search/vector-search-overview/>` in the Atlas documentation.
44+
To create an Atlas Search or Vector Search index, pass the ``createSearchIndexes``
45+
command to the ``mongoc_collection_command_simple()`` function. You can use this command
46+
to create one or multiple indexes.
4347

4448
The following code example shows how to create an Atlas Search index:
4549

@@ -49,26 +53,37 @@ The following code example shows how to create an Atlas Search index:
4953
:end-before: end-create-search-index
5054
:dedent:
5155

52-
The following code example shows how to create multiple indexes:
56+
The following code example shows how to create an Atlas Vector Search index:
57+
58+
.. literalinclude:: /includes/indexes/indexes.c
59+
:language: c
60+
:start-after: start-create-vector-search-index
61+
:end-before: end-create-vector-search-index
62+
:dedent:
63+
64+
The following code example shows how to create both search indexes in
65+
one call to the ``mongoc_collection_command_simple()`` function:
5366

5467
.. literalinclude:: /includes/indexes/indexes.c
5568
:language: c
5669
:start-after: start-create-search-indexes
5770
:end-before: end-create-search-indexes
5871
:dedent:
5972

60-
To learn more about the syntax used to define Atlas Search indexes, see the
61-
:atlas:`Review Atlas Search Index Syntax </atlas-search/index-definitions>` guide
62-
in the Atlas documentation.
73+
To learn more about the syntax used to define each search index, see the
74+
following guides in the Atlas documentation:
75+
76+
- :atlas:`Review Atlas Search Index Syntax </atlas-search/index-definitions>`
77+
- :atlas:`How to Index Fields for Vector Search </atlas-vector-search/vector-search-type/>`
6378

6479
.. _c-atlas-search-index-list:
6580

6681
List Search Indexes
6782
-------------------
6883

6984
You can pass the ``$listSearchIndexes`` aggregation stage to the
70-
``mongoc_collection_aggregate()`` function to return all Atlas Search indexes in a
71-
collection.
85+
``mongoc_collection_aggregate()`` function to return all Atlas Search
86+
and Vector Search indexes in a collection.
7287

7388
The following code example shows how to print a list of the search indexes in
7489
a collection:
@@ -85,23 +100,35 @@ Update a Search Index
85100
---------------------
86101

87102
You can pass the ``updateSearchIndex`` command to the ``mongoc_collection_command_simple()``
88-
function to update an Atlas Search index.
103+
function to update an Atlas Search or Vector Search index.
89104

90-
The following code shows how to update a search index:
105+
The following code shows how to update the Atlas Search index
106+
created in the :ref:`c-atlas-search-index-create` section of this guide
107+
to use dynamic mappings:
91108

92109
.. literalinclude:: /includes/indexes/indexes.c
93110
:language: c
94111
:start-after: start-update-search-index
95112
:end-before: end-update-search-index
96113
:dedent:
97114

115+
The following code shows how to update the Atlas Vector Search
116+
index created in the :ref:`c-atlas-search-index-create` section of this guide
117+
to use the ``cosine`` similarity function:
118+
119+
.. literalinclude:: /includes/indexes/indexes.c
120+
:language: c
121+
:start-after: start-update-vector-search-index
122+
:end-before: end-update-vector-search-index
123+
:dedent:
124+
98125
.. _c-atlas-search-index-drop:
99126

100127
Delete a Search Index
101128
---------------------
102129

103130
You can pass the ``dropSearchIndexes`` command to the ``mongoc_collection_command_simple()``
104-
function to delete an Atlas Search index.
131+
function to delete an Atlas Search or Vector Search index.
105132

106133
The following code shows how to delete a search index from a collection:
107134

@@ -114,8 +141,11 @@ The following code shows how to delete a search index from a collection:
114141
Additional Information
115142
----------------------
116143

117-
To learn more about MongoDB Atlas Search, see the :atlas:`Atlas Search Indexes </atlas-search/atlas-search-overview/>`
118-
documentation.
144+
To learn more about MongoDB Atlas Search, see :atlas:`Atlas Search Indexes </atlas-search/atlas-search-overview/>`
145+
in the Atlas documentation.
146+
147+
To learn more about MongoDB Atlas Vector Search, see :atlas:`How to Index Fields for Vector Search
148+
</atlas-vector-search/vector-search-type/>` in the Atlas documentation.
119149

120150
API Documentation
121151
~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)