本文介绍如何通过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列表 |