本文介紹如何通過HTTP API在Collection中進行相似性檢索。
前提條件
Method與URL
POST https://{Endpoint}/v1/collections/{CollectionName}/query使用樣本
說明
需要使用您的api-key替換樣本中的YOUR_API_KEY、您的Cluster Endpoint替換樣本中的YOUR_CLUSTER_ENDPOINT,代碼才能正常運行。
本樣本需要參考建立Collection-使用樣本提前建立好名稱為
quickstart的Collection
根據向量進行相似性檢索
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"vector": [0.1, 0.2, 0.3, 0.4],
"topk": 10,
"include_vector": true
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/query
# example output:
# {
# "code": 0,
# "request_id": "2cd1cac7-f1ee-4d15-82a8-b65e75d8fd13",
# "message": "Success",
# "output": [
# {
# "id": "1",
# "vector":[
# 0.10000000149011612,
# 0.20000000298023224,
# 0.30000001192092896,
# 0.4000000059604645
# ],
# "fields": {
# "name": "zhangshan",
# "weight": null,
# "age": 20,
# "anykey": "anyvalue"
# },
# "score": 0.3
# }
# ]
# }根據主鍵(對應的向量)進行相似性檢索
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"id": "1",
"topk": 1,
"include_vector": true
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/query
# example output:
# {
# "code":0,
# "request_id":"fab4e8a2-15e4-4b55-816f-3b66b7a44962",
# "message":"Success",
# "output":[
# {
# "id":"1",
# "vector":[
# 0.10000000149011612,
# 0.20000000298023224,
# 0.30000001192092896,
# 0.4000000059604645
# ],
# "fields": {
# "name": "zhangshan",
# "weight": null,
# "age": 20,
# "anykey": "anyvalue"
# },
# "score": 0.3
# }
# ]
# }帶過濾條件的相似性檢索
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"filter": "age > 18",
"topk": 1,
"include_vector": true
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/query
# example output:
# {
# "code":0,
# "request_id":"4c7331d8-fba1-4c3a-8673-124568670de7",
# "message":"Success",
# "output":[
# {
# "id":"1",
# "vector":[
# 0.10000000149011612,
# 0.20000000298023224,
# 0.30000001192092896,
# 0.4000000059604645
# ],
# "fields": {
# "name": "zhangshan",
# "weight": null,
# "age": 20,
# "anykey": "anyvalue"
# },
# "score": 0.0
# }
# ]
# }帶有Sparse Vector的向量檢索
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"vector": [0.1, 0.2, 0.3, 0.4],
"sparse_vector":{"1":0.4, "10000":0.6, "222222":0.8},
"topk": 1,
"include_vector": true
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/query
# example output:
# {
# "code":0,
# "request_id":"ad84f7a0-b4b2-4023-ae80-b6f092609a53",
# "message":"Success",
# "output":[
# {
# "id":"2",
# "vector":[
# 0.10000000149011612,
# 0.20000000298023224,
# 0.30000001192092896,
# 0.4000000059604645
# ],
# "fields":{"name":null,"weight":null,"age":null},
# "score":1.46,
# "sparse_vector":{
# "10000":0.6,
# "1":0.4,
# "222222":0.8
# }
# }
# ]
# }入參描述
說明
vector和id兩個入參需要二選一使用,如都不傳入,則僅完成條件過濾。
參數 | Location | 類型 | 必填 | 說明 |
{Endpoint} | path | str | 是 | Cluster的Endpoint,可在控制台Cluster詳情中查看 |
{CollectionName} | path | str | 是 | Collection名稱 |
dashvector-auth-token | header | str | 是 | api-key |
vector | body | array | 否 | 向量資料 |
sparse_vector | body | dict | 否 | 稀疏向量 |
id | body | str | 否 | 主鍵,表示根據主鍵對應的向量進行相似性檢索 |
topk | body | int | 否 | 返回topk相似性結果,預設10 |
filter | body | str | 否 | 過濾條件,需滿足SQL where子句規範,詳見條件過濾檢索 |
include_vector | body | bool | 否 | 是否返迴向量資料,預設false |
output_fields | body | array | 否 | 返回field的欄位名列表,預設返回所有Fields |
partition | body | str | 否 | Partition名稱 |
出參描述
欄位 | 類型 | 描述 | 樣本 |
code | int | 傳回值,參考返回狀態代碼說明 | 0 |
message | str | 返回訊息 | success |
request_id | str | 請求唯一id | 19215409-ea66-4db9-8764-26ce2eb5bb99 |
output | array | 相似性檢索結果,Doc列表 |