id: hybridsearch.md related_key: filter
summary: Conduct a Hybrid Search with Milvus.
混合搜索
当前主题介绍如何进行混合搜索。
混合搜索本质上是带有属性过滤的向量搜索。通过指定过滤标量 field 或者主键 field 的 布尔表达式,你可以使用特定条件限制搜索。
下面的例子展示了如何在 向量搜索 的基础上进行混合搜索。假设你想基于书籍介绍的特征向量搜索某些书籍,然而你只需要特定 word count 范围内的书籍。你可以指定布尔表达式来过滤过滤搜索参数中的 word_count
field。Milvus 只会在匹配表达式的 entity 中搜索相似的向量。
加载 collection
Milvus 中的所有搜索和结构化匹配操作都在内存中执行。在进行向量相似性搜索之前将 collection 加载到内存中。
{{fragments/multiple_code.md}}
from pymilvus import Collection
collection = Collection("book") # Get an existing collection.
collection.load()
await milvusClient.collectionManager.loadCollection({
collection_name: "book",
});
err := milvusClient.LoadCollection(
context.Background(), // ctx
"book", // CollectionName
false // async
)
if err != nil {
log.Fatal("failed to load collection:", err.Error())
}
milvusClient.loadCollection(
LoadCollectionParam.newBuilder()
.withCollectionName("book")
.build());
load -c book
混合向量搜索
通过指定布尔表达式,你可以在向量搜索过程中过滤 entity 的标量 field。以下示例将搜索范围限制为指定 word_count
值范围内的向量。
{{fragments/multiple_code.md}}
search_param = {
"data": [[0.1, 0.2]],
"anns_field": "book_intro",
"param": {"metric_type": "L2", "params": {"nprobe": 10}},
"limit": 2,
"expr": "word_count <= 11000",
}
res = collection.search(**search_param)
const results = await milvusClient.dataManager.search({
collection_name: "book",
expr: "word_count <= 11000",
vectors: [[0.1, 0.2]],
search_params: {
anns_field: "book_intro",
topk: "2",
metric_type: "L2",
params: JSON.stringify({ nprobe: 10 }),
},
vector_type: 101, // DataType.FloatVector,
});
sp, _ := entity.NewIndexFlatSearchParam( // NewIndex*SearchParam func
10, // searchParam
)
searchResult, err := milvusClient.Search(
context.Background(), // ctx
"book", // CollectionName
[]string{}, // partitionNames
"word_count <= 11000", // expr
[]string{"book_id"}, // outputFields
[]entity.Vector{entity.FloatVector([]float32{0.1, 0.2})}, // vectors
"book_intro", // vectorField
entity.L2, // metricType
2, // topK
sp, // sp
)
if err != nil {
log.Fatal("fail to search collection:", err.Error())
}
final Integer SEARCH_K = 2;
final String SEARCH_PARAM = "{\"nprobe\":10}";
List<String> search_output_fields = Arrays.asList("book_id");
List<List<Float>> search_vectors = Arrays.asList(Arrays.asList(0.1f, 0.2f));
SearchParam searchParam = SearchParam.newBuilder()
.withCollectionName("book")
.withMetricType(MetricType.L2)
.withOutFields(search_output_fields)
.withTopK(SEARCH_K)
.withVectors(search_vectors)
.withVectorFieldName("book_intro")
.withExpr("word_count <= 11000")
.withParams(SEARCH_PARAM)
.build();
R<SearchResults> respSearch = milvusClient.search(searchParam);
search
Collection name (book): book
The vectors of search data(the length of data is number of query (nq), the dim of every vector in data must be equal to vector field’s of collection. You can also import a csv file without headers): [[0.1, 0.2]]
The vector field used to search of collection (book_intro): book_intro
Metric type: L2
Search parameter nprobe's value: 10
The max number of returned record, also known as topk: 2
The boolean expression used to filter attribute []: word_count <= 11000
The names of partitions to search (split by "," if multiple) ['_default'] []:
timeout []:
Guarantee Timestamp(It instructs Milvus to see all operations performed before a provided timestamp. If no such timestamp is provided, then Milvus will search all operations performed to date) [0]:
Travel Timestamp(Specify a timestamp in a search to get results based on a data view) [0]:
参数 | 说明 |
---|---|
data |
用于搜索的向量。 |
anns_field |
要搜索的 field 名称。 |
params |
该索引特有的搜索参数。详细信息请参考 向量索引 。 |
limit |
输出向量结果数。 |
expr |
用于过滤属性的布尔表达式。有关详细信息,请参考 布尔表达式规则。 |
partition_names (optional) |
要搜索的 partition 名称列表。 |
output_fields (optional) |
返回的 field 名称。当前版本不支持 Vector filed 。 |
timeout (optional) |
允许 RPC 的持续时间(以秒为单位)。当设置为 None 时,客户端等待服务器响应或者发生错误。 |
round_decimal (optional) |
返回距离的小数位数。 |
参数 | 说明 |
---|---|
collection_name |
要搜索的 collection 名称。 |
search_params |
用于搜索的参数(对象)。 |
vectors |
用于搜索的向量。 |
vector_type |
二进制型或浮点型向量的预检查。二进制型向量为100 ,浮点型向量为101 。 |
partition_names (optional) |
要搜索的 partition 名称列表。 |
expr (optional) |
用于过滤属性的布尔表达式。有关详细信息,请参考 布尔表达式规则。 |
output_fields (optional) |
返回的 field 名称。当前版本不支持 Vector field 。 |
参数 | 说明 | 选项 |
---|---|---|
NewIndex*SearchParam func |
根据不同的索引类型创建 entity.SearchParam 的函数。 | 浮点型向量:
|
searchParam |
该索引特有的搜索参数。 | 详细信息请参考 向量索引 。 |
ctx |
控制调用 API 的 Context。 | N/A |
CollectionName |
要加载的 collection 名称。 | N/A |
partitionNames |
要加载的 partition 名称列表。如果将其置空,将搜索所有的 partition 。 | N/A |
expr |
用于过滤属性的布尔表达式。 | 有关详细信息,请参考 布尔表达式规则。 |
output_fields |
要返回的 field 名称。 | 当前版本不支持 Vector field 。 |
vectors |
用于搜索的向量。 | N/A |
vectorField |
要搜索的 filed 名称。 | N/A |
metricType |
用于搜索的指标类型。 | 此参数必须设置为与用于索引构建的指标类型相同。 |
topK |
输出向量结果数。 | N/A |
sp |
该索引特有的搜索参数 entity.SearchParam 。 | N/A |
参数 | 说明 | 选项 |
---|---|---|
CollectionName |
要加载的 collection 名称。 | N/A |
MetricType |
距离计算方式。 | 此参数必须设置为与用于索引构建的指标类型相同。 |
OutFields |
要返回的 field 名称。 | 当前版本不支持 Vector field 。 |
TopK |
输出向量结果数。 | N/A |
Vectors |
用于搜索的向量。 | N/A |
VectorFieldName |
要搜索的 field 名称。 | N/A |
Expr |
用于过滤属性的布尔表达式。 | 有关详细信息,请参考 布尔表达式规则。 |
Params |
该索引特有的搜索参数。 | 详细信息请参考 向量索引 。 |
选项 | 全称 | 说明 |
---|---|---|
—help | n/a | 显示使用命令的帮助。 |
检查返回的结果。
{{fragments/multiple_code.md}}
assert len(res) == 1
hits = res[0]
assert len(hits) == 2
print(f"- Total hits: {len(hits)}, hits ids: {hits.ids} ")
print(f"- Top1 hit id: {hits[0].id}, distance: {hits[0].distance}, score: {hits[0].score} ")
console.log(results.results)
fmt.Printf("%#v\n", searchResult)
for _, sr := range searchResult {
fmt.Println(sr.IDs)
fmt.Println(sr.Scores)
}
SearchResultsWrapper wrapperSearch = new SearchResultsWrapper(respSearch.getData().getResults());
System.out.println(wrapperSearch.getIDScore(0));
System.out.println(wrapperSearch.getFieldData("book_id", 0));
# Milvus CLI automatically returns the primary key values of the most similar vectors and their distances.
更多内容
- 了解更多关于 Milvus 的基本操作:
- 探索 Milvus SDKs 的 API 参考: