Docs Menu
Docs Home
/ / /
C#/.NET 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.

Note

Atlas Search index-management methods run asynchronously. The driver methods can return a result before the desired action completes on the server.

This guide explains how to perform the following actions to manage your Atlas Search and Vector Search indexes:

  • Create a Search Index Model

  • Create a Search Index

  • Create Multiple Search Indexes

  • List Search Indexes

  • Update a Search Index

  • Delete a Search Index

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 or Vector Search index, you must first build a CreateSearchIndexModel instance that sets your index specifications.

The CreateSearchIndexModel has the following properties:

Property
Type
Description

definition

BsonDocument

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.

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 CreateSearchIndexModel instance to provide specifications for an index named search_idx. The code specifies static mappings of the title and released fields:

var def = new BsonDocument {
{ "mappings", new BsonDocument {
{ "dynamic", false },
{ "fields", new BsonDocument {
{ "title", new BsonDocument { {"type", "string" } } },
{ "released", new BsonDocument { { "type", "date" } } } } }
} }
};
var indexModel = new CreateSearchIndexModel(
"search_idx",
SearchIndexType.Search,
def
);

The following example creates a CreateSearchIndexModel 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:

var def = new BsonDocument
{
{ "fields", new BsonArray
{
new BsonDocument
{
{ "type", "vector" },
{ "path", "plot_embedding" },
{ "numDimensions", 1536 },
{ "similarity", "euclidean" }
}
}
}
};
var indexModel = new CreateSearchIndexModel(
"vs_idx",
SearchIndexType.VectorSearch,
def
);

You can create an Atlas Search or Vector Search index on a collection by calling the SearchIndexes.CreateOne() method on an IMongoCollection instance. This method accepts an index model as a parameter, specified in a CreateSearchIndexModel instance.

The following example creates an Atlas Search index on the embedded_movies collection. The code creates a CreateSearchIndexModel that sets the index name and enables dynamic mapping. Then, the code passes the CreateSearchIndexModel instance to the SearchIndexes.CreateOne() method to create the Atlas Search index:

var indexModel = new CreateSearchIndexModel(
"example_index",
SearchIndexType.Search,
new BsonDocument {
{ "mappings", new BsonDocument {
{ "dynamic", true },
} }
}
);
var result = movieCollection.SearchIndexes.CreateOne(indexModel);
Console.WriteLine("Created Atlas Search index:\n{0}", result);
Created Atlas Search index:
"example_index"

You can create multiple Atlas Search and Vector Search indexes by calling the SearchIndexes.CreateMany() method on an IMongoCollection instance. This method accepts an IEnumerable of CreateSearchIndexModel instances as a parameter.

This example performs the following actions:

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

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

  3. Passes a List of both CreateSearchIndexModel instances to the SearchIndexes.CreateMany() method

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

var searchModel = new CreateSearchIndexModel(
"as_idx",
SearchIndexType.Search,
new BsonDocument {
{ "mappings", new BsonDocument {
{ "dynamic", true },
} }
}
);
var vectorModel = new CreateSearchIndexModel(
"vs_idx",
SearchIndexType.VectorSearch,
new BsonDocument
{
{ "fields", new BsonArray
{
new BsonDocument
{
{ "type", "vector" },
{ "path", "plot_embedding" },
{ "numDimensions", 1536 },
{ "similarity", "euclidean" }
}
}
}
}
);
var models = new List<CreateSearchIndexModel> { searchModel, vectorModel };
var indexes = movieCollection.SearchIndexes.CreateMany(models);
Console.WriteLine("Created Search indexes:\n{0} {1}", indexes.ToArray());
Created Search indexes:
as_idx vs_idx

You can access information about a collection's existing Atlas Search and Vector Search indexes by calling the SearchIndexes.List() 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 SearchIndexes.List() method and prints a list of the Atlas Search and Vector Search indexes on the collection:

var indexesList = movieCollection.SearchIndexes.List().ToList();
foreach (var i in indexesList)
{
Console.WriteLine(i);
}
{ "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": [...] }

You can update an Atlas Search or Vector Search index by calling the SearchIndexes.Update() method on an IMongoCollection 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 SearchIndexes.Update() method to update the index:

var updatedDef = new BsonDocument
{
{ "fields", new BsonArray
{
new BsonDocument
{
{ "type", "vector" },
{ "path", "plot_embedding" },
{ "numDimensions", 1536 },
{ "similarity", "dotProduct" }
}
}
}
};
movieCollection.SearchIndexes.Update("vs_index", updatedDef);

You can delete an Atlas Search or Vector Search index by calling the SearchIndexes.DropOne() method on an IMongoCollection 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 SearchIndexes.DropOne() method to delete the index:

movieCollection.SearchIndexes.DropOne("example_index");

To learn about other indexes you can create by using the .NET/C# 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