Overview
In this guide, you can learn how to create and manage MongoDB Search indexes. These indexes allow you to use MongoDB Search features to perform fast, full-text searches on data stored in an Atlas cluster. A MongoDB Search index configures the behavior of MongoDB Search by specifying which fields to index, how these fields are indexed, and other optional settings. To learn more about MongoDB Search indexes, see the MongoDB Search documentation.
This guide explains how to perform the following actions to manage your MongoDB Search indexes:
Note
The examples in this guide access the posts collection in the sample_training
database, which is one of the Atlas sample datasets. For instructions on importing
the Atlas sample data, see Load Sample Data in the Atlas
documentation.
Create a Search Index Model
To create a MongoDB Search index, you must first build a SearchIndexModel instance
that sets your index specifications. To begin building a SearchIndexModel instance,
call the SearchIndexModel::builder() method.
Note
Instantiating Models
The Rust driver implements the Builder design pattern for the
creation of many different types, including SearchIndexModel. You
can use the builder() method to construct an instance of each type
by chaining option builder methods.
The Rust driver provides the following SearchIndexModel builder methods:
definition(), which accepts a BSON document parameter and sets your index definitionname(), which accepts a string parameter and sets your index name
The BSON document that you pass to the definition() builder method must include
the mappings field. To automatically index all supported fields in your collection,
enable dynamic mappings by setting the mappings.dynamic nested field to true. To
index only specified fields, enable static mappings by setting the mappings.dynamic
nested field to false and including a list of fields you want to index.
Tip
MongoDB Search Field Mappings
To learn more about MongoDB Search field mappings, see Define Field Mappings in the Atlas documentation.
Example
The following example creates specifications for a index named example_index in a
SearchIndexModel instance. The code sets a static mapping to index only the body
and date fields:
let def = doc! { "mappings": doc! { "dynamic": false, "fields": { "body": {"type": "string"}, "date": {"type": "date"} } }}; let idx_model = SearchIndexModel::builder() .definition(def) .name("example_index".to_string()) .build();
Create a Search Index
You can create a MongoDB Search index on a collection by calling the create_search_index()
method on a Collection instance. This method accepts the following parameters:
Index model, specified in a
SearchIndexModelinstanceIndex options, specified in a CreateSearchIndexOptions instance
Example
The following example creates a MongoDB Search index on the posts collection.
The code creates a SearchIndexModel that sets the index name and enables dynamic
mapping. Then, the code passes the SearchIndexModel instance to the create_search_index()
method to create the MongoDB Search index:
let idx_model = SearchIndexModel::builder() .definition(doc! { "mappings": doc! {"dynamic": true} }) .name("example_index".to_string()) .build(); let result = my_coll.create_search_index(idx_model).await?; println!("Created MongoDB Search index:\n{}", result);
Created MongoDB Search index: "example_index"
Create Multiple Search Indexes
You can create multiple MongoDB Search indexes at once by calling the create_search_indexes()
method on a Collection instance. This method accepts the following parameters:
List of index models, specified as a vector of
SearchIndexModelinstancesIndex options, specified in a
CreateSearchIndexOptionsinstance
Example
The following example creates two MongoDB Search indexes named dynamic_index and
static_index on the posts collection. The code creates SearchIndexModel
instances for each index that specify the index names and definitions. Then, the code
passes these models as a vector to the create_search_indexes() method and creates
the indexes:
let dyn_idx = SearchIndexModel::builder() .definition(doc! { "mappings": doc! {"dynamic": true} }) .name("dynamic_index".to_string()) .build(); let static_idx = SearchIndexModel::builder() .definition(doc! {"mappings": doc! { "dynamic": false, "fields": { "title": {"type": "string"}}}}) .name("static_index".to_string()) .build(); let models = vec![dyn_idx, static_idx]; let result = my_coll.create_search_indexes(models).await?; println!("Created MongoDB Search indexes:\n{:?}", result);
Created MongoDB Search indexes: ["dynamic_index", "static_index"]
List Search Indexes
You can access information about a collection's existing MongoDB Search indexes
by calling the list_search_indexes() method on the collection. This
method accepts the following parameters:
Name of the index to retrieve information about
Aggregation options, specified in an AggregateOptions instance
Index options, specified in a ListSearchIndexOptions instance
Example
The following example accesses information about the MongoDB Search indexes created
in the Create Multiple Search Indexes section of this page. The code calls the
list_search_indexes() method and passes a value of None for each parameter,
which instructs the driver to return information about all MongoDB Search indexes with
default options. Then, the code outputs the search indexes:
let mut cursor = my_coll.list_search_indexes().await?; while let Some(index) = cursor.try_next().await? { println!("{}\n", index); }
{ "id": "...", "name": "dynamic_index", "status": "READY", "queryable": true, "latestDefinitionVersion": {...}, "latestDefinition": { "mappings": { "dynamic": true } }, "statusDetail": [...] } { "id": "...", "name": "static_index", "status": "READY", "queryable": true, "latestDefinitionVersion": {...}, "latestDefinition": { "mappings": { "dynamic": false, "fields": { "title": { "type": "string" } } } }, "statusDetail": [...] }
Tip
To learn more about iterating through a cursor, see the Access Data by Using a Cursor guide.
Update a Search Index
You can update a MongoDB Search index by calling the update_search_index()
method on a Collection instance. This method accepts the following parameters:
Name of the index to update
Modified index definition document
Index options, specified in an UpdateSearchIndexOptions instance
Example
The following example updates the MongoDB Search index named static_index
created in the Create Multiple Search Indexes section of this page. The code
creates a new index definition document that instructs the index to use dynamic
mappings instead of static mappings. Then, the code calls the update_search_index()
method to update the index:
let name = "static_index"; let definition = doc! { "mappings": doc! {"dynamic": true} }; my_coll.update_search_index(name, definition).await?;
Delete a Search Index
You can delete a MongoDB Search index by calling the delete_search_index()
method on a Collection instance. This method accepts the following parameters:
Name of the index to delete
Index options, specified in a DropSearchIndexOptions instance
Example
The following example deletes the MongoDB Search index named example_index
created in the Create a Search Index section of this page. The code
passes the index name to the delete_search_index() method to delete the index:
let name = "example_index"; my_coll.drop_search_index(name).await?;
Additional Information
To learn about other indexes you can create by using the Rust driver, see the Indexes guide.
To learn more about MongoDB Search, see the following Atlas documentation:
API Documentation
To learn more about the methods and types mentioned in this guide, see the following API documentation: