Docs Menu
Docs Home
/ / /
Kotlin Sync Driver
/ /

Atlas Search and Vector Search Indexes

On this page

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

You can programmatically manage your Atlas Search and Atlas Vector Search indexes by using the Kotlin Sync driver.

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:

  • createSearchIndex() (valid for Atlas Search indexes only)

  • createSearchIndexes()

  • listSearchIndexes()

  • updateSearchIndex()

  • dropSearchIndex()

Note

The Atlas Search and Vector 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 or view the indexes list in the Atlas UI.

The following sections provide code examples that demonstrate how to use each of the preceding methods.

You can use the createSearchIndex() method to create a single Atlas Search index. You cannot use this method to create a Vector Search index.

You can use the createSearchIndexes() method to create multiple Atlas Search or Vector Search indexes. You must create a SearchIndexModel instance for each index, then pass a list of SearchIndexModel instances to the createSearchIndexes() method.

The following code example shows how to create an Atlas Search index:

val index = Document("mappings", Document("dynamic", true))
collection.createSearchIndex("mySearchIdx", index)

The following code example shows how to create Atlas Search and Vector Search indexes in one call:

val searchIdxMdl = SearchIndexModel(
"searchIdx",
Document("analyzer", "lucene.standard").append(
"mappings", Document("dynamic", true)
),
SearchIndexType.search()
)
val vectorSearchIdxMdl = SearchIndexModel(
"vsIdx",
Document(
"fields",
listOf(
Document("type", "vector")
.append("path", "embeddings")
.append("numDimensions", 1536)
.append("similarity", "dotProduct")
)
),
SearchIndexType.vectorSearch()
)
collection.createSearchIndexes(
listOf(searchIdxMdl, vectorSearchIdxMdl)
)

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

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)
}

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)

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>")

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

Back

Compound Indexes