本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用向量搜尋集合
透過 OpenSearch Serverless 中的向量搜尋集合類型,您可以執行可擴展、高效能的相似性搜尋。您可以建置現代機器學習 (ML) 擴增的搜尋體驗和生成式人工智慧 (AI) 應用程式,而無需管理基礎向量資料庫基礎設施。
向量搜尋集合的使用案例包括影像搜尋、文件搜尋、音樂擷取、產品建議、影片搜尋、以位置為基礎的搜尋、詐騙偵測和異常偵測。
OpenSearch Serverless 的向量引擎使用 OpenSearch 中的 k 近鄰 (k-NN) 搜尋功能
向量引擎提供距離指標,例如歐幾里德距離、餘弦相似性和點產品相似性,可容納 16,000 個維度。您可以存放具有各種中繼資料資料類型的欄位,例如數字、布林值、日期、關鍵字和地理位置。您也可以使用文字存放欄位以取得描述性資訊,以將更多內容新增至存放的向量。協調資料類型可降低複雜性、提高可維護性,並避免資料重複、版本相容性挑戰和授權問題。
NextGen 向量搜尋集合
NextGen 向量搜尋會根據工作負載隨需擴展,以最佳化成本和效能之間的平衡。只有提供作用中搜尋請求所需的資料區塊才會載入記憶體,而工作者會根據所需的記憶體和 CPU 資源動態擴展。當集合閒置而沒有持續的請求時,索引和搜尋都會擴展到零,從而節省額外的成本。根據預設,NextGen 包含內建最佳化,可改善召回,同時降低成本和延遲。
-
自訂文件 ID – NextGen 集合支援自訂文件 IDs,讓客戶能夠更輕鬆地使用使用者提供的 IDs執行更新或索引文件。
-
32 倍壓縮索引 – 預設會使用進階的 32 倍壓縮技術建立所有索引。您可以覆寫預設壓縮層級,然後選取任何支援的壓縮層級:1x、2x、8x、16x 或 32x (預設)。
-
索引建置加速 – GPU 加速預設為啟用,以協助更快速、更有效率地建置大規模向量索引。它可縮短將資料編製索引至向量索引所需的時間,提供高輸送量索引體驗並節省成本。只有在索引建置操作期間需要時,才會佈建 GPU 資源。您可以使用設定 來控制每個索引的 GPU 用量
index.knn.remote_index_build.enabled。如需詳細資訊,請參閱向量索引的 GPU 加速。 -
簡化 API – NextGen 向量搜尋集合不需要索引映射中的
engine和mode參數。系統會自動在內部決定最佳組態,降低索引建立的複雜性。 -
最佳化搜尋回應 – 預設情況下,NextGen 向量集合中的搜尋回應會從結果中排除原始向量。這可減少端對端搜尋延遲和回應承載大小。若要在搜尋回應中包含向量,請參閱 使用向量擷取完整文件。
-
NextGen 向量集合的read-after-write延遲 (
refresh_interval) 為 10 秒。
向量搜尋集合入門
在本教學課程中,您將完成下列步驟,以即時儲存、搜尋和擷取向量內嵌:
步驟 1:設定許可
若要完成本教學課程 (以及一般使用 OpenSearch Serverless),您必須擁有正確的 AWS Identity and Access Management (IAM) 許可。在本教學課程中,您會建立集合、上傳和搜尋資料,然後刪除集合。
使用者或角色必須連接身分型政策,該政策包含以下最低許可:
如需有關 OpenSearch Serverless IAM 許可的詳細資訊,請參閱 Amazon OpenSearch Serverless 的身分和存取管理。
步驟 2:建立集合
若要建立新的集合,請遵循統一集合建立流程 (NextGen Express Create),其會自動設定加密、網路和資料存取政策。如需說明,請參閱建立 NextGen 集合 (快速建立)。
對於本教學課程的其餘部分,範例集合命名為 housing,並且是 NextGen 向量搜尋集合。
注意
如果您選擇改為建立 Classic 向量集合,請參閱 使用 Classic 向量集合 以取得 Classic 集合的特定程序。
步驟 3:上傳並搜尋資料
索引是具有常見資料結構描述的文件集合,可讓您儲存、搜尋和擷取向量內嵌和其他欄位。您可以使用 OpenSearch Dashboards 中的開發工具
索引和搜尋房屋集合中的資料
-
若要為新集合建立索引,請在開發工具
主控台中傳送下列請求。根據預設,這會建立具有歐幾里得距離和 32 倍壓縮的索引。 PUT housing-index { "settings": { "index.knn": true }, "mappings": { "properties": { "housing-vector": { "type": "knn_vector", "dimension": 3, "space_type": "l2" }, "title": { "type": "text" }, "price": { "type": "long" }, "location": { "type": "geo_point" } } } } -
若要使用不同的壓縮層級,請在欄位映射
compression_level中設定 。下列範例會建立 索引,並將compression_level設為 1 倍。PUT housing-index-1x { "settings": { "index.knn": true }, "mappings": { "properties": { "housing-vector": { "type": "knn_vector", "dimension": 3, "compression_level": "1x", "space_type": "l2" }, "title": { "type": "text" }, "price": { "type": "long" }, "location": { "type": "geo_point" } } } }支援的壓縮層級為 1 倍、2 倍、8 倍、16 倍和 32 倍。
-
若要將文件索引為
housing-index,您可以使用系統產生的 ID (POST) 或使用者提供的 ID (PUT)。# System-generated document ID POST housing-index/_doc { "housing-vector": [10, 20, 30], "title": "2 bedroom in downtown Seattle", "price": "2800", "location": "47.71, 122.00" } # User-provided document ID PUT housing-index/_doc/100 { "housing-vector": [10, 20, 30], "title": "2 bedroom in downtown Seattle", "price": "2800", "location": "47.71, 122.00" } -
若要搜尋與索引中屬性類似的屬性,請傳送下列查詢。根據預設,搜尋回應會從 排除原始向量
_source,以減少延遲和承載大小。GET housing-index/_search { "size": 5, "query": { "knn": { "housing-vector": { "vector": [10, 20, 30], "k": 2 } } } }回應會從 排除
housing-vector欄位_source:{ "took": 10, "timed_out": false, "_shards": { "total": 0, "successful": 0, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "housing-index", "_id": "100", "_score": 1, "_source": { "price": "2800", "location": "47.71, 122.00", "title": "2 bedroom in downtown Seattle" } } ] } }
使用向量擷取完整文件
若要覆寫預設行為,請在搜尋請求true中_source將 設定為 。您也可以使用 的 includes/excludes 選項_source來擷取特定欄位。
GET housing-index/_search { "size": 5, "_source": true, "query": { "knn": { "housing-vector": { "vector": [10, 20, 30], "k": 2 } } } }
回應現在包含 中的 housing-vector 欄位_source:
{ "took": 10, "timed_out": false, "_shards": { "total": 0, "successful": 0, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "housing-index", "_id": "100", "_score": 1, "_source": { "housing-vector": [10, 20, 30], "price": "2800", "location": "47.71, 122.00", "title": "2 bedroom in downtown Seattle" } } ] } }
步驟 4:刪除集合
由於外殼集合用於測試目的,因此請在完成實驗時將其刪除。
刪除 OpenSearch Serverless 集合
-
開啟 Amazon OpenSearch Service 主控台。
-
在左側導覽窗格中,選擇集合,然後選取外殼集合。
-
選擇刪除並確認刪除。
篩選的搜尋
您可以使用篩選條件來精簡語意搜尋結果。若要建立索引並對文件執行篩選搜尋,請將上一個教學課程中的上傳和搜尋資料替換為下列指示。其他步驟保持不變。如需篩選條件的詳細資訊,請參閱使用篩選條件進行 k-NN 搜尋
索引和搜尋房屋集合中的資料
-
若要為您的集合建立單一索引,請在開發工具
主控台中傳送下列請求: PUT housing-index-filtered { "settings": { "index.knn": true }, "mappings": { "properties": { "housing-vector": { "type": "knn_vector", "dimension": 3, "space_type": "l2", "method": { "name": "hnsw" } }, "title": { "type": "text" }, "price": { "type": "long" }, "location": { "type": "geo_point" } } } } -
若要將單一文件索引為已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" } -
若要以指定價格在地理點的指定距離內搜尋西雅圖的公寓,請傳送下列請求:
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 } } } ] } } } } } }
限制
-
使用 32 倍壓縮的 NextGen 向量索引不支援放射搜尋。
使用 Classic 向量集合
傳統向量集合是 OpenSearch Serverless 向量搜尋的原始世代。如果您有現有的 Classic 向量集合,請使用本節中的程序。對於新集合,我們建議 NextGen — 請參閱 建立集合 以建立 NextGen 向量集合。
注意
-
Amazon OpenSearch Serverless Classic 集合支援 Faiss 16 位元純量量化,可在 32 位元浮點數和 16 位元向量之間執行轉換。若要進一步了解,請參閱 Faiss 16 位元純量量化
。您也可以使用二進位向量來降低記憶體成本。如需詳細資訊,請參閱二進位向量 。 -
Amazon OpenSearch Serverless Classic 集合支援磁碟型向量搜尋,可大幅降低低記憶體環境中向量工作負載的操作成本。如需詳細資訊,請參閱磁碟型向量搜尋
。
下列範例適用於 Classic 向量集合,這些集合預設使用 nmslib引擎,並在搜尋回應中包含原始向量。
在 Classic 房屋集合中編製索引和搜尋資料
-
若要為您的 Classic 集合建立索引,請在開發工具
主控台中傳送下列請求。根據預設,這會建立具有 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" } } } } -
若要將單一文件編製索引為 housing-index,請傳送下列請求:
POST housing-index/_doc { "housing-vector": [ 10, 20, 30 ], "title": "2 bedroom in downtown Seattle", "price": "2800", "location": "47.71, 122.00" } -
若要搜尋類似索引中的屬性,請傳送下列查詢:
GET housing-index/_search { "size": 5, "query": { "knn": { "housing-vector": { "vector": [ 10, 20, 30 ], "k": 5 } } } }
若要建立索引並執行篩選後的搜尋,請使用下列範例。如需篩選條件的詳細資訊,請參閱使用篩選條件進行 k-NN 搜尋
在 Classic 房屋集合上編製索引並執行篩選搜尋
-
若要為您的集合建立單一索引,請在開發工具
主控台中傳送下列請求: 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" } } } } -
若要將單一文件索引為已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" } -
若要以指定價格在地理點的指定距離內搜尋西雅圖的公寓,請傳送下列請求:
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 } } } ] } } } } } }
傳統向量集合有下列限制:
-
向量搜尋集合不支援 Apache Lucene ANN 引擎。
-
向量搜尋集合僅支援具有 Faiss 的 HNSW 演算法。它們不支援 IVF 或 IVFQ。
-
向量搜尋集合不支援暖機、統計資料和模型訓練 API 操作。
-
向量搜尋集合不支援內嵌或預存指令碼。
-
AWS 管理主控台 向量搜尋集合的 中不提供索引計數資訊。
-
向量搜尋集合上索引的重新整理間隔為 60 秒。
數十億個規模的工作負載
傳統向量搜尋集合支援數十億個向量的工作負載。您不需要為擴展目的重新編製索引,因為自動擴展會為您執行此操作。如果您有數百萬個具有大量維度的向量 (或更多),且需要超過 200 OCUs,請聯絡 AWS Support
後續步驟
現在您已了解如何建立向量搜尋集合和索引資料,您可以嘗試下列練習:
-
使用 OpenSearch Python 用戶端來使用向量搜尋集合。請參閱 GitHub
上的本教學課程。 -
使用 OpenSearch Java 用戶端來使用向量搜尋集合。請參閱 GitHub
上的本教學課程。 -
設定 LangChain 以使用 OpenSearch 作為向量存放區。LangChain 是一種開放原始碼架構,用於開發採用語言模型的應用程式。如需詳細資訊,請參閱 LangChain 文件
。