11import itertools
22import time
3+ from typing import Dict , Union
34
45from ..helpers import parse_to_dict
56from ._util import to_string
@@ -377,7 +378,17 @@ def info(self):
377378 it = map (to_string , res )
378379 return dict (zip (it , it ))
379380
380- def _mk_query_args (self , query ):
381+ def get_params_args (self , query_params : Dict [str , Union [str , int , float ]]):
382+ args = []
383+ if len (query_params ) > 0 :
384+ args .append ("params" )
385+ args .append (len (query_params ) * 2 )
386+ for key , value in query_params .items ():
387+ args .append (key )
388+ args .append (value )
389+ return args
390+
391+ def _mk_query_args (self , query , query_params : Dict [str , Union [str , int , float ]]):
381392 args = [self .index_name ]
382393
383394 if isinstance (query , str ):
@@ -387,9 +398,16 @@ def _mk_query_args(self, query):
387398 raise ValueError (f"Bad query type { type (query )} " )
388399
389400 args += query .get_args ()
401+ if query_params is not None :
402+ args += self .get_params_args (query_params )
403+
390404 return args , query
391405
392- def search (self , query ):
406+ def search (
407+ self ,
408+ query : Union [str , Query ],
409+ query_params : Dict [str , Union [str , int , float ]] = None ,
410+ ):
393411 """
394412 Search the index for a given query, and return a result of documents
395413
@@ -401,7 +419,7 @@ def search(self, query):
401419
402420 For more information: https://oss.redis.com/redisearch/Commands/#ftsearch
403421 """ # noqa
404- args , query = self ._mk_query_args (query )
422+ args , query = self ._mk_query_args (query , query_params = query_params )
405423 st = time .time ()
406424 res = self .execute_command (SEARCH_CMD , * args )
407425
@@ -413,18 +431,26 @@ def search(self, query):
413431 with_scores = query ._with_scores ,
414432 )
415433
416- def explain (self , query ):
434+ def explain (
435+ self ,
436+ query = Union [str , Query ],
437+ query_params : Dict [str , Union [str , int , float ]] = None ,
438+ ):
417439 """Returns the execution plan for a complex query.
418440
419441 For more information: https://oss.redis.com/redisearch/Commands/#ftexplain
420442 """ # noqa
421- args , query_text = self ._mk_query_args (query )
443+ args , query_text = self ._mk_query_args (query , query_params = query_params )
422444 return self .execute_command (EXPLAIN_CMD , * args )
423445
424- def explain_cli (self , query ): # noqa
446+ def explain_cli (self , query : Union [ str , Query ] ): # noqa
425447 raise NotImplementedError ("EXPLAINCLI will not be implemented." )
426448
427- def aggregate (self , query ):
449+ def aggregate (
450+ self ,
451+ query : Union [str , Query ],
452+ query_params : Dict [str , Union [str , int , float ]] = None ,
453+ ):
428454 """
429455 Issue an aggregation query.
430456
@@ -445,6 +471,8 @@ def aggregate(self, query):
445471 cmd = [CURSOR_CMD , "READ" , self .index_name ] + query .build_args ()
446472 else :
447473 raise ValueError ("Bad query" , query )
474+ if query_params is not None :
475+ cmd += self .get_params_args (query_params )
448476
449477 raw = self .execute_command (* cmd )
450478 return self ._get_aggregate_result (raw , query , has_cursor )
0 commit comments