我是 Milvus 和矢量数据库的新手。我正在尝试创建一个 3 维集合来存储颜色并执行范围搜索以识别与特定颜色相似的条目。但是,范围搜索将返回集合中直到
limit
的所有条目,而不仅仅是指定半径内的条目。
Milvus 版本:2.4.5(最近从 Dockerhub 拉取)
同时使用 REST API 和 Python 客户端
curl -s --request POST "http://localhost:19530/v2/vectordb/collections/create" \
--data-raw '{
"collectionName": "colors",
"dimension": 3,
"metricType": "L2",
"autoId": true
}'
2.添加项目(对所有 6 点重复):
curl --request POST "http://localhost:19530/v2/vectordb/entities/insert" --data-raw '{
"data": [
{"vector": [0.0, 255.0, 0.0]}
],
"collectionName": "colors"
}'
合集包含:
[0.0, 255.0, 0.0]
[0.0, 255.0, 55.0]
[100.0, 255.0, 0.0]
[0.0, 200.0, 200.0]
[255.0, 100.0, 0.0]
[255.0, 0.0, 0.0]
curl -s --request POST "http://localhost:19530/v2/vectordb/collections/load" --data-raw '{ "collectionName": "colors" }'
curl -s --request POST "http://localhost:19530/v2/vectordb/entities/search" --data-raw '{
"collectionName": "colors",
"data": [
[0, 255, 0]
],
"outputFields": ["id", "vector"],
"searchParams": {
"metricType": "L2",
"params": {
"radius": 14400
}
}
}' | jq
我希望仅获得搜索半径 120 (14400 = 120^2) 内的点 [0, 255, 0]、[100, 255, 0] 和 [0, 255, 55]点 [0, 255, 0].
搜索返回集合中的所有 6 个点。
from pymilvus import connections, Collection
connections.connect("default", host="192.168.1.17", port=19530)
colors_collection = Collection("colors")
colors_collection.load()
results = colors_collection.search(
data=[[0, 255, 0]],
anns_field="vector",
param={"radius": 14400},
limit=1000,
output_fields=["vector"]
)
colors_collection.release()
connections.disconnect("default")
for item in results[0]:
print(item)
我已通过
describe
API 路径验证该集合已设置为使用 L2 指标。
我尝试过在搜索请求中指定或不指定 L2 指标类型。
根据这个视频,Milvus 省略了距离公式的平方根部分以提高性能,这就是为什么我使用 14400 (120^2) 作为半径。
为什么范围搜索返回所有点而不是仅返回指定半径内的点?我是否误解了半径参数的工作原理,或者我的设置有问题?
任何帮助或见解将不胜感激。谢谢!
您遇到的问题是由于 Milvus RESTful 搜索 API 中的一个小错误造成的。它无法正确处理请求中的
"searchParams"
字段。以下是如何正确设置用于范围搜索的curl命令的格式:
curl --location --request POST "http://localhost:19530/v2/vectordb/entities/search" \
--header "Content-Type: application/json" \
--data-raw '{
"collectionName": "colors",
"data": [
[0, 255, 0]
],
"annsField": "vector",
"params": {
"radius": 14400
},
"limit": 1000,
"outputFields": [
"vector"
]
}'
主要变化:
"searchParams"
并将其内容直接移至"params"
下方。"annsField": "vector"
参数。"params"
与 JSON 结构中的其他顶级键处于同一级别。补充说明:
"metricType": "L2"
。radius
值 14400 是正确的(120^2),因为 Milvus 出于性能原因在距离计算中省略了平方根。对于 Python 用户,正确的格式是:
results = colors_collection.search(
data=[[0, 255, 0]],
anns_field="vector",
param={"radius": 14400},
limit=1000,
output_fields=["vector"]
)
这应该正确返回指定半径内的点。