对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

MongoDB 搜索 和 MongoDB Vector 搜索索引

在本指南中,您可以学习如何使用 Ruby 驱动程序以编程方式管理 MongoDB Search 和 MongoDB Vector Search 搜索索引。

MongoDB Search 功能使您能够对托管在 MongoDB 上的集合执行全文搜索。要了解有关 MongoDB Search 的更多信息,请参阅 MongoDB Search 概述。

MongoDB Vector Search 使您能够对存储在 MongoDB 中的向量嵌入执行语义搜索。要了解有关 MongoDB Vector Search 的更多信息,请参阅 MongoDB Vector Search 概述。

您可以调用以下方法来管理MongoDB Search 和 MongoDB Vector Search 搜索索引:

  • search_indexes#create_one

  • search_indexes#create_many

  • search_indexes#update_one

  • search_indexes#drop_one

注意

MongoDB Search 和 MongoDB 向量搜索 索引管理是异步的

Ruby驱动程序异步管理MongoDB Search 和 MongoDB Vector Search 搜索索引。以下部分中描述的方法会立即返回服务器响应,但对搜索索引的更改会在背景进行,可能要稍后一段时间才能完成。

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

要创建单个MongoDB Search 或MongoDB 向量搜索索引,请使用 search_indexes#create_one 方法。要创建多个索引,请使用 search_indexes#create_many 方法。这两个方法都会立即返回,而索引是在背景异步创建的。

以下代码示例展示了如何通过提供索引定义和可选的索引名称来创建MongoDB 搜索索引:

# Creates indexes on all dynamically indexable fields with a default index name
collection.search_indexes.create_one(
{ mappings: { dynamic: true } }
)
# Creates an index on the specified field with the specified index name
index_definition = {
mappings: {
dynamic: false,
fields: {
fullplot: { type: 'string' }
}
}
}
collection.search_indexes.create_one(index_definition, name: 'mySearchIndex')

注意

默认下,如果不传递 type 参数,驱动程序会创建MongoDB 搜索索引。要创建MongoDB 向量搜索 索引,必须在调用 create_one 时将 type 参数设立为 'vectorSearch'。

通过提供索引规范数组,您可以使用 search_indexes#create_many 创建多个MongoDB 搜索或MongoDB Vector 搜索 索引。每个索引规范应包括以下部分:

  • definition 参数:定义索引

  • name 参数:指定索引名称

  • type 参数:指定索引类型('search' 或 'vectorSearch')

以下代码示例展示了如何在一次调用中同时创建MongoDB 搜索索引和MongoDB Vector 搜索索引:

index_spec_1 = {
name: 'searchIndex_plot',
type: 'search',
definition: {
mappings: {
dynamic: false,
fields: {
plot: { type: 'string' }
}
}
}
}
index_spec_2 = {
name: 'vsIndex_plot_embedding',
type: 'vectorSearch',
definition: {
fields: [
{
type: "vector",
path: "plot_embedding",
numDimensions: 1536,
similarity: "dotProduct"
}
]
}
}
collection.search_indexes.create_many([index_spec_1, index_spec_2])

对于较长的索引定义,在方法调用之外定义索引会很有帮助。要学习有关索引定义语法的更多信息,请参阅 Atlas 手册中的查看 MongoDB 搜索索引语法或如何为 MongoDB 向量搜索编制字段索引指南。

要更新 MongoDB Search 或 MongoDB 向量搜索索引,请使用 search_indexes#update_one 方法。

要更新索引,必须提供新的索引定义。您必须使用索引的 name 或 id 来指定要更新的索引。以下代码展示了如何更新MongoDB搜索索引:

updated_definition = {
mappings: {
dynamic: false,
fields: { fullplot: { type: 'string' } }
}
}
# Specifies the index to update by using the index name
collection.search_indexes.update_one(updated_definition, name: 'searchIndex_plot')
# Specifies the index to update by using the index id
collection.search_indexes.update_one(updated_definition, id: <index id>)

要删除MongoDB Search 或MongoDB Vector Search索引,请使用 search_indexes#drop_one 方法。

要删除索引,您必须提供索引的 id 或 name。以下代码展示了如何从集合中删除搜索索引:

# Specifies the index to delete by using the index name
collection.search_indexes.drop_one(name: 'searchIndex_plot')
# Specifies the index to delete by using the index id
collection.search_indexes.drop_one(id: <index id>)

您可以使用 search_indexes 对象列出集合上每个 MongoDB 搜索 和 MongoDB 向量搜索 索引的完整索引规范:

puts collection.search_indexes.collect(&:to_json)

要在每个索引的索引规范中列出单独的字段,请遍历 search_indexes 对象:

collection.search_indexes.each do |index_spec|
p index_spec['id']
p index_spec['name']
p index_spec['status']
p index_spec['queryable']
p index_spec['latestDefinition']
end

要了解有关MongoDB 搜索的更多信息,请参阅MongoDB 搜索文档。

要学习有关 MongoDB 向量搜索 的更多信息,请参阅 Ruby 驱动程序的 运行 MongoDB 向量搜索 查询 指南或 MongoDB 向量搜索 文档。

要进一步了解本指南所讨论的任何方法,请参阅以下 API 文档: