1
1
import itertools
2
2
import time
3
+ from typing import Dict , Union
3
4
4
5
from ..helpers import parse_to_dict
5
6
from ._util import to_string
@@ -377,7 +378,17 @@ def info(self):
377
378
it = map (to_string , res )
378
379
return dict (zip (it , it ))
379
380
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 ]]):
381
392
args = [self .index_name ]
382
393
383
394
if isinstance (query , str ):
@@ -387,9 +398,16 @@ def _mk_query_args(self, query):
387
398
raise ValueError (f"Bad query type { type (query )} " )
388
399
389
400
args += query .get_args ()
401
+ if query_params is not None :
402
+ args += self .get_params_args (query_params )
403
+
390
404
return args , query
391
405
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
+ ):
393
411
"""
394
412
Search the index for a given query, and return a result of documents
395
413
@@ -401,7 +419,7 @@ def search(self, query):
401
419
402
420
For more information: https://oss.redis.com/redisearch/Commands/#ftsearch
403
421
""" # noqa
404
- args , query = self ._mk_query_args (query )
422
+ args , query = self ._mk_query_args (query , query_params = query_params )
405
423
st = time .time ()
406
424
res = self .execute_command (SEARCH_CMD , * args )
407
425
@@ -413,18 +431,26 @@ def search(self, query):
413
431
with_scores = query ._with_scores ,
414
432
)
415
433
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
+ ):
417
439
"""Returns the execution plan for a complex query.
418
440
419
441
For more information: https://oss.redis.com/redisearch/Commands/#ftexplain
420
442
""" # noqa
421
- args , query_text = self ._mk_query_args (query )
443
+ args , query_text = self ._mk_query_args (query , query_params = query_params )
422
444
return self .execute_command (EXPLAIN_CMD , * args )
423
445
424
- def explain_cli (self , query ): # noqa
446
+ def explain_cli (self , query : Union [ str , Query ] ): # noqa
425
447
raise NotImplementedError ("EXPLAINCLI will not be implemented." )
426
448
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
+ ):
428
454
"""
429
455
Issue an aggregation query.
430
456
@@ -445,6 +471,8 @@ def aggregate(self, query):
445
471
cmd = [CURSOR_CMD , "READ" , self .index_name ] + query .build_args ()
446
472
else :
447
473
raise ValueError ("Bad query" , query )
474
+ if query_params is not None :
475
+ cmd += self .get_params_args (query_params )
448
476
449
477
raw = self .execute_command (* cmd )
450
478
return self ._get_aggregate_result (raw , query , has_cursor )
0 commit comments