Docs 菜单
Docs 主页
/ /

MongoDB Search 和MongoDB Vector Search 索引

在本指南中,您可以学习;了解如何使用Ruby驾驶员以编程方式管理MongoDB Search 和MongoDB Vector Search 索引。

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

MongoDB Vector Search 使您能够对存储在MongoDB Atlas中的向量嵌入执行语义搜索。要学习;了解有关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 Vector Search 索引管理是异步的

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

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

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

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

# 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 Search索引。要创建MongoDB Vector Search索引,必须在调用 create_one 时将 type 参数设立为 'vectorSearch'

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

  • definition 参数:定义索引

  • name 参数:指定索引名称

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

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

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 Search 索引事务语法”或“如何为MongoDB Vector Search 编制字段索引”指南。

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

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

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 方法。

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

# 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 Search 和MongoDB Vector Search索引的完整索引规范:

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 Search 的更多信息,请参阅MongoDB Search 文档。

要学习;了解有关MongoDB Vector Search 的更多信息,请参阅Ruby驱动程序的 运行MongoDB Vector Search 查询指南或MongoDB Vector Search 文档。

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

后退

Multikey

在此页面上