

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

# Amazon DocumentDB 的向量搜尋
<a name="vector-search"></a>

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

Amazon DocumentDB 的向量搜尋結合了 JSON 型文件資料庫的彈性和豐富的查詢功能，以及向量搜尋的強大功能。如果您想要使用現有的 Amazon DocumentDB 資料或靈活的文件資料結構來建置機器學習和生成式 AI 使用案例，例如語意搜尋體驗、產品建議、個人化、聊天機器人、詐騙偵測和異常偵測，則 Amazon DocumentDB 的向量搜尋是您的理想選擇。向量搜尋可在 Amazon DocumentDB 5.0 執行個體型叢集上使用。

**Topics**
+ [插入向量](#w2aac21c11b9)
+ [建立向量索引](#w2aac21c11c11)
+ [取得索引定義](#w2aac21c11c13)
+ [查詢向量](#w2aac21c11c15)
+ [功能和限制](#vector-limitations)
+ [最佳實務](#w2aac21c11c19)

## 插入向量
<a name="w2aac21c11b9"></a>

若要將向量插入 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]}
]);
```

## 建立向量索引
<a name="w2aac21c11c11"></a>

Amazon DocumentDB 支援階層式可導覽小型世界 (HNSW) 索引和使用平面壓縮 (IVFFlat) 索引方法的反轉檔案。IVFFlat 索引會將向量分隔為清單，然後搜尋最接近查詢向量的所選清單子集。另一方面，HNSW 索引會將向量資料整理成多層圖形。雖然 HNSW 的建置時間比 IVFFlat 慢，但可提供更好的查詢效能和召回。與 IVFFlat 不同，HNSW 不涉及任何訓練步驟，因此無需載入任何初始資料即可產生索引。對於大多數使用案例，我們建議使用 HNSW 索引類型進行向量搜尋。

如果您未建立向量索引，Amazon DocumentDB 會執行最接近的鄰近搜尋，以確保完美召回。不過，在生產案例中，速度至關重要。我們建議使用向量索引，這可能會交易一些召回以提高速度。請務必注意，新增向量索引可能會導致不同的查詢結果。

**範本**

您可以使用下列 `createIndex`或 `runCommand` 範本，在向量欄位上建置向量索引：

------
#### [ 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" 
      }] 
  }
);
```

------


| 參數 | 需求 | 資料類型 | Description | 值 (s) | 
| --- | --- | --- | --- | --- | 
|  **name**  |  選擇性  |  string  |  指定索引的名稱。  |  英數字元  | 
|  **type**  |  選擇性  |    |  指定索引的類型。  |  支援：hnsw 或 ivfflat 預設：HNSW （引擎修補程式 3.0.4574 以上）  | 
|  **dimensions**  |  必要  |  integer  |  指定向量資料中的維度數目。  |  最多 2，000 個維度。  | 
|  **similarity**  |  必要  |  string  |  指定用於相似性計算的距離指標。  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/documentdb/latest/developerguide/vector-search.html)  | 
|  **lists**  |  IVFFlat 需要  |  integer  |  指定 IVFFlat 索引用來將向量資料分組的叢集數量。建議設定是最多 100 1M份文件/1000 份文件的數量`sqrt(# of documents)`，以及超過 1M份文件的數量。  |  下限：1 上限：請參閱[功能和限制](#vector-limitations)下列各執行個體類型的清單。  | 
|  **m**  |  選擇性  |  integer  |  指定 HNSW 索引的最大連線數  |  預設：16 範圍 【2， 100】  | 
|  **efConstruction**  |  選擇性  |  integer  |  指定用於建構 HNSW 索引圖形的動態候選清單大小。 `efConstruction` 必須大於或等於 (2 \$1 m)  |  預設：64 範圍 【4， 1000】  | 

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

**範例**

------
#### [ 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
    }
  }
)
```

------

## 取得索引定義
<a name="w2aac21c11c13"></a>

您可以使用 `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"
 }
]
```

## 查詢向量
<a name="w2aac21c11c15"></a>

Amazon DocumentDB 支援兩個向量搜尋運算子來查詢向量：

### 傳統向量搜尋運算子
<a name="w2aac21c11c15b5"></a>

使用下列範本來查詢向量：

```
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 | 說明 | 值 (s) | 
| --- | --- | --- | --- | --- | 
|  **vectorSearch**  |  必要  |  operator  |  在 \$1search 命令內用於查詢向量。  |    | 
|  **vector**  |  必要  |  陣列  |  指出將用於尋找類似向量的查詢向量。  |    | 
|  **path**  |  必要  |  string  |  定義向量欄位的名稱。  |    | 
|  **k**  |  必要  |  integer  |  指定搜尋傳回的結果數目。  |    | 
|  **similarity**  |  必要  |  string  |  指定用於相似性計算的距離指標。  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/documentdb/latest/developerguide/vector-search.html)  | 
|  **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 ] }
```

### `$vectorSearch` 運算子 （可在 Amazon DocumentDB 8.0 或更新版本中使用）
<a name="w2aac21c11c15b7"></a>

使用下列範本來查詢向量：

```
db.collection.aggregate([
{
  "$vectorSearch": {
    "exact": true | false,
    "index": "<index-name>" [supports only HNSW index],
    "limit": <number-of-results> [same as k],
    "path": "<vector field-to-search>",
    "queryVector": <array-of-numbers>,
    "numCandidates": <number-of-candidates> [same as efSearch], 
  }
}])
```

## 功能和限制
<a name="vector-limitations"></a>

**版本相容性**
+ Amazon DocumentDB 的向量搜尋僅適用於 Amazon DocumentDB 5.0\$1 執行個體型叢集。

**向量**
+ Amazon DocumentDB 最多可為 2，000 個維度的向量編製索引。不過，如果沒有索引，最多可儲存 16，000 個維度。

**索引**
+ 對於 IVFFlat 索引建立，清單參數的建議設定是最多 100 1M文件和`sqrt(# of documents)`超過 100 萬份文件的文件/1M000。由於可用的記憶體限制，Amazon DocumentDB 支援清單參數的特定最大值，取決於維度數目。下表提供向量 500、1000 和 2，000 維度的清單參數最大值供您參考：    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/documentdb/latest/developerguide/vector-search.html)
+ 向量索引`partial`不支援其他索引選項，例如 `compound``sparse`或 。
+ Amazon DocumentDB 5.0 中的 HNSW 索引不支援平行索引建置。

**向量查詢**
+ 對於向量搜尋查詢，請務必微調參數，例如 `probes`或 `efSearch` 以獲得最佳結果。`probes` 或 `efSearch` 參數的值越高，取回值越高，速度也越低。開始微調探查參數的建議設定是 `sqrt(# of lists)`。

## 最佳實務
<a name="w2aac21c11c19"></a>

了解在 Amazon DocumentDB 中使用向量搜尋的最佳實務。在新最佳實務確定時，會不斷更新此小節。
+ 使用平面壓縮 (IVFFlat) 索引建立的反轉檔案涉及根據相似性叢集和組織資料點。因此，為了讓索引更有效率，建議您在建立索引之前至少載入一些資料。
+ 對於向量搜尋查詢，請務必微調參數，例如 `probes`或 `efSearch` 以獲得最佳結果。`probes` 或 `efSearch` 參數的值越高，召回率越高，速度越低。開始微調`probes`參數的建議設定為 `sqrt(lists)`。

**資源**
+ [向量搜尋什麼是新的部落格文章](https://aws.amazon.com/blogs/aws/vector-search-for-amazon-documentdb-with-mongodb-compatibility-is-now-generally-available)
+ [語意搜尋程式碼範例](https://github.com/aws-samples/amazon-documentdb-samples/tree/master/blogs/semanticsearch-docdb)
+ [Amazon DocumentDB 向量搜尋程式碼範例](https://github.com/aws-samples/amazon-documentdb-samples/tree/master/samples/vector-search)