Docs Menu
Docs Home
/ / /
Rust Driver
/ /

Atlas Search and Vector Search Indexes

On this page

  • Overview
  • Create a Search Index Model
  • Example Models
  • 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 and Vector Search indexes. These indexes allow you to use the following features:

  • Atlas Search: Perform fast, full-text searches

  • Atlas Vector Search: Perform semantic (similarity) searches on vector embeddings

Atlas Search and 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 Atlas Search and 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.

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 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

definition()

Document

Specifies the index definition. If you omit this setting, the driver creates an Atlas Search index with dynamic mappings.

name()

String

Sets the index name. If you omit this setting, the driver sets the name to default.

index_type()

SearchIndexType

Sets the index type. If you omit this setting, the driver creates an Atlas Search index by default.

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

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

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

You can create an Atlas Search or 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.

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

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

This example performs the following actions:

  1. Creates a SearchIndexModel instance that specifies an Atlas Search index named as_idx

  2. Creates a SearchIndexModel instance that specifies an Atlas Vector Search index named vs_idx

  3. Passes a vec of both SearchIndexModel instances to the create_search_indexes() method

  4. Creates the Atlas Search and Vector Search indexes on the embedded_movies collection

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 Atlas Search indexes:
["as_idx", "vs_idx"]

You can access information about a collection's existing Atlas Search and Vector Search indexes by calling the list_search_indexes() method on the collection.

The following example accesses information about the Atlas Search and 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 Atlas Search and 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.

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

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?;

You can delete an Atlas Search or 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.

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).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 Atlas Vector Search, see the following Atlas documentation:

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

Back

Indexes