

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

# 使用向量搜尋集合
<a name="serverless-vector-search"></a>

OpenSearch Serverless 中的*向量搜尋*集合類型提供可擴展且高效能的相似性搜尋功能。這可讓您輕鬆地建置現代機器學習 (ML) 增強型搜尋體驗和生成式人工智慧 (AI) 應用程式，而無需管理基礎向量資料庫基礎設施。

向量搜尋集合的使用案例包括影像搜尋、文件搜尋、音樂擷取、產品建議、影片搜尋、以位置為基礎的搜尋、詐騙偵測和異常偵測。

由於 OpenSearch Serverless 的向量引擎由 OpenSearch 中的 [k 近鄰 (k-NN) 搜尋功能](https://opensearch.org/docs/latest/search-plugins/knn/index/)提供支援，因此您可以簡單地取得相同的功能和無伺服器環境。引擎支援 [k-NN 外掛程式 API](https://opensearch.org/docs/latest/search-plugins/knn/api/)。透過這些操作，您可以利用全文搜尋、進階篩選、彙總、地理空間查詢、巢狀查詢，以更快速地擷取資料，以及增強的搜尋結果。

向量引擎提供距離指標，例如歐幾里德距離、餘弦相似性和點產品相似性，可容納 16，000 個維度。您可以存放具有各種中繼資料資料類型的欄位，例如數字、布林值、日期、關鍵字和地理位置。您也可以使用文字存放欄位以取得描述性資訊，以將更多內容新增至存放的向量。協調資料類型可降低複雜性、提高可維護性，並避免資料重複、版本相容性挑戰和授權問題。

**注意**  
記下以下資訊：  
Amazon OpenSearch Serverless 支援 Faiss 16 位元純量量化，可用於在 32 位元浮點數和 16 位元向量之間執行轉換。若要進一步了解，請參閱 [ Faiss 16 位元純量量化](https://opensearch.org/docs/latest/search-plugins/knn/knn-vector-quantization/#faiss-16-bit-scalar-quantization)。您也可以使用二進位向量來降低記憶體成本。如需詳細資訊，請參閱[二進位向量](https://opensearch.org/docs/latest/field-types/supported-field-types/knn-vector#binary-vectors)。
Amazon OpenSearch Serverless 支援磁碟型向量搜尋。磁碟型向量搜尋可大幅降低低記憶體環境中向量工作負載的操作成本。如需詳細資訊，請參閱[磁碟型向量搜尋](https://docs.opensearch.org/2.19/vector-search/optimizing-storage/disk-based-vector-search/)。

## 向量搜尋集合入門
<a name="serverless-vector-tutorial"></a>

在本教學課程中，您將完成下列步驟，以即時儲存、搜尋和擷取向量內嵌：

1. [設定許可](#serverless-vector-permissions)

1. [建立集合](#serverless-vector-create)

1. [上傳並搜尋資料](#serverless-vector-index)

1. [刪除集合](#serverless-vector-delete)

### 步驟 1：設定許可
<a name="serverless-vector-permissions"></a>

若要完成本教學課程 （以及一般使用 OpenSearch Serverless)，您必須擁有正確的 AWS Identity and Access Management (IAM) 許可。在本教學課程中，您會建立集合、上傳和搜尋資料，然後刪除集合。

使用者或角色必須連接[身分型政策](security-iam-serverless.md#security-iam-serverless-id-based-policies)，該政策包含以下最低許可：

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Action": [
        "aoss:CreateCollection",
        "aoss:ListCollections",
        "aoss:BatchGetCollection",
        "aoss:DeleteCollection",
        "aoss:CreateAccessPolicy",
        "aoss:ListAccessPolicies",
        "aoss:UpdateAccessPolicy",
        "aoss:CreateSecurityPolicy",
        "iam:ListUsers",
        "iam:ListRoles"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
```

------

如需有關 OpenSearch Serverless IAM 許可的詳細資訊，請參閱 [Amazon OpenSearch Serverless 的身分和存取管理](security-iam-serverless.md)。

### 步驟 2：建立集合
<a name="serverless-vector-create"></a>

*集合*是一組 OpenSearch 索引，可一起運作以支援特定工作負載或使用案例。

**建立 OpenSearch Serverless 集合**

1. 開啟位於 https：//[https://console.aws.amazon.com/aos/home](https://console.aws.amazon.com/aos/home ) 的 Amazon OpenSearch Service 主控台。

1. 在左側導覽窗格中選擇 **Collections** (集合)，然後選擇 **Create collection** (建立集合)。

1. 命名集合**外殼**。

1. 針對集合類型，選擇**向量搜尋**。如需詳細資訊，請參閱[選擇集合類型](serverless-overview.md#serverless-usecase)。

1. 在**部署類型**下，清除**啟用備援 （作用中複本）**。這會在開發或測試模式中建立集合，並將集合中的 OpenSearch 運算單位 (OCUs數目減少為兩個。如果您想要在本教學課程中建立生產環境，請保持選取核取方塊。

1. 在**安全**下，選取**輕鬆建立**以簡化您的安全組態。根據預設，向量引擎中的所有資料都會在傳輸中和靜態時加密。向量引擎支援精細的 IAM 許可，讓您可以定義誰可以建立、更新和刪除加密、網路、集合和索引。

1. 選擇**下一步**。

1. 檢閱集合設定，然後選擇 **Submit** (提交)。等待幾分鐘，讓收集狀態變成 `Active`。

### 步驟 3：上傳並搜尋資料
<a name="serverless-vector-index"></a>

*索引*是具有常見資料結構描述的文件集合，可讓您儲存、搜尋和擷取向量內嵌和其他欄位。您可以使用 OpenSearch Dashboards 中的[開發工具](https://opensearch.org/docs/latest/dashboards/dev-tools/index-dev/)主控台或 [Postman](https://www.postman.com/downloads/) 或 [awscurl](https://github.com/okigan/awscurl) 等 HTTP 工具，在 OpenSearch Serverless 集合中建立和上傳資料至索引。本教學課程使用開發工具。

**索引和搜尋房屋集合中的資料**

1. 若要為新集合建立單一索引，請在[開發工具](https://opensearch.org/docs/latest/dashboards/dev-tools/index-dev/)主控台中傳送下列請求。根據預設，這會建立具有`nmslib`引擎和歐幾里得距離的索引。

   ```
   PUT housing-index
   {
      "settings": {
         "index.knn": true
      },
      "mappings": {
         "properties": {
            "housing-vector": {
               "type": "knn_vector",
               "dimension": 3
            },
            "title": {
               "type": "text"
            },
            "price": {
               "type": "long"
            },
            "location": {
               "type": "geo_point"
            }
         }
      }
   }
   ```

1. 若要將單一文件編製索引為 *housing-index*，請傳送下列請求：

   ```
   POST housing-index/_doc
   {
     "housing-vector": [
       10,
       20,
       30
     ],
     "title": "2 bedroom in downtown Seattle",
     "price": "2800",
     "location": "47.71, 122.00"
   }
   ```

1. 若要搜尋與索引中屬性類似的屬性，請傳送下列查詢：

   ```
   GET housing-index/_search
   {
       "size": 5,
       "query": {
           "knn": {
               "housing-vector": {
                   "vector": [
                       10,
                       20,
                       30
                   ],
                   "k": 5
               }
           }
       }
   }
   ```

### 步驟 4：刪除集合
<a name="serverless-vector-delete"></a>

由於*房屋*集合是用於測試目的，因此請務必在完成實驗時將其刪除。

**刪除 OpenSearch Serverless 集合**

1. 返回 **Amazon OpenSearch Service** 主控台。

1. 在左側導覽窗格中選擇**集合**，然後選取**屬性**集合。

1. 選擇**刪除**並確認刪除。

## 篩選的搜尋
<a name="serverless-vector-filter"></a>

您可以使用篩選條件來精簡語意搜尋結果。若要建立索引並對文件執行篩選搜尋，請將上一個教學課程中的[上傳和搜尋資料](#serverless-vector-index)替換為下列指示。其他步驟保持不變。如需篩選條件的詳細資訊，請參閱[使用篩選條件進行 k-NN 搜尋](https://opensearch.org/docs/latest/search-plugins/knn/filter-search-knn/)。

**索引和搜尋房屋集合中的資料**

1. 若要為您的集合建立單一索引，請在[開發工具](https://opensearch.org/docs/latest/dashboards/dev-tools/index-dev/)主控台中傳送下列請求：

   ```
   PUT housing-index-filtered
   {
     "settings": {
       "index.knn": true
     },
     "mappings": {
       "properties": {
         "housing-vector": {
           "type": "knn_vector",
           "dimension": 3,
           "method": {
             "engine": "faiss",
             "name": "hnsw"
           }
         },
         "title": {
           "type": "text"
         },
         "price": {
           "type": "long"
         },
         "location": {
           "type": "geo_point"
         }
       }
     }
   }
   ```

1. 若要將單一文件索引為已*housing-index-filtered*，請傳送下列請求：

   ```
   POST housing-index-filtered/_doc
   {
     "housing-vector": [
       10,
       20,
       30
     ],
     "title": "2 bedroom in downtown Seattle",
     "price": "2800",
     "location": "47.71, 122.00"
   }
   ```

1. 若要以指定價格在地理點的指定距離內搜尋西雅圖的公寓，請傳送下列請求：

   ```
   GET housing-index-filtered/_search
   {
     "size": 5,
     "query": {
       "knn": {
         "housing-vector": {
           "vector": [
             0.1,
             0.2,
             0.3
           ],
           "k": 5,
           "filter": {
             "bool": {
               "must": [
                 {
                   "query_string": {
                     "query": "Find me 2 bedroom apartment in Seattle under $3000 ",
                     "fields": [
                       "title"
                     ]
                   }
                 },
                 {
                   "range": {
                     "price": {
                       "lte": 3000
                     }
                   }
                 },
                 {
                   "geo_distance": {
                     "distance": "100miles",
                     "location": {
                       "lat": 48,
                       "lon": 121
                     }
                   }
                 }
               ]
             }
           }
         }
       }
     }
   }
   ```

## 數十億個規模的工作負載
<a name="serverless-vector-billion"></a>

向量搜尋集合支援具有數十億個向量的工作負載。您不需要為擴展目的重新編製索引，因為自動擴展會為您執行此操作。如果您有數百萬個具有大量維度的向量 （或更多），且需要超過 200 OCUs，請聯絡 [AWS Support](https://aws.amazon.com/premiumsupport/) 來提高您帳戶的 OpenSearch 運算單位 (OCUs上限。

## 限制
<a name="serverless-vector-limitations"></a>

向量搜尋集合有下列限制：
+ 向量搜尋集合不支援 Apache Lucene ANN 引擎。
+ 向量搜尋集合僅支援具有 Faiss 的 HNSW 演算法，不支援 IVF 和 IVFQ。
+ 向量搜尋集合不支援暖機、統計資料和模型訓練 API 操作。
+ 向量搜尋集合不支援內嵌或預存指令碼。
+  AWS 管理主控台 向量搜尋集合的 中不提供索引計數資訊。
+ 向量搜尋集合上索引的重新整理間隔為 60 秒。

## 後續步驟
<a name="serverless-vector-next"></a>

現在您知道如何建立向量搜尋集合和索引資料，建議您嘗試下列一些練習：
+ 使用 OpenSearch Python 用戶端來使用向量搜尋集合。請參閱 [GitHub](https://github.com/opensearch-project/opensearch-py/blob/main/guides/plugins/knn.md) 上的本教學課程。
+ 使用 OpenSearch Java 用戶端來使用向量搜尋集合。請參閱 [GitHub](https://github.com/opensearch-project/opensearch-java/blob/main/guides/plugins/knn.md) 上的本教學課程。
+ 設定 LangChain 以使用 OpenSearch 作為向量存放區。LangChain 是一種開放原始碼架構，用於開發採用語言模型的應用程式。如需詳細資訊，請參閱 [LangChain 文件](https://python.langchain.com/docs/integrations/vectorstores/opensearch)。