Atlas Search Indexes
On this page
Overview
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.
You can call the following methods on a collection to manage your Atlas Search indexes:
createSearchIndex()
createSearchIndexes()
listSearchIndexes()
updateSearchIndex()
dropSearchIndex()
Note
The Atlas Search index management methods run asynchronously and might return before
confirming that they ran successfully. To determine the current status of the indexes,
call the listSearchIndexes()
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 createSearchIndex() and the createSearchIndexes() methods to create one or more Atlas Search indexes.
You can also use these methods to create Atlas Vector Search Indexes. Atlas Vector Search enables you to perform semantic searches on vector embeddings stored in MongoDB Atlas. To learn more about this feature, see the Atlas Vector Search Overview.
The following code example shows how to create an Atlas Search index:
val index = Document("mappings", Document("dynamic", true)) collection.createSearchIndex("<index name>", index)
The following code example shows how to create multiple indexes:
val indexOne = SearchIndexModel("<first index name>", Document("mappings", Document("dynamic", true))) val indexTwo = SearchIndexModel("<second index name>", Document("mappings", Document("dynamic", true))) collection.createSearchIndexes(listOf(indexOne, indexTwo))
To learn more about the syntax used to define Atlas Search indexes, see the Review Atlas Search Index Syntax guide in the Atlas manual.
List Search Indexes
You can use the listSearchIndexes() method to return all Atlas Search indexes in a collection.
The following code example shows how to print a list of the search indexes in a collection:
val results = collection.listSearchIndexes() results.forEach { result -> println(result) }
Update a Search Index
You can use the updateSearchIndex() method to update an Atlas Search index.
The following code shows how to update a search index:
val newIndex = Document("mappings", Document("dynamic", true)) collection.updateSearchIndex("<index to update>", newIndex)
Delete a Search Index
You can use the dropSearchIndex() method to delete an Atlas Search index.
The following code shows how to delete a search index from a collection:
collection.dropIndex("<index to delete>")
Additional Information
To learn more about MongoDB Atlas Search, see the Atlas Search Indexes documentation.