@@ -109,6 +109,10 @@ class Client(object):
109
109
DEL_CMD = 'FT.DEL'
110
110
AGGREGATE_CMD = 'FT.AGGREGATE'
111
111
CURSOR_CMD = 'FT.CURSOR'
112
+ SPELLCHECK_CMD = 'FT.SPELLCHECK'
113
+ DICT_ADD_CMD = 'FT.DICTADD'
114
+ DICT_DEL_CMD = 'FT.DICTDEL'
115
+ DICT_DUMP_CMD = 'FT.DICTDUMP'
112
116
113
117
114
118
NOOFFSETS = 'NOOFFSETS'
@@ -367,4 +371,101 @@ def aggregate(self, query):
367
371
rows = raw [1 :]
368
372
369
373
res = AggregateResult (rows , cursor , schema )
370
- return res
374
+ return res
375
+
376
+ def spellcheck (self , query , distance = None , include = None , exclude = None ):
377
+ """
378
+ Issue a spellcheck query
379
+
380
+ ### Parameters
381
+
382
+ **query**: search query.
383
+ **distance***: the maximal Levenshtein distance for spelling suggestions (default: 1, max: 4).
384
+ **include**: specifies an inclusion custom dictionary.
385
+ **exclude**: specifies an exclusion custom dictionary.
386
+ """
387
+ cmd = [self .SPELLCHECK_CMD , self .index_name , query ]
388
+ if distance :
389
+ cmd .extend (['DISTANCE' , distance ])
390
+
391
+ if include :
392
+ cmd .extend (['TERMS' , 'INCLUDE' , include ])
393
+
394
+ if exclude :
395
+ cmd .extend (['TERMS' , 'EXCLUDE' , exclude ])
396
+
397
+ raw = self .redis .execute_command (* cmd )
398
+
399
+ corrections = {}
400
+ if raw == 0 :
401
+ return corrections
402
+
403
+ for _correction in raw :
404
+ if isinstance (_correction , long ) and _correction == 0 :
405
+ continue
406
+
407
+ if len (_correction ) != 3 :
408
+ continue
409
+ if not _correction [2 ]:
410
+ continue
411
+ if not _correction [2 ][0 ]:
412
+ continue
413
+
414
+ # For spellcheck output
415
+ # 1) 1) "TERM"
416
+ # 2) "{term1}"
417
+ # 3) 1) 1) "{score1}"
418
+ # 2) "{suggestion1}"
419
+ # 2) 1) "{score2}"
420
+ # 2) "{suggestion2}"
421
+ #
422
+ # Following dictionary will be made
423
+ # corrections = {
424
+ # '{term1}': [
425
+ # {'score': '{score1}', 'suggestion': '{suggestion1}'},
426
+ # {'score': '{score2}', 'suggestion': '{suggestion2}'}
427
+ # ]
428
+ # }
429
+ corrections [_correction [1 ]] = [
430
+ {'score' : _item [0 ], 'suggestion' :_item [1 ]}
431
+ for _item in _correction [2 ]
432
+ ]
433
+
434
+ return corrections
435
+
436
+ def dict_add (self , name , * terms ):
437
+ """Adds terms to a dictionary.
438
+
439
+ ### Parameters
440
+
441
+ - **name**: Dictionary name.
442
+ - **terms**: List of items for adding to the dictionary.
443
+ """
444
+ cmd = [self .DICT_ADD_CMD , name ]
445
+ cmd .extend (terms )
446
+ raw = self .redis .execute_command (* cmd )
447
+ return raw
448
+
449
+ def dict_del (self , name , * terms ):
450
+ """Deletes terms from a dictionary.
451
+
452
+ ### Parameters
453
+
454
+ - **name**: Dictionary name.
455
+ - **terms**: List of items for removing from the dictionary.
456
+ """
457
+ cmd = [self .DICT_DEL_CMD , name ]
458
+ cmd .extend (terms )
459
+ raw = self .redis .execute_command (* cmd )
460
+ return raw
461
+
462
+ def dict_dump (self , name ):
463
+ """Dumps all terms in the given dictionary.
464
+
465
+ ### Parameters
466
+
467
+ - **name**: Dictionary name.
468
+ """
469
+ cmd = [self .DICT_DUMP_CMD , name ]
470
+ raw = self .redis .execute_command (* cmd )
471
+ return raw
0 commit comments