Docs Menu

Docs HomeDevelop ApplicationsPython DriversPyMongo

Atlas Search Indexes

On this page

  • Overview
  • Create a Search Index
  • List Search Indexes
  • Update a Search Index
  • Delete a Search Index

The Atlas Search feature enables you to perform full-text searches on collections hosted on MongoDB Atlas. The indexes specify the behavior of the search and which fields to index.

To learn more about MongoDB Atlas Search, see the Atlas Search Indexes documentation.

You can call the following methods on a collection to manage your Atlas 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.

You can use the create_search_index() and the create_search_indexes() methods to create Atlas Search indexes.

The following code example shows how to create a single index:

index = {
"definition": {
"mappings": {
"dynamic": True
}
},
"name": "<index name>",
}
collection.create_search_index(index)

The following code example shows how to create multiple indexes:

index_one = {
"definition": {
"mappings": {
"dynamic": True
}
},
"name": "my_index",
}
index_two = {
"definition": {
"mappings": {
"dynamic": True
}
},
"name": "my_other_index",
}
indexes = [index_one, index_two]
collection.create_search_indexes(models=indexes)

You can use the list_search_indexes() method to return the Atlas 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)

You can use the update_search_index() method to update an Atlas Search index.

The following code shows how to update a search index:

new_index = {
"definition": {
"mappings": {
"dynamic": True
}
},
"name": "my_new_index",
}
collection.update_search_index("my_index", new_index)

You can use the drop_search_index() method to remove an Atlas Search index.

The following code shows how to delete a search index from a collection:

collection.drop_index("my_index")
← Multikey Indexes