Atlas Search Indexes
On this page
Overview
Atlas Search enables you to perform full-text searches on collections hosted on MongoDB Atlas. Atlas Search indexes specify the behavior of the search and which fields to index.
The following sections provide code examples that demonstrate how to create, list, update, and delete Atlas Search indexes.
Create a Search Index
You can pass the createSearchIndexes
command to the mongoc_collection_command_simple()
function to create one or more Atlas Search indexes.
You can also use this function to create Atlas Vector Search indexes. Atlas Vector Search enables you to perform semantic searches on vector embeddings stored in MongoDB Atlas. To learn more about this feature, see the Atlas Vector Search Overview in the Atlas documentation.
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 multiple indexes:
bson_t cmd; bson_error_t error; char *cmd_str = bson_strdup_printf ( BSON_STR ({ "createSearchIndexes" : "%s", "indexes" : [ {"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<first index name>"}, {"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<second 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 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 Atlas Search indexes, see the Review Atlas Search Index Syntax guide in the Atlas documentation.
List Search Indexes
You can pass the $listSearchIndexes
aggregation stage to the
mongoc_collection_aggregate()
function to return all Atlas 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);
Update a Search Index
You can pass the updateSearchIndex
command to the mongoc_collection_command_simple()
function to update an Atlas Search index.
The following code shows how to update a search index:
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);
Delete a Search Index
You can pass the dropSearchIndexes
command to the mongoc_collection_command_simple()
function to delete an Atlas 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);
Additional Information
To learn more about MongoDB Atlas Search, see the Atlas Search Indexes documentation.
API Documentation
To learn more about any of the functions discussed in this guide, see the following API documentation: