向量搜索 Amazon DocumentDB - Amazon DocumentDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

向量搜索 Amazon DocumentDB

向量搜索是机器学习中使用的一种方法,它通过使用距离或相似度量度比较向量表示法来查找与给定数据点相似的数据点。两个向量在向量空间中越接近,则认为底层项目就越相似。这种技术有助于捕捉数据的语义含义。这种方法在各种应用中都很有用,例如推荐系统、自然语言处理和图像识别。

Amazon DocumentDB 的矢量搜索将基于 JSON 的文档数据库的灵活性和丰富的查询功能与矢量搜索的强大功能相结合。如果您想使用现有的 Amazon DocumentDB 数据或灵活的文档数据结构来构建机器学习和生成式 AI 用例,例如语义搜索体验、产品推荐、个性化、聊天机器人、欺诈检测和异常检测,那么 Amazon DocumentDB 的矢量搜索是您的理想选择。基于亚马逊 DocumentDB 5.0 实例的集群提供矢量搜索。

插入向量

要将矢量插入您的 Amazon DocumentDB 数据库,您可以使用现有的插入方法:

示例

在以下示例中,在测试数据库中创建了由五个文档组成的集合。每个文档都包含两个字段:产品名称及其相应的矢量嵌入。

db.collection.insertMany([ {"product_name": "Product A", "vectorEmbedding": [0.2, 0.5, 0.8]}, {"product_name": "Product B", "vectorEmbedding": [0.7, 0.3, 0.9]}, {"product_name": "Product C", "vectorEmbedding": [0.1, 0.2, 0.5]}, {"product_name": "Product D", "vectorEmbedding": [0.9, 0.6, 0.4]}, {"product_name": "Product E", "vectorEmbedding": [0.4, 0.7, 0.2]} ]);

创建向量索引

Amazon DocumentDB 支持分层可导航小世界 (HNSW) 索引和采用平面压缩的倒置文件 (IVFFLat) 索引方法。ivfFLat 索引将向量分成列表,然后搜索这些列表中最接近查询向量的选定子集。另一方面,HNSW 索引将向量数据组织成多层图。尽管与 ivffLat 相比,HNSW 的构建时间较慢,但它提供了更好的查询性能和召回率。与 ivfFLat 不同,HNSW 不涉及训练步骤,因此无需加载任何初始数据即可生成索引。对于大多数用例,我们建议使用 HNSW 索引类型进行矢量搜索。

如果您不创建向量索引,Amazon DocumentDB 会执行精确的最近邻搜索,从而确保完美的调用。但是,在生产场景中,速度至关重要。我们建议使用向量索引,这可能会以一些回调来提高速度。请务必注意,添加向量索引可能会导致不同的查询结果。

模板

您可以使用以下内容createIndexrunCommand模板在向量场上构建向量索引:

Using createIndex

在某些驱动程序(例如 mongosh 和 Java)中,使用中的vectorOptions参数createIndex可能会导致错误。在这种情况下,我们建议使用runCommand

db.collection.createIndex( { "<vectorField>": "vector" }, { "name": "<indexName>", "vectorOptions": { "type": " <hnsw> | <ivfflat> ", "dimensions": <number_of_dimensions>, "similarity": " <euclidean> | <cosine> | <dotProduct> ", "lists": <number_of_lists> [applicable for IVFFlat], "m": <max number of connections> [applicable for HNSW], "efConstruction": <size of the dynamic list for index build> [applicable for HNSW] } } );
Using runCommand

在某些驱动程序(例如 mongosh 和 Java)中,使用中的vectorOptions参数createIndex可能会导致错误。在这种情况下,我们建议使用runCommand

db.runCommand( { "createIndexes": "<collection>", "indexes": [{ key: { "<vectorField>": "vector" }, vectorOptions: { type: " <hnsw> | <ivfflat> ", dimensions: <number of dimensions>, similarity: " <euclidean> | <cosine> | <dotProduct> ", lists: <number_of_lists> [applicable for IVFFlat], m: <max number of connections> [applicable for HNSW], efConstruction: <size of the dynamic list for index build> [applicable for HNSW] }, name: "myIndex" }] } );
参数 要求 数据类型 描述

name

可选

字符串

指定索引的名称。

字母数字

type

可选

指定索引的类型。

支持:hnsw 或 ivfflat

默认:HNSW(引擎补丁 3.0.4574 及更高版本)

dimensions

必需

整数

指定矢量数据中的维数。

最大尺寸为 2,000 个。

similarity

必需

字符串

指定用于相似度计算的距离度量。

  • euclidean

  • cosine

  • dotProduct

lists

ivffLat 必填项

整数

指定 ivfFLat 索引用于对矢量数据进行分组的聚类数。对于最多 100 万个文档和sqrt(# of documents)超过 100 万个文档,推荐的设置是文档数量/1000。

最小值:1

最大值:请参阅下方每种实例类型的列表特征和限制表。

m

可选

整数

指定 HNSW 索引的最大连接数

默认值:16

射程 [2, 100]

efConstruction

可选

整数

指定用于构建 HNSW 指数图表的动态候选列表的大小。

efConstruction必须大于或等于 (2* m)

默认值:64

射程 [4, 1000]

请务必适当设置子参数的值,例如 ivffLat mefConstruction HNSW 的子参数值,因为这会影响搜索的准确性/召回率、构建时间和性能。lists列表值越高,查询速度越快,因为这会减少每个列表中的向量数量,从而产生较小的区域。但是,较小的区域大小可能会导致更多的召回错误,从而降低精度。对于 HNSW 来说,efConstruction增加值m并提高准确度,但也会增加索引构建时间和大小。请参阅以下示例:

示例

HNSW
db.collection.createIndex( { "vectorEmbedding": "vector" }, { "name": "myIndex", "vectorOptions": { "type": "hnsw", "dimensions": 3, "similarity": "euclidean", "m": 16, "efConstruction": 64 } } );
IVFFlat
db.collection.createIndex( { "vectorEmbedding": "vector" }, { "name": "myIndex", "vectorOptions": { "type": "ivfflat", "dimensions": 3, "similarity": "euclidean", "lists":1 } } )

获取索引定义

您可以使用以下getIndexes命令查看索引的详细信息,包括向量索引:

示例

db.collection.getIndexes()

示例输出

[ { "v" : 4, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.collection" }, { "v" : 4, "key" : { "vectorEmbedding" : "vector" }, "name" : "myIndex", "vectorOptions" : { "type" : "ivfflat", "dimensions" : 3, "similarity" : "euclidean", "lists" : 1 }, "ns" : "test.collection" } ]

查询向量

向量查询模板

使用以下模板查询向量:

db.collection.aggregate([ { $search: { "vectorSearch": { "vector": <query vector>, "path": "<vectorField>", "similarity": "<distance metric>", "k": <number of results>, "probes":<number of probes> [applicable for IVFFlat], "efSearch":<size of the dynamic list during search> [applicable for HNSW] } } } ]);
参数 要求 Type 描述

vectorSearch

必需

operator

在 $search 命令中用于查询向量。

vector

必需

数组

表示将用于查找相似向量的查询向量。

path

必需

字符串

定义向量场的名称。

k

必需

整数

指定搜索返回的结果数。

similarity

必需

字符串

指定用于相似度计算的距离度量。

  • euclidean

  • cosine

  • dotProduct

probes

可选

整数

要向量搜索检查的聚类数量。值越高,回忆效果越好,但速度会降低。可以将其设置为列表数量,以进行精确的最近邻搜索(此时计划器将不使用索引)。开始微调的推荐设置为sqrt(# of lists)

默认值:1

efSearch

可选

整数

指定 HNSW 索引在搜索期间使用的动态候选列表的大小。值越高,efSearch可以提高召回率,但会降低速度。

原定设置值:40

射程 [1, 1000]

必须微调 efSearch (HNSW) 或 probes (iVFlat) 的值,以实现所需的性能和精度。请参阅以下示例操作:

HNSW
db.collection.aggregate([ { $search: { "vectorSearch": { "vector": [0.2, 0.5, 0.8], "path": "vectorEmbedding", "similarity": "euclidean", "k": 2, "efSearch": 40 } } } ]);
IVFFlat
db.collection.aggregate([ { $search: { "vectorSearch": { "vector": [0.2, 0.5, 0.8], "path": "vectorEmbedding", "similarity": "euclidean", "k": 2, "probes": 1 } } } ]);

示例输出

此操作的输出将类似于以下内容:

{ "_id" : ObjectId("653d835ff96bee02cad7323c"), "product_name" : "Product A", "vectorEmbedding" : [ 0.2, 0.5, 0.8 ] } { "_id" : ObjectId("653d835ff96bee02cad7323e"), "product_name" : "Product C", "vectorEmbedding" : [ 0.1, 0.2, 0.5 ] }

特征和限制

版本兼容性

  • 亚马逊 DocumentDB 的矢量搜索仅适用于基于亚马逊 DocumentDB 5.0 实例的集群。

向量

  • 亚马逊 DocumentDB 可以索引最多 2,000 个维度的向量。但是,在没有索引的情况下最多可以存储 16,000 个维度。

索引

  • 对于创建 ivfFLat 索引,列表参数的推荐设置为最多 100 万个文档和sqrt(# of documents)超过 100 万个文档的文档数量/1000。由于工作内存限制,Amazon DocumentDB 支持列表参数的一定最大值,具体取决于维度的数量。下表提供了维度为 500、1000 和 2,000 的向量的列表参数的最大值供您参考:

    实例类型 包含 500 个维度的列表 包含 1000 个维度的列表 有 2000 个维度的清单

    t3.med

    372

    257

    150

    r5.l

    915

    741

    511

    r5.xl

    1,393

    1,196

    901

    r5.2xl

    5,460

    5,230

    4,788

    r5.4xl

    7,842

    7,599

    7,138

    r5.8xl

    11,220

    10,974

    10,498

    r5.12xl

    13,774

    13,526

    13,044

    r5.16xl

    15,943

    15,694

    15,208

    r5.24xl

    19,585

    19,335

    18,845

  • 向量索引不支持其他索引选项compound,例如sparsepartial

  • HNSW 索引不支持并行索引构建。只有 ivfFlat 索引支持它。

向量查询

  • 对于向量搜索查询,为了获得最佳结果,必须微调probesefSearch等参数。probesefSearch参数的值越高,召回率越高,速度越低。开始微调探针参数的推荐设置为sqrt(# of lists)

最佳实践

学习在 Amazon DocumentDB 中使用矢量搜索的最佳实践。随着新的最佳实践的确定,此节将不断更新。

  • 使用平面压缩的倒置文件 (IVFFLat) 索引的创建涉及根据相似性对数据点进行聚类和组织。因此,为了使索引更加有效,我们建议您在创建索引之前至少加载一些数据。

  • 对于矢量搜索查询,必须微调参数(例如probes或)efSearch以获得最佳结果。probesefSearch参数的值越高,召回率越高,速度越低。开始微调probes参数的推荐设置为sqrt(lists)

资源