Skip to content

Commit 5aa42a6

Browse files
authored
move sortable and no_index to the field field abstract… (#118)
* fix #34 #116 - move sortable and no_index to the field field abstract class * add tests for no_index and sortable flags
1 parent 6fa0721 commit 5aa42a6

File tree

2 files changed

+57
-51
lines changed

2 files changed

+57
-51
lines changed

redisearch/client.py

+32-41
Original file line numberDiff line numberDiff line change
@@ -19,81 +19,72 @@ class Field(object):
1919
TAG = 'TAG'
2020
SORTABLE = 'SORTABLE'
2121
NOINDEX = 'NOINDEX'
22-
SEPARATOR = 'SEPARATOR'
23-
PHONETIC = 'PHONETIC'
2422

25-
def __init__(self, name, *args):
23+
def __init__(self, name, *args, sortable=False, no_index=False):
2624
self.name = name
27-
self.args = args
25+
self.args = list(args)
26+
self.args_suffix = list()
27+
28+
if sortable:
29+
self.args_suffix.append(Field.SORTABLE)
30+
if no_index:
31+
self.args_suffix.append(Field.NOINDEX)
32+
33+
if no_index and not sortable:
34+
raise ValueError('Non-Sortable non-Indexable fields are ignored')
35+
36+
def append_arg(self, value):
37+
self.args.append(value)
2838

2939
def redis_args(self):
30-
return [self.name] + list(self.args)
40+
return [self.name] + self.args + self.args_suffix
3141

3242

3343
class TextField(Field):
3444
"""
3545
TextField is used to define a text field in a schema definition
3646
"""
3747
NOSTEM = 'NOSTEM'
48+
PHONETIC = 'PHONETIC'
49+
50+
def __init__(self, name, weight=1.0, no_stem=False, phonetic_matcher=None, **kwargs):
51+
Field.__init__(self, name, Field.TEXT, Field.WEIGHT, weight, **kwargs)
3852

39-
def __init__(self, name, weight=1.0, sortable=False, no_stem=False,
40-
no_index=False, phonetic_matcher=None):
41-
args = [Field.TEXT, Field.WEIGHT, weight]
4253
if no_stem:
43-
args.append(self.NOSTEM)
44-
if sortable:
45-
args.append(Field.SORTABLE)
46-
if no_index:
47-
args.append(self.NOINDEX)
54+
Field.append_arg(self, self.NOSTEM)
4855
if phonetic_matcher and phonetic_matcher in ['dm:en', 'dm:fr', 'dm:pt', 'dm:es']:
49-
args.append(self.PHONETIC)
50-
args.append(phonetic_matcher)
51-
52-
if no_index and not sortable:
53-
raise ValueError('Non-Sortable non-Indexable fields are ignored')
54-
Field.__init__(self, name, *args)
56+
Field.append_arg(self, self.PHONETIC)
57+
Field.append_arg(self, phonetic_matcher)
5558

5659

5760
class NumericField(Field):
5861
"""
59-
NumericField is used to define a numeric field in a schema defintion
62+
NumericField is used to define a numeric field in a schema definition
6063
"""
6164

62-
def __init__(self, name, sortable=False, no_index=False):
63-
args = [Field.NUMERIC]
64-
if sortable:
65-
args.append(Field.SORTABLE)
66-
if no_index:
67-
args.append(Field.NOINDEX)
68-
69-
if no_index and not sortable:
70-
raise ValueError('Non-Sortable non-Indexable fields are ignored')
71-
72-
super(NumericField, self).__init__(name, *args)
65+
def __init__(self, name, **kwargs):
66+
Field.__init__(self, name, Field.NUMERIC, **kwargs)
7367

7468

7569
class GeoField(Field):
7670
"""
77-
GeoField is used to define a geo-indexing field in a schema defintion
71+
GeoField is used to define a geo-indexing field in a schema definition
7872
"""
7973

80-
def __init__(self, name):
81-
Field.__init__(self, name, Field.GEO)
74+
def __init__(self, name, **kwargs):
75+
Field.__init__(self, name, Field.GEO, **kwargs)
8276

8377

8478
class TagField(Field):
79+
SEPARATOR = 'SEPARATOR'
80+
8581
"""
8682
TagField is a tag-indexing field with simpler compression and tokenization.
8783
See http://redisearch.io/Tags/
8884
"""
8985

90-
def __init__(self, name, separator=',', no_index=False):
91-
args = [Field.TAG, Field.SEPARATOR, separator]
92-
93-
if no_index:
94-
args.append(Field.NOINDEX)
95-
96-
Field.__init__(self, name, *args)
86+
def __init__(self, name, separator=',', **kwargs):
87+
Field.__init__(self, name, Field.TAG, self.SEPARATOR, separator, **kwargs)
9788

9889

9990
class IndexDefinition(object):

test/test.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -489,27 +489,42 @@ def testNoIndex(self):
489489
client = self.getCleanClient('idx')
490490

491491
client.create_index(
492-
(TextField('f1', no_index=True, sortable=True), TextField('f2')))
492+
(TextField('field'),
493+
TextField('text', no_index=True, sortable=True),
494+
NumericField('numeric', no_index=True, sortable=True),
495+
GeoField('geo', no_index=True, sortable=True),
496+
TagField('tag', no_index=True, sortable=True)))
493497

494-
client.add_document('doc1', f1='MarkZZ', f2='MarkZZ')
495-
client.add_document('doc2', f1='MarkAA', f2='MarkAA')
498+
client.add_document('doc1', field='aaa', text='1', numeric='1', geo='1,1', tag='1')
499+
client.add_document('doc2', field='aab', text='2', numeric='2', geo='2,2', tag='2')
496500

497-
res = client.search(Query('@f1:Mark*'))
501+
res = client.search(Query('@text:aa*'))
498502
self.assertEqual(0, res.total)
499503

500-
res = client.search(Query('@f2:Mark*'))
504+
res = client.search(Query('@field:aa*'))
501505
self.assertEqual(2, res.total)
502506

503-
res = client.search(Query('@f2:Mark*').sort_by('f1', asc=False))
507+
res = client.search(Query('*').sort_by('text', asc=False))
504508
self.assertEqual(2, res.total)
509+
self.assertEqual('doc2', res.docs[0].id)
510+
511+
res = client.search(Query('*').sort_by('text', asc=True))
505512
self.assertEqual('doc1', res.docs[0].id)
506513

507-
res = client.search(Query('@f2:Mark*').sort_by('f1', asc=True))
508-
self.assertEqual('doc2', res.docs[0].id)
514+
res = client.search(Query('*').sort_by('numeric', asc=True))
515+
self.assertEqual('doc1', res.docs[0].id)
516+
517+
res = client.search(Query('*').sort_by('geo', asc=True))
518+
self.assertEqual('doc1', res.docs[0].id)
519+
520+
res = client.search(Query('*').sort_by('tag', asc=True))
521+
self.assertEqual('doc1', res.docs[0].id)
509522

510523
# Ensure exception is raised for non-indexable, non-sortable fields
511-
self.assertRaises(Exception, TextField,
512-
'name', no_index=True, sortable=False)
524+
self.assertRaises(Exception, TextField, 'name', no_index=True, sortable=False)
525+
self.assertRaises(Exception, NumericField, 'name', no_index=True, sortable=False)
526+
self.assertRaises(Exception, GeoField, 'name', no_index=True, sortable=False)
527+
self.assertRaises(Exception, TagField, 'name', no_index=True, sortable=False)
513528

514529
def testPartial(self):
515530
client = self.getCleanClient('idx')

0 commit comments

Comments
 (0)