Docs Menu
Docs Home
/ / /
C Driver
/ /

Atlas Search Indexes

On this page

  • Overview
  • Create a Search Index
  • List Search Indexes
  • Update a Search Index
  • Delete a Search Index
  • Additional Information
  • API Documentation

In this guide, you can learn how to programmatically manage your Atlas Search and Atlas Vector Search indexes by using the C driver.

The Atlas Search feature enables you to perform full-text searches on collections hosted on MongoDB Atlas. To learn more about Atlas Search, see the Atlas Search Overview in the Atlas documentation.

Atlas Vector Search enables you to perform semantic searches on vector embeddings stored in MongoDB Atlas. To learn more about Atlas Vector Search, see the Atlas Vector Search Overview in the Atlas documentation.

The following sections provide code examples that demonstrate how to create, list, update, and delete Atlas Search and Vector Search indexes.

To create an Atlas Search or Vector Search index, pass the createSearchIndexes command to the mongoc_collection_command_simple() function. You can use this command to create one or multiple indexes.

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

bson_t cmd;
bson_error_t error;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"createSearchIndexes" : "%s",
"indexes" : [ {"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<index name>"} ]
}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
bson_free (cmd_str);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully created search index\n");
} else {
fprintf (stderr, "Failed to create search index: %s", error.message);
}
bson_destroy (&cmd);

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

bson_t cmd;
bson_error_t error;
char * cmd_str = bson_strdup_printf(
BSON_STR({
"createSearchIndexes": "%s",
"indexes": [{
"name": "<index name>",
"type": "vectorSearch",
"definition": {
"fields": [{
"type": "vector",
"path": "plot_embedding",
"numDimensions": 1536,
"similarity": "euclidean"
}]
}
}]
}),
"<collection name>"
);
bson_init_from_json(&cmd, cmd_str, -1, &error);
bson_free(cmd_str);
if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) {
printf("Successfully created Vector Search index\n");
} else {
fprintf(stderr, "Failed to create Vector Search index: %s", error.message);
}
bson_destroy(&cmd);

The following code example shows how to create both search indexes in one call to the mongoc_collection_command_simple() function:

bson_t cmd;
bson_error_t error;
char * cmd_str = bson_strdup_printf(
BSON_STR({
"createSearchIndexes": "%s",
"indexes": [{
"definition": {
"mappings": {
"dynamic": false
}
},
"name": "<Atlas Search index name>"
},
{
"name": "<Vector Search index name>",
"type": "vectorSearch",
"definition": {
"fields": [{
"type": "vector",
"path": "plot_embedding",
"numDimensions": 1536,
"similarity": "euclidean"
}]
}
}
]
}),
"<collection name>");
bson_init_from_json(&cmd, cmd_str, -1, &error);
bson_free(cmd_str);
if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) {
printf("Successfully created search indexes\n");
} else {
fprintf(stderr, "Failed to create search indexes: %s", error.message);
}
bson_destroy(&cmd);

To learn more about the syntax used to define each search index, see the following guides in the Atlas documentation:

You can pass the $listSearchIndexes aggregation stage to the mongoc_collection_aggregate() function to return all Atlas Search and Vector Search indexes in a collection.

The following code example shows how to print a list of the search indexes in a collection:

bson_t pipeline;
const bson_t *doc;
bson_error_t error;
const char *pipeline_str = BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]});
bson_init_from_json (&pipeline, pipeline_str, -1, &error);
mongoc_cursor_t *cursor =
mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, &pipeline, NULL, NULL);
while (mongoc_cursor_next (cursor, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
bson_destroy (&pipeline);
mongoc_cursor_destroy (cursor);

You can pass the updateSearchIndex command to the mongoc_collection_command_simple() function to update an Atlas Search or Vector Search index.

The following code shows how to update the Atlas Search index created in the Create a Search Index section of this guide to use dynamic mappings:

bson_t cmd;
bson_error_t error;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"updateSearchIndex" : "%s",
"definition" : {"mappings" : {"dynamic" : true}}, "name" : "<index name>"}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
bson_free (cmd_str);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully updated search index\n");
} else {
fprintf (stderr, "Failed to create search index: %s", error.message);
}
bson_destroy (&cmd);

The following code shows how to update the Atlas Vector Search index created in the Create a Search Index section of this guide to use the cosine similarity function:

bson_t cmd;
bson_error_t error;
char * cmd_str = bson_strdup_printf(
BSON_STR({
"updateSearchIndex": "%s",
"name": "<index name>",
"definition": {
"fields": [{
"type": "vector",
"path": "plot_embedding",
"numDimensions": 1536,
"similarity": "cosine"
}]
}
}),
"<collection name>"
);
bson_init_from_json(&cmd, cmd_str, -1, &error);
bson_free(cmd_str);
if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) {
printf("Successfully updated search index\n");
} else {
fprintf(stderr, "Failed to create search index: %s", error.message);
}
bson_destroy(&cmd);

You can pass the dropSearchIndexes command to the mongoc_collection_command_simple() function to delete an Atlas Search or Vector Search index.

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

bson_t cmd;
bson_error_t error;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"dropSearchIndexes" : "%s",
"index" : "<index name>"
}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully deleted search index\n");
} else {
fprintf (stderr, "Failed to delete search index: %s", error.message);
}
bson_destroy (&cmd);

To learn more about MongoDB Atlas Search, see Atlas Search Indexes in the Atlas documentation.

To learn more about MongoDB Atlas Vector Search, see How to Index Fields for Vector Search in the Atlas documentation.

To learn more about any of the functions discussed in this guide, see the following API documentation:

Back

Multikey Indexes