本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
$vectorSearch
8.0 版的新功能
Elastic 叢集不支援。
Amazon DocumentDB 中的$vectorSearch運算子可讓您執行向量搜尋,這是一種用於機器學習的方法,透過使用距離或類似指標比較其向量表示法來尋找類似的資料點。此功能結合了 JSON 型文件資料庫的彈性和豐富的查詢,以及向量搜尋的強大功能,可讓您建置機器學習和生成式 AI 使用案例,例如語意搜尋、產品建議等。
參數
-
<exact>(選用):指定要執行確切最近鄰 (ENN) 還是最近鄰 (ANN) 搜尋的旗標。值可以是下列其中一項: -
false - 執行 ANN 搜尋
-
true - 執行 ENN 搜尋
如果省略或設定為 false,numCandidates則為必要項目。
- `<index>` : Name of the Vector Search index to use. - `<limit>` : Number of documents to return in the results. - `<numCandidates>` (optional): This field is required if 'exact' is false or omitted. Number of nearest neighbors to use during the search. Value must be less than or equal to (<=) 10000. You can't specify a number less than the number of documents to return ('limit'). - `<path>` : Indexed vector type field to search. - `<queryVector>` : Array of numbers that represent the query vector.
範例 (MongoDB Shell)
下列範例示範如何使用 $vectorSearch運算子,根據其向量表示法尋找類似的產品描述。
建立範例文件
db.products.insertMany([ { _id: 1, name: "Product A", description: "A high-quality, eco-friendly product for your home.", description_vector: [ 0.2, 0.5, 0.8 ] }, { _id: 2, name: "Product B", description: "An innovative and modern kitchen appliance.", description_vector: [0.7, 0.3, 0.9] }, { _id: 3, name: "Product C", description: "A comfortable and stylish piece of furniture.", description_vector: [0.1, 0.2, 0.4] } ]);
建立向量搜尋索引
db.runCommand( { createIndexes: "products", indexes: [{ key: { "description_vector": "vector" }, vectorOptions: { type: "hnsw", dimensions: 3, similarity: "cosine", m: 16, efConstruction: 64 }, name: "description_index" }] } );
查詢範例
db.products.aggregate([ { $vectorSearch: { index: "description_index", limit: 2, numCandidates: 10, path: "description_vector", queryVector: [0.1, 0.2, 0.3] } } ]);
輸出
[
{
"_id": 1,
"name": "Product A",
"description": "A high-quality, eco-friendly product for your home.",
"description_vector": [ 0.2, 0.5, 0.8 ]
},
{
"_id": 3,
"name": "Product C",
"description": "A comfortable and stylish piece of furniture.",
"description_vector": [ 0.1, 0.2, 0.4 ]
}
]
程式碼範例
若要檢視使用 $vectorSearch命令的程式碼範例,請選擇您要使用的語言標籤: