Atlas Search and Vector Search Indexes
On this page
Overview
You can manage your Atlas Search and Atlas Vector Search indexes by using PyMongo. The indexes specify the behavior of the search and which fields to index.
Atlas Search enables you to perform full-text searches on collections hosted on MongoDB Atlas. Atlas Search indexes specify the behavior of the search and which fields to index.
Atlas Vector Search enables you to perform semantic searches on vector embeddings stored in MongoDB Atlas. Vector Search indexes define the indexes for the vector embeddings that you want to query and the boolean, date, objectId, numeric, string, or UUID values that you want to use to pre-filter your data.
You can call the following methods on a collection to manage your Atlas Search and Vector Search indexes:
create_search_index()
create_search_indexes()
list_search_indexes()
update_search_index()
drop_search_index()
Note
The Atlas Search Index management methods run asynchronously. The
driver methods can return before confirming that they ran
successfully. To determine the current status of the indexes, call the
list_search_indexes()
method.
The following sections provide code examples that demonstrate how to use each of the preceding methods.
Create a Search Index
You can use the create_search_index() and the create_search_indexes() methods to create Atlas Search indexes or Atlas Vector Search indexes.
The following code example shows how to create a single Atlas Search index:
index = { "definition": { "mappings": { "dynamic": True } }, "name": "<index name>", } collection.create_search_index(index)
The following code example shows how to create a single Atlas Vector Search index by using the SearchIndexModel object:
from pymongo.operations import SearchIndexModel search_index_model = SearchIndexModel( definition={ "fields": [ { "type": "vector", "numDimensions": <number of dimensions>, "path": "<field to index>", "similarity": "<select from euclidean, cosine, dotProduct>" } ] }, name="<index name>", type="vectorSearch", ) collection.create_search_index(model=search_index_model)
You can use the create_search_indexes()
method to create multiple indexes. These indexes can be Atlas Search or
Vector Search indexes. The create_search_indexes()
method takes a list of
SearchIndexModel
objects that correspond to each index you want to create.
The following code example shows how to create an Atlas Search index and an Atlas Vector Search index:
search_idx = SearchIndexModel( definition ={ "mappings": { "dynamic": True } }, name="my_index", ) vector_idx = SearchIndexModel( definition={ "fields": [ { "type": "vector", "numDimensions": <number of dimensions>, "path": "<field to index>", "similarity": "<select from euclidean, cosine, dotProduct>" } ] }, name="my_vector_index", type="vectorSearch", ) indexes = [search_idx, vector_idx] collection.create_search_indexes(models=indexes)
List Search Indexes
You can use the list_search_indexes() method to get information about the Atlas Search and Vector Search indexes of a collection.
The following code example shows how to print a list of the search indexes of a collection:
results = list(collection.list_search_indexes()) for index in results: print(index)
Update a Search Index
You can use the update_search_index() method to update an Atlas Search or Vector Search index.
The following code example shows how to update an Atlas Search index:
new_index_definition = { "mappings": { "dynamic": False } } collection.update_search_index("my_index", new_index)
The following code example shows how to update an Atlas Vector Search index:
new_index_definition = { "fields": [ { "type": "vector", "numDimensions": 1536, "path": "<field to index>", "similarity": "euclidean" }, ] } collection.update_search_index("my_vector_index", new_index_definition)
Delete a Search Index
You can use the drop_search_index() method to remove an Atlas Search or Vector Search index.
The following code shows how to delete a search index from a collection:
collection.drop_index("my_index")