Docs Menu
Docs Home
/ / /
Scala
/

Atlas Search Indexes

On this page

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

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.

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)
.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 Atlas Search indexes, see the Review Atlas Search Index Syntax guide in the Atlas manual.

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 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 an Atlas 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 an Atlas 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 Atlas Search, see the Atlas Search documentation.

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

Back

Multikey