Skip to content

Commit

Permalink
Support hybrid search with expression template
Browse files Browse the repository at this point in the history
Signed-off-by: Cai Zhang <[email protected]>
  • Loading branch information
xiaocai2333 committed Dec 20, 2024
1 parent 0d53a32 commit cc45027
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 158 deletions.
22 changes: 21 additions & 1 deletion examples/hybrid_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,27 @@
req = AnnSearchRequest(**search_param)
req_list.append(req)

print("rank by RRFRanker")
print(fmt.format("rank by RRFRanker"))
hybrid_res = milvus_client.hybrid_search(collection_name, req_list, RRFRanker(), default_limit, output_fields=["random"])
for hits in hybrid_res:
for hit in hits:
print(f" hybrid search hit: {hit}")

req_list = []
for i in range(len(field_names)):
# 4. generate search data
vectors_to_search = rng.random((nq, dim))
search_param = {
"data": vectors_to_search,
"anns_field": field_names[i],
"param": {"metric_type": "L2"},
"limit": default_limit,
"expr": "random > {radius}",
"expr_params": {"radius": 0.5}}
req = AnnSearchRequest(**search_param)
req_list.append(req)

print(fmt.format("rank by RRFRanker with expression template"))
hybrid_res = milvus_client.hybrid_search(collection_name, req_list, RRFRanker(), default_limit, output_fields=["random"])
for hits in hybrid_res:
for hit in hits:
Expand Down
22 changes: 22 additions & 0 deletions examples/hybrid_search/hybrid_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,25 @@
for hits in hybrid_res:
for hit in hits:
print(f" hybrid search hit: {hit}")

print("rank by WightedRanker with expression template")
req_list = []
for i in range(len(field_names)):
# 4. generate search data
vectors_to_search = rng.random((nq, dim))
search_param = {
"data": vectors_to_search,
"anns_field": field_names[i],
"param": {"metric_type": "L2"},
"limit": default_limit,
"expr": "random > {radius}",
"expr_params": {"radius": 0.5}}
req = AnnSearchRequest(**search_param)
req_list.append(req)

hybrid_res = hello_milvus.hybrid_search(req_list, WeightedRanker(*weights), default_limit, output_fields=["random"])

print("rank by WightedRanker")
for hits in hybrid_res:
for hit in hits:
print(f" hybrid search hit: {hit}")
6 changes: 6 additions & 0 deletions pymilvus/client/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ def __init__(
param: Dict,
limit: int,
expr: Optional[str] = None,
expr_params: Optional[dict] = None,
):
self._data = data
self._anns_field = anns_field
Expand All @@ -426,6 +427,7 @@ def __init__(
if expr is not None and not isinstance(expr, str):
raise DataTypeNotMatchException(message=ExceptionsMessage.ExprType % type(expr))
self._expr = expr
self._expr_params = expr_params

@property
def data(self):
Expand All @@ -447,6 +449,10 @@ def limit(self):
def expr(self):
return self._expr

@property
def expr_params(self):
return self._expr_params

def __str__(self):
return {
"anns_field": self.anns_field,
Expand Down
1 change: 1 addition & 0 deletions pymilvus/client/grpc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ def hybrid_search(
req.expr,
partition_names=partition_names,
round_decimal=round_decimal,
expr_params=req.expr_params,
**kwargs,
)
requests.append(search_request)
Expand Down
Loading

0 comments on commit cc45027

Please sign in to comment.