Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversRust Driver

Atlas Search Indexes

On this page

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

In this guide, you can learn how to create and manage Atlas Search indexes. These indexes allow you to use MongoDB's Atlas Search feature to perform fast, full-text searches on data stored in an Atlas cluster. An Atlas Search index configures the behavior of Atlas Search by specifying which fields to index, how these fields are indexed, and other optional settings. To learn more about Atlas Search indexes, see the Atlas Search documentation.

This guide explains how to perform the following actions to manage your Atlas 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.

To create an Atlas 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 definition

  • name(), 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

Atlas Search Field Mappings

To learn more about Atlas Search field mappings, see Define Field Mappings in the Atlas documentation.

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();

You can create an Atlas Search index on a collection by calling the create_search_index() method on a Collection instance. This method accepts the following parameters:

The following example creates an Atlas 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 Atlas 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, None).await?;
println!("Created Atlas Search index:\n{}", result);

You can create multiple Atlas 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 SearchIndexModel instances

  • Index options, specified in a CreateSearchIndexOptions instance

The following example creates two Atlas 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, None).await?;
println!("Created Atlas Search indexes:\n{:?}", result);

You can access information about a collection's existing Atlas Search indexes by calling the list_search_indexes() method on the collection. This method accepts the following parameters:

The following example accesses information about the Atlas 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 Atlas Search indexes with default options. Then, the code outputs the search indexes:

let mut cursor = my_coll.list_search_indexes(None, None, None).await?;
while let Some(index) = cursor.try_next().await? {
println!("{}\n", index);
}

Tip

To learn more about iterating through a cursor, see the Access Data by Using a Cursor guide.

You can update an Atlas 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

The following example updates the Atlas 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, None).await?;

You can delete an Atlas Search index by calling the delete_search_index() method on a Collection instance. This method accepts the following parameters:

The following example deletes the Atlas 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, None).await?;

To learn about other indexes you can create by using the Rust driver, see the Indexes guide.

To learn more about Atlas Search, see the following Atlas documentation:

To learn more about the methods and types mentioned in this guide, see the following API documentation:

←  IndexesTransactions →