Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [2.4] Support hybrid search with expression template #2490

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 27 additions & 2 deletions examples/hybrid_search/hybrid_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,39 @@
req = AnnSearchRequest(**search_param)
req_list.append(req)

print(fmt.format("rank by WightedRanker"))
hybrid_res = hello_milvus.hybrid_search(req_list, WeightedRanker(*weights), default_limit, output_fields=["random"])
for hits in hybrid_res:
for hit in hits:
print(f" hybrid search hit: {hit}")

print("rank by WightedRanker")
print(fmt.format("rank by RRFRanker"))
hybrid_res = hello_milvus.hybrid_search(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 WightedRanker with expression template"))
hybrid_res = hello_milvus.hybrid_search(req_list, WeightedRanker(*weights), default_limit, output_fields=["random"])
for hits in hybrid_res:
for hit in hits:
print(f" hybrid search hit: {hit}")

print("rank by RRFRanker")
print(fmt.format("rank by RRFRanker with expression template"))
hybrid_res = hello_milvus.hybrid_search(req_list, RRFRanker(), default_limit, output_fields=["random"])
for hits in hybrid_res:
for hit in hits:
Expand Down
6 changes: 6 additions & 0 deletions pymilvus/client/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,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 @@ -361,6 +362,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 @@ -382,6 +384,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 @@ -863,6 +863,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
4 changes: 3 additions & 1 deletion pymilvus/client/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,9 @@ def search_requests_with_expr(
placeholder_group=plg_str,
dsl_type=common_types.DslType.BoolExprV1,
search_params=req_params,
expr_template_values=cls.prepare_expression_template(kwargs.get("expr_params", {})),
expr_template_values=cls.prepare_expression_template(
{} if kwargs.get("expr_params") is None else kwargs.get("expr_params")
),
)
if expr is not None:
request.dsl = expr
Expand Down
1 change: 0 additions & 1 deletion pymilvus/milvus_client/async_milvus_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ async def hybrid_search(
partition_names: Optional[List[str]] = None,
**kwargs,
) -> List[List[dict]]:

conn = self._get_connection()
try:
res = await conn.hybrid_search(
Expand Down
Loading