7
7
from ._util import to_string
8
8
from .aggregation import AggregateRequest , AggregateResult , Cursor
9
9
from .suggestion import SuggestionParser
10
+ from ..helpers import parse_to_dict
10
11
11
12
NUMERIC = "NUMERIC"
12
13
20
21
EXPLAINCLI_CMD = "FT.EXPLAINCLI"
21
22
DEL_CMD = "FT.DEL"
22
23
AGGREGATE_CMD = "FT.AGGREGATE"
24
+ PROFILE_CMD = "FT.PROFILE"
23
25
CURSOR_CMD = "FT.CURSOR"
24
26
SPELLCHECK_CMD = "FT.SPELLCHECK"
25
27
DICT_ADD_CMD = "FT.DICTADD"
@@ -382,11 +384,11 @@ def explain_cli(self, query): # noqa
382
384
383
385
def aggregate (self , query ):
384
386
"""
385
- Issue an aggregation query
387
+ Issue an aggregation query.
386
388
387
389
### Parameters
388
390
389
- **query**: This can be either an `AggeregateRequest `, or a `Cursor`
391
+ **query**: This can be either an `AggregateRequest `, or a `Cursor`
390
392
391
393
An `AggregateResult` object is returned. You can access the rows from
392
394
its `rows` property, which will always yield the rows of the result.
@@ -401,6 +403,10 @@ def aggregate(self, query):
401
403
raise ValueError ("Bad query" , query )
402
404
403
405
raw = self .execute_command (* cmd )
406
+ return self ._get_AggregateResult (raw , query , has_cursor )
407
+
408
+ def _get_AggregateResult (self , raw , query , has_cursor ):
409
+ # has_cursor = bool(query._cursor)
404
410
if has_cursor :
405
411
if isinstance (query , Cursor ):
406
412
query .cid = raw [1 ]
@@ -418,8 +424,51 @@ def aggregate(self, query):
418
424
schema = None
419
425
rows = raw [1 :]
420
426
421
- res = AggregateResult (rows , cursor , schema )
422
- return res
427
+ return AggregateResult (rows , cursor , schema )
428
+
429
+ def profile (self , query , limited = False ):
430
+ """
431
+ Performs a search or aggregate command and collects performance
432
+ information.
433
+
434
+ ### Parameters
435
+
436
+ **query**: This can be either an `AggregateRequest`, `Query` or
437
+ string.
438
+ **limited**: If set to True, removes details of reader iterator.
439
+
440
+ """
441
+ st = time .time ()
442
+ cmd = [PROFILE_CMD , self .index_name , "" ]
443
+ if limited :
444
+ cmd .append ("LIMITED" )
445
+ cmd .append ('QUERY' )
446
+
447
+ if isinstance (query , AggregateRequest ):
448
+ cmd [2 ] = "AGGREGATE"
449
+ cmd += query .build_args ()
450
+ elif isinstance (query , Query ):
451
+ cmd [2 ] = "SEARCH"
452
+ cmd += query .get_args ()
453
+ elif isinstance (query , str ):
454
+ cmd [2 ] = "SEARCH"
455
+ cmd .append (query )
456
+ else :
457
+ raise ValueError ("Must provide AggregateRequest object, "
458
+ "Query object or str." )
459
+
460
+ res = self .execute_command (* cmd )
461
+
462
+ if isinstance (query , AggregateRequest ):
463
+ result = self ._get_AggregateResult (res [0 ], query , query ._cursor )
464
+ else :
465
+ result = Result (res [0 ],
466
+ not query ._no_content ,
467
+ duration = (time .time () - st ) * 1000.0 ,
468
+ has_payload = query ._with_payloads ,
469
+ with_scores = query ._with_scores ,)
470
+
471
+ return result , parse_to_dict (res [1 ])
423
472
424
473
def spellcheck (self , query , distance = None , include = None , exclude = None ):
425
474
"""
0 commit comments