search api的数据结构
此api的功能是向量相似度搜索(vector similarity search)
一个完整的search例子:
服务端collection是一个hnsw类型的索引。
import random
from pymilvus import (connections,Collection,
)dim = 128if __name__ == '__main__':connections.connect(alias="default",user='',password='',host='192.168.230.71',port='19530')hello_milvus = Collection("hello_milvus")# hnsw索引的搜索参数search_param = {"metric_type": "L2","params": {"ef": 200}}search_data = [random.random() for _ in range(dim)]results = hello_milvus.search(data=[search_data],anns_field="embeddings",param=search_param,limit=5,expr=None,output_fields=['pk','book_id'],consistency_level="Eventually")print(results)
输出:
["id: 2196, distance: 12.860455513000488, entity: {'pk': 2196, 'book_id': 2196}","id: 888, distance: 15.004779815673828, entity: {'pk': 888, 'book_id': 888}","id: 2454, distance: 15.082155227661133, entity: {'pk': 2454, 'book_id': 2454}","id: 884, distance: 15.443259239196777, entity: {'pk': 884, 'book_id': 884}","id: 2058, distance: 15.52014446258545, entity: {'pk': 2058, 'book_id': 2058}"
]
SearchRequest数据结构
type SearchRequest struct {Base *commonpb.MsgBaseDbName stringCollectionName stringPartitionNames []stringDsl string// serialized `PlaceholderGroup`PlaceholderGroup []byteDslType commonpb.DslTypeOutputFields []stringSearchParams []*commonpb.KeyValuePairTravelTimestamp uint64GuaranteeTimestamp uint64Nq int64NotReturnAllMeta boolConsistencyLevel commonpb.ConsistencyLevelUseDefaultConsistency boolSearchByPrimaryKeys boolXXX_NoUnkeyedLiteral struct{}XXX_unrecognized []byteXXX_sizecache int32
}
Dsl ??
PlaceholderGroup ??
DslType ??
nq:number of query
ConsistencyLevel:一致性级别
const (ConsistencyLevel_Strong ConsistencyLevel = 0ConsistencyLevel_Session ConsistencyLevel = 1ConsistencyLevel_Bounded ConsistencyLevel = 2ConsistencyLevel_Eventually ConsistencyLevel = 3ConsistencyLevel_Customized ConsistencyLevel = 4
)
使用Strong一致性级别搜索速度会很慢。
从数据结构可以看出,search的时候可以指定partition。