Overview
In this guide, you can learn how to create and manage MongoDB Search and MongoDB Vector Search indexes. These indexes allow you to use the following features:
MongoDB Search: Perform fast, full-text searches
MongoDB Vector Search: Perform semantic (similarity) searches on vector embeddings
MongoDB Search and MongoDB Vector Search indexes specify which fields to index, specify how these fields are indexed, and set other optional configurations.
This guide explains how to perform the following actions to manage your MongoDB Search and MongoDB Vector Search indexes:
Note
Sample Data
The examples in this guide use the embedded_movies collection
in the sample_mflix 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 some struct 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:
Builder Method  | Parameter Type  | Description  | 
|---|---|---|
  | 
  | Specifies the index definition. If you omit this setting, the driver creates a MongoDB Search index with dynamic mappings.  | 
  | 
  | Sets the index name. If you omit this setting, the
driver sets the name to   | 
  | 
  | Sets the index type. If you omit this setting, the driver creates a MongoDB Search index by default.  | 
To learn more about MongoDB Search field mappings, see Define Field Mappings in the Atlas documentation.
To learn more about defining MongoDB Vector Search indexes, see How to Index Fields for Vector Search in the Atlas documentation.
Example Models
The following example creates a SearchIndexModel instance to provide
specifications for an index named search_idx. The code specifies static
mappings of the title and released fields:
let def = doc! { "mappings": doc! {     "dynamic": false,     "fields": {         "title": {"type": "string"},         "released": {"type": "date"}     } }}; let idx_model = SearchIndexModel::builder()     .definition(def)     .name("search_idx".to_string())     .index_type(SearchIndexType::Search)     .build(); 
The following example creates a SearchIndexModel instance to provide
specifications for an index named vs_idx. The code specifies the
embedding path as  plot_embedding, indexes 1536 dimensions, and
uses the "euclidean" vector similarity function:
let def = doc! {     "fields": [{         "type": "vector",         "path": "plot_embedding",         "numDimensions": 1536,         "similarity": "euclidean",     }] }; let idx_model = SearchIndexModel::builder()     .definition(def)     .name("vs_idx".to_string())     .index_type(SearchIndexType::VectorSearch)     .build(); 
Create a Search Index
You can create a MongoDB Search or MongoDB Vector Search index on a collection by
calling the create_search_index() method on a Collection
instance. This method accepts an index model as a parameter, specified
in a SearchIndexModel instance.
Example
The following example creates a MongoDB Search index on the
embedded_movies 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 and Vector Search indexes
by calling the create_search_indexes() method on a Collection
instance. This method accepts a vector of SearchIndexModel instances
as a parameter.
Example
This example performs the following actions:
Creates a
SearchIndexModelinstance that specifies a MongoDB Search index namedas_idxCreates a
SearchIndexModelinstance that specifies a MongoDB Vector Search index namedvs_idxPasses a
vecof bothSearchIndexModelinstances to thecreate_search_indexes()methodCreates the MongoDB Search and MongoDB Vector Search indexes on the
embedded_moviescollection
let as_idx = SearchIndexModel::builder()     .definition(doc! { "mappings": doc! {"dynamic": true} })     .name("as_idx".to_string())     .build(); let vs_idx = SearchIndexModel::builder()     .definition(doc! {         "fields": [{             "type": "vector",             "path": "plot_embedding",             "numDimensions": 1536,             "similarity": "euclidean",         }]     })     .name("vs_idx".to_string())     .index_type(SearchIndexType::VectorSearch)     .build(); let models = vec![as_idx, vs_idx]; let result = my_coll.create_search_indexes(models).await?; println!("Created indexes:\n{:?}", result); 
Created MongoDB Search indexes: ["as_idx", "vs_idx"] 
List Search Indexes
You can access information about a collection's existing MongoDB Search
and MongoDB Vector Search indexes by calling the list_search_indexes()
method on the collection.
Example
The following example accesses information about the MongoDB Search and
MongoDB Vector Search indexes created in the Create Multiple Search Indexes
section of this page. The code calls the list_search_indexes()
method and prints a list of the MongoDB Search and MongoDB Vector Search indexes
on the collection:
let mut cursor = my_coll.list_search_indexes().await?; while let Some(index) = cursor.try_next().await? {     println!("{}\n", index); } 
{ "id": "...", "name": "as_idx", "status": "READY", "queryable": true, "latestDefinitionVersion": {...}, "latestDefinition": { "mappings": { "dynamic": true } }, "statusDetail": [...] } { "id": "...", "name": "vs_idx", "type": "vectorSearch", "status": "READY", "queryable": true, ..., "latestDefinition": { "fields": [{ "type": "vector", "path": "plot_embedding", "numDimensions": 1536, "similarity": "euclidean" }] }, "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 or MongoDB Vector 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
Example
The following example updates the Vector Search index named vs_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
"dotProduct" as the vector similarity function. Then, the code calls
the update_search_index() method to update the index:
let name = "vs_index"; let updated_def = doc! {     "fields": [{         "type": "vector",         "path": "plot_embedding",         "numDimensions": 1536,         "similarity": "dotProduct",     }] }; my_coll.update_search_index(name, updated_def).await?; 
Delete a Search Index
You can delete a MongoDB Search or MongoDB Vector Search index by calling the
delete_search_index() method on a Collection instance. This
method accepts the name of the index to delete as a parameter.
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:
To learn more about MongoDB Vector 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: