MongoDB.local SF, Jan 15: See the speaker lineup & ship your AI vision faster. Use WEB50 to save 50%
Find out more >
Docs 菜单
Docs 主页
/ /
使用索引
/ / /

Atlas Search 索引

Atlas Search 使您能够对 MongoDB Atlas 上托管的集合执行全文搜索。Atlas Search 索引指定了搜索行为以及要索引的字段。

您可以在集合上调用以下方法来管理 Atlas Search 索引:

  • createSearchIndex()

  • createSearchIndexes()

  • listSearchIndexes()

  • updateSearchIndex()

  • dropSearchIndex()

注意

Atlas Search索引管理方法异步运行,可能会在确认运行成功之前返回。要确定索引的当前状态,请调用 listSearchIndexes() 方法。

以下各节将提供代码示例,演示如何使用上述每种方法。

您可以使用 createSearchIndex()createSearchIndexes() 方法创建一个或多个Atlas Search索引。

以下代码示例展示了如何创建单个索引:

val index = Document("mappings", Document("dynamic", true))
collection.createSearchIndex("<index name>", index)

以下代码示例展示了如何创建多个索引:

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

要详细了解用于定义 Atlas Search 索引的语法,请参阅 Atlas 手册中的查看 Atlas Search 索引语法指南。

您可以使用 listSearchIndexes() 方法返回集合中的所有 Atlas Search 索引。

以下代码示例显示如何打印集合的搜索索引列表:

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

您可以使用 updateSearchIndex() 方法更新 Atlas Search 索引。

以下代码展示如何更新搜索索引:

val newIndex = Document("mappings", Document("dynamic", true))
collection.updateSearchIndex("<index to update>", newIndex)

您可以使用 dropSearchIndex() 方法删除 Atlas Search 索引。

以下代码展示了如何从集合中删除搜索索引:

collection.dropIndex("<index to delete>")

如需了解有关 MongoDB Atlas Search 的更多信息,请参阅“Atlas Search 索引”文档。

后退

复合索引

在此页面上