Docs Menu
Docs Home
/ /

MongoDB Search Indexes

MongoDB Search enables you to perform full-text searches on collections hosted on MongoDB Atlas. MongoDB 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 MongoDB Search indexes:

  • createSearchIndex()

  • createSearchIndexes()

  • listSearchIndexes()

  • updateSearchIndex()

  • dropSearchIndex()

Note

The MongoDB 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.

You can use the createSearchIndex() and the createSearchIndexes() methods to create one or more MongoDB Search indexes.

You can also use these methods to create MongoDB Vector Search Indexes. MongoDB Vector Search enables you to perform semantic searches on vector embeddings stored in MongoDB Atlas. To learn more about this feature, see the MongoDB Vector Search Overview.

The following code example shows how to create a MongoDB Search index:

val index = Document("mappings" -> Document("dynamic" -> true))
collection.createSearchIndex("<index name>", index)
.subscribe((result: String) => ())

The following code example shows how to create multiple indexes. Unlike the createSearchIndex() method, which assigns a default name to the created index, you must provide index names for each index when using the createSearchIndexes() method.

val indexOne = SearchIndexModel("<first index name>", Document("mappings" -> Document("dynamic" -> true, "fields" -> Document("field1" -> Document("type" -> "string")))))
val indexTwo = SearchIndexModel("<second index name>", Document("mappings" -> Document("dynamic" -> false, "fields" -> Document("field2" -> Document("type" -> "string")))))
collection.createSearchIndexes(List(indexOne, indexTwo))
.subscribe((result: String) => ())

To learn more about the syntax used to define MongoDB Search indexes, see the Review MongoDB Search Index Syntax guide in the Atlas manual.

You can use the listSearchIndexes() method to return all MongoDB Search indexes in a collection.

The following code example shows how to print a list of the search indexes in a collection by subscribing to the Observable returned by the listSearchIndexes() method:

collection.listSearchIndexes()
.subscribe((result: Document) => println(result.toJson()))
{"id": "...", "name": "<index name 1>", "type": "search", "status": "READY", "queryable": true, ... }
{"id": "...", "name": "<index name 2>", "type": "search", "status": "READY", "queryable": true, ... }

You can use the updateSearchIndex() method to update a MongoDB Search index.

The following code shows how to update a search index:

val updateIndex = Document("mappings" -> Document("dynamic" -> false))
collection.updateSearchIndex("<index to update>", updateIndex)
.subscribe((result: Unit) => ())

You can use the dropSearchIndex() method to delete a MongoDB Search index.

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

collection.dropSearchIndex("<index name>")
.subscribe((result: Unit) => ())

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

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

Back

Multikey

On this page