Docs Menu
Docs Home
/ /

MongoDB Search and Vector Search Indexes

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

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.

MongoDB 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 MongoDB Search and Vector Search indexes:

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

  • createSearchIndexes()

  • listSearchIndexes()

  • updateSearchIndex()

  • dropSearchIndex()

Note

The MongoDB 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 MongoDB Search index. You cannot use this method to create a Vector Search index.

You can use the createSearchIndexes() method to create multiple MongoDB 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 a MongoDB Search index:

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

The following code example shows how to create MongoDB 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 MongoDB Search indexes, see the Review MongoDB Search Index Syntax guide in the Atlas documentation.

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:

val results = collection.listSearchIndexes()
results.forEach { result ->
println(result)
}

You can use the updateSearchIndex() method to update a MongoDB 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 a MongoDB 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 Search, see the MongoDB Search Indexes documentation.

Back

Indexes

On this page