Skip to content

Add query_params to FT.PROFILE #2198

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

Merged
merged 3 commits into from
Jun 1, 2022
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
30 changes: 19 additions & 11 deletions redis/commands/search/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import itertools
import time
from typing import Dict, Union
from typing import Dict, Optional, Union

from redis.client import Pipeline

Expand Down Expand Up @@ -384,7 +384,11 @@ def info(self):
it = map(to_string, res)
return dict(zip(it, it))

def get_params_args(self, query_params: Dict[str, Union[str, int, float]]):
def get_params_args(
self, query_params: Union[Dict[str, Union[str, int, float]], None]
):
if query_params is None:
return []
args = []
if len(query_params) > 0:
args.append("params")
Expand All @@ -404,8 +408,7 @@ def _mk_query_args(self, query, query_params: Dict[str, Union[str, int, float]])
raise ValueError(f"Bad query type {type(query)}")

args += query.get_args()
if query_params is not None:
args += self.get_params_args(query_params)
args += self.get_params_args(query_params)

return args, query

Expand Down Expand Up @@ -480,8 +483,7 @@ def aggregate(
cmd = [CURSOR_CMD, "READ", self.index_name] + query.build_args()
else:
raise ValueError("Bad query", query)
if query_params is not None:
cmd += self.get_params_args(query_params)
cmd += self.get_params_args(query_params)

raw = self.execute_command(*cmd)
return self._get_aggregate_result(raw, query, has_cursor)
Expand All @@ -506,16 +508,22 @@ def _get_aggregate_result(self, raw, query, has_cursor):

return AggregateResult(rows, cursor, schema)

def profile(self, query, limited=False):
def profile(
self,
query: Union[str, Query, AggregateRequest],
limited: bool = False,
query_params: Optional[Dict[str, Union[str, int, float]]] = None,
):
"""
Performs a search or aggregate command and collects performance
information.

### Parameters

**query**: This can be either an `AggregateRequest`, `Query` or
string.
**query**: This can be either an `AggregateRequest`, `Query` or string.
**limited**: If set to True, removes details of reader iterator.
**query_params**: Define one or more value parameters.
Each parameter has a name and a value.

"""
st = time.time()
Expand All @@ -530,6 +538,7 @@ def profile(self, query, limited=False):
elif isinstance(query, Query):
cmd[2] = "SEARCH"
cmd += query.get_args()
cmd += self.get_params_args(query_params)
else:
raise ValueError("Must provide AggregateRequest object or " "Query object.")

Expand Down Expand Up @@ -928,8 +937,7 @@ async def aggregate(
cmd = [CURSOR_CMD, "READ", self.index_name] + query.build_args()
else:
raise ValueError("Bad query", query)
if query_params is not None:
cmd += self.get_params_args(query_params)
cmd += self.get_params_args(query_params)

raw = await self.execute_command(*cmd)
return self._get_aggregate_result(raw, query, has_cursor)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,30 @@ def test_profile_limited(client):
assert len(res.docs) == 3 # check also the search result


@pytest.mark.redismod
@skip_ifmodversion_lt("2.4.3", "search")
def test_profile_query_params(modclient: redis.Redis):
modclient.flushdb()
modclient.ft().create_index(
(
VectorField(
"v", "HNSW", {"TYPE": "FLOAT32", "DIM": 2, "DISTANCE_METRIC": "L2"}
),
)
)
modclient.hset("a", "v", "aaaaaaaa")
modclient.hset("b", "v", "aaaabaaa")
modclient.hset("c", "v", "aaaaabaa")
query = "*=>[KNN 2 @v $vec]"
q = Query(query).return_field("__v_score").sort_by("__v_score", True).dialect(2)
res, det = modclient.ft().profile(q, query_params={"vec": "aaaaaaaa"})
assert det["Iterators profile"]["Counter"] == 2.0
assert det["Iterators profile"]["Type"] == "VECTOR"
assert res.total == 2
assert "a" == res.docs[0].id
assert "0" == res.docs[0].__getattribute__("__v_score")


@pytest.mark.redismod
@skip_ifmodversion_lt("2.4.3", "search")
def test_vector_field(modclient):
Expand Down