本文介紹如何通過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
# }
# }
# ]
# }
向量檢索高級參數
詳情可參考 向量檢索高級參數。
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"vector": [0.1, 0.2, 0.3, 0.4],
"vector_param":{ "radius": 0.53, "is_linear": false, "ef": 1000 },
"topk": 10,
"include_vector": true
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart_euclidean/query
#example output:
#{
# "code": 0,
# "request_id": "59df860b-7d29-466b-a345-0bfe9e27329e",
# "message": "Success",
# "output": [
# {
# "id": "2",
# "vector": [
# 0.20000000298023224,
# 0.30000001192092896,
# 0.4000000059604645,
# 0.5
# ],
# "fields": {
# "anykey1": "str-value",
# "anykey2": 1,
# "name": "zhangshan",
# "weight": null,
# "anykey3": true,
# "anykey4": 3.1415925,
# "age": 70
# },
# "score": 0.04
# },
# {
# "id": "3",
# "vector": [
# 0.30000001192092896,
# 0.4000000059604645,
# 0.5,
# 0.6000000238418579
# ],
# "fields": {
# "name": null,
# "weight": null,
# "age": null
# },
# "score": 0.16000001
# },
# {
# "id": "4",
# "vector": [
# 0.4000000059604645,
# 0.5,
# 0.6000000238418579,
# 0.699999988079071
# ],
# "fields": {
# "name": "zhangsan",
# "weight": null,
# "age": 20
# },
# "score": 0.36
# }
# ]
#}
多向量檢索
詳情可參考 多向量檢索。
RrfRanker 示例
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"vectors": {"title": {"vector": [0.1, 0.2, 0.3, 0.4]}, "content": {"vector": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6], "param": {"num_candidates": 10}}},
"topk": 20,
"rerank": {"ranker_name": "rrf", "ranker_params": {"rank_constant":"100"} }
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/multi_vector_demo/query
# example output:
#{
# "code": 0,
# "request_id": "db20ba2b-9dc6-4c23-9266-430e6fb1a70d",
# "message": "Success",
# "output": [
# {
# "id": "1",
# "fields": {
# "author": null
# },
# "score": 0.019704912
# },
# {
# "id": "2",
# "fields": {
# "author": "zhangsan"
# },
# "score": 0.00990099
# },
# {
# "id": "3",
# "fields": {
# "author": null,
# "anykey": "anyvalue"
# },
# "score": 0.009803922
# }
# ]
#}
WeightedRanker 示例
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"vectors": {"title": {"vector": [0.1, 0.2, 0.3, 0.4]}, "content": {"vector": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6], "param": {"num_candidates": 10}}},
"topk": 20,
"rerank": {"ranker_name": "weighted", "ranker_params": {"weights": "{\"title\":0.2, \"content\":0.8}" }}
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/multi_vector_demo/query
# example output:
#{
# "code": 0,
# "request_id": "c7413fa9-92fd-4493-8f21-6c65c83e7b91",
# "message": "Success",
# "output": [
# {
# "id": "1",
# "fields": {
# "author": null
# },
# "score": 0.8156271
# },
# {
# "id": "3",
# "fields": {
# "author": null,
# "anykey": "anyvalue"
# },
# "score": 0.5880098
# },
# {
# "id": "2",
# "fields": {
# "author": "zhangsan"
# },
# "score": 0.2
# }
# ]
#}
使用多向量的一個向量執行檢索
curl -XPOST \
-H 'dashvector-auth-token: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"vectors": {"title": {"vector": [0.1, 0.2, 0.3, 0.4], "param":{ "radius": 0.1, "is_linear": true, "ef": 1000 }}},
"topk": 20,
"include_vector": true
}' https://YOUR_CLUSTER_ENDPOINT/v1/collections/multi_vector_demo/query
# example output:
#{
# "code": 0,
# "request_id": "b9bc3d5a-8edf-4d5b-916d-0ced6ae570cb",
# "message": "Success",
# "output": [
# {
# "id": "4",
# "vectors": {
# "title": [
# 0.10000000149011612,
# 0.20000000298023224,
# 0.30000001192092896,
# 0.4000000059604645
# ]
# },
# "fields": {
# "author": "zhangsan"
# },
# "score": 0.0
# },
# {
# "id": "2",
# "vectors": {
# "title": [
# 0.10000000149011612,
# 0.20000000298023224,
# 0.30000001192092896,
# 0.4000000059604645
# ]
# },
# "fields": {
# "author": "zhangsan"
# },
# "score": 0.0
# }
# ]
#}
入參描述
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名稱 |
vectors | body | dict | 否 | 多個向量檢索,類型為 |
rerank | body | dict | 否 | 融合排序參數,詳情參考多向量檢索 |
vector_param | body | dict | 否 | 高級檢索參數,詳情參考 向量檢索高級參數 |
出參描述
字段 | 類型 | 描述 | 示例 |
code | int | 返回值,參考返回狀態碼說明 | 0 |
message | str | 返回消息 | success |
request_id | str | 請求唯一id | 19215409-ea66-4db9-8764-26ce2eb5bb99 |
output | array | 相似性檢索結果,Doc列表 | |
usage | map | 對Serverless實例(按量付費)集合的Doc檢索請求,成功后返回實際消耗的讀請求單元數 |
|