Amazon DocumentDB 的向量搜尋 - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Amazon DocumentDB 的向量搜尋

向量搜尋是機器學習中使用的一種方法,透過使用距離或類似指標來比較向量表示法,以尋找與特定資料點類似的資料點。兩個向量在向量空間中越接近,則視為基礎項目越相似。此技術有助於擷取資料的語意意義。此方法適用於各種應用程式,例如建議系統、自然語言處理和影像辨識。

Amazon DocumentDB 的向量搜尋結合了 JSON 型文件資料庫的彈性和豐富的查詢功能,以及向量搜尋的強大功能。如果您想要使用現有的 Amazon DocumentDB 資料或靈活的文件資料結構來建置機器學習和生成式 AI 使用案例,例如語意搜尋體驗、產品建議、個人化、聊天機器人、詐騙偵測和異常偵測,則 Amazon DocumentDB 的向量搜尋是您的理想選擇。向量搜尋可在 Amazon 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 索引會將向量資料整理成多層圖形。雖然 HNSW 的建置時間比 IVFFlat 慢,但它可提供更好的查詢效能和召回。與 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" }] } );
參數 需求 資料類型 描述 Value (s)

name

選擇性

string

指定索引的名稱。

英數字元

type

選擇性

指定索引的類型。

支援:hnsw 或 ivfflat

預設:HNSW (引擎修補程式 3.0.4574 之後)

dimensions

必要

integer

指定向量資料中的維度。

最多 2,000 個維度。

similarity

必要

string

指定用於相似性計算的距離指標。

  • euclidean

  • cosine

  • dotProduct

lists

IVFFlat 需要

integer

指定 IVFFlat 索引用於分組向量資料的叢集數量。建議設定是最多 1M 文件和sqrt(# of documents)超過 1M 文件的文件/1M000 數量。

下限:1

上限:請參閱功能和限制下列各執行個體類型資料表的清單。

m

選擇性

integer

指定 HNSW 索引的最大連線數

預設:16

範圍 【2, 100】

efConstruction

選擇性

integer

指定動態候選清單的大小,以建構 HNSW 索引的圖形。

efConstruction 必須大於或等於 (2 * m)

預設:64

範圍 【4, 1000】

請務必適當地設定子參數的值,例如 lists for IVFFlat mefConstruction for HNSW,因為它會影響搜尋的準確性/召回、建置時間和效能。較高的清單值會增加查詢的速度,因為它會減少每個清單中的向量數量,導致較小的區域。不過,較小的區域大小可能會導致更多召回錯誤,進而降低準確度。對於 HNSW,增加 的值mefConstruction提高準確性,但也會增加索引建置時間和大小。請參閱以下範例:

範例

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 描述 Value (s)

vectorSearch

必要

operator

在 $search 命令內用於查詢向量。

vector

必要

陣列

指出將用於尋找類似向量的查詢向量。

path

必要

string

定義向量欄位的名稱。

k

必要

integer

指定搜尋傳回的結果數量。

similarity

必要

string

指定用於相似性計算的距離指標。

  • euclidean

  • cosine

  • dotProduct

probes

選擇性

integer

您希望向量搜尋檢查的叢集數量。較高的值以速度的成本提供更好的召回。它可以設定為最接近的鄰搜尋清單數量 (此時規劃器不會使用索引)。開始微調的建議設定為 sqrt(# of lists)

預設:1

efSearch

選擇性

integer

指定 HNSW 索引在搜尋期間使用的動態候選清單大小。較高的 值efSearch可提高召回速度的成本。

預設:40

範圍 【1, 1000】

請務必微調 efSearch(HNSW) 或 probes(IVFFlat) 的值,以達到所需的效能和準確性。請參閱下列範例操作:

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 ] }

功能和限制

版本相容性

  • Amazon DocumentDB 的向量搜尋僅適用於 Amazon DocumentDB 5.0 執行個體型叢集。

向量

  • Amazon DocumentDB 最多可為 2,000 個維度的向量編製索引。不過,如果沒有索引,最多可以存放 16,000 個維度。

索引

  • 對於 IVFFlat 索引建立,清單參數的建議設定是文件數量/1000,最多 1M 個文件和sqrt(# of documents)超過 1M 個文件。由於記憶體有效限制,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

  • 向量索引partial不支援其他索引選項,例如 compoundsparse或 。

  • HNSW 索引不支援平行索引建置。僅支援 IVFFlat 索引。

向量查詢

  • 對於向量搜尋查詢,請務必微調參數,例如 probesefSearch 以獲得最佳結果。probesefSearch 參數的值越高,則召回和速度越高。開始微調探查參數的建議設定是 sqrt(# of lists)

最佳實務

了解在 Amazon DocumentDB 中使用向量搜尋的最佳實務。在新最佳實務確定時,會不斷更新此小節。

  • 使用平面壓縮 (IVFFlat) 索引建立的反轉檔案涉及根據相似性叢集和組織資料點。因此,為了讓索引更有效率,我們建議您在建立索引之前至少載入一些資料。

  • 對於向量搜尋查詢,請務必微調參數,例如 probesefSearch 以獲得最佳結果。probesefSearch 參數的值越高,則召回值越高,速度越低。開始微調probes參數的建議設定是 sqrt(lists)

資源