Skip to content

Commit 1a84f3b

Browse files
authored
Add IndexDefinition (#76)
* Add IndexDefinition * add test for definition
1 parent 0a225fc commit 1a84f3b

File tree

3 files changed

+103
-5
lines changed

3 files changed

+103
-5
lines changed

redisearch/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"""
7171
from .result import Result
7272
from .document import Document
73-
from .client import Client, NumericField, TextField, GeoField, TagField
73+
from .client import Client, NumericField, TextField, GeoField, TagField, IndexDefinition
7474
from .query import Query, NumericFilter, GeoFilter, SortbyField
7575
from .auto_complete import AutoCompleter, Suggestion
7676

redisearch/client.py

+59-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,62 @@ def __init__(self, name, separator=',', no_index=False):
9797
Field.__init__(self, name, *args)
9898

9999

100+
class IndexDefinition(object):
101+
"""
102+
IndexDefinition is used to define a index definition for automatic indexing on Hash update
103+
"""
104+
105+
ON = 'ON'
106+
HASH = 'HASH'
107+
ASYNC = 'ASYNC'
108+
PREFIX = 'PREFIX'
109+
FILTER = 'FILTER'
110+
LANGUAGE_FIELD = 'LANGUAGE_FIELD'
111+
LANGUAGE = 'LANGUAGE'
112+
SCORE_FIELD = 'SCORE_FIELD'
113+
SCORE = 'SCORE'
114+
PAYLOAD_FIELD = 'PAYLOAD_FIELD'
115+
116+
def __init__(self, async=False, prefix=[], filter=None, language_field=None, language=None, score_field=None, score=1.0, payload_field=None):
117+
118+
args = [self.ON, self.HASH]
119+
120+
if async:
121+
args.append(self.ASYNC)
122+
123+
if len(prefix) > 0:
124+
args.append(self.PREFIX)
125+
args.append(len(prefix))
126+
for p in prefix:
127+
args.append(p)
128+
129+
if filter is not None:
130+
args.append(self.FILTER)
131+
args.append(filter)
132+
133+
if language_field is not None:
134+
args.append(self.LANGUAGE_FIELD)
135+
args.append(language_field)
136+
137+
if language is not None:
138+
args.append(self.LANGUAGE)
139+
args.append(language)
140+
141+
if score_field is not None:
142+
args.append(self.SCORE_FIELD)
143+
args.append(score_field)
144+
145+
if score is not None:
146+
args.append(self.SCORE)
147+
args.append(score)
148+
149+
if payload_field is not None:
150+
args.append(self.PAYLOAD_FIELD)
151+
args.append(payload_field)
152+
153+
self.args = args
154+
155+
100156
class Client(object):
101157
"""
102158
A client for the RediSearch module.
@@ -198,7 +254,7 @@ def batch_indexer(self, chunk_size=100):
198254
return Client.BatchIndexer(self, chunk_size=chunk_size)
199255

200256
def create_index(self, fields, no_term_offsets=False,
201-
no_field_flags=False, stopwords=None):
257+
no_field_flags=False, stopwords=None, definition=None):
202258
"""
203259
Create the search index. The index must not already exist.
204260
@@ -211,6 +267,8 @@ def create_index(self, fields, no_term_offsets=False,
211267
"""
212268

213269
args = [self.CREATE_CMD, self.index_name]
270+
if definition is not None:
271+
args += definition.args
214272
if no_term_offsets:
215273
args.append(self.NOOFFSETS)
216274
if no_field_flags:

test/test.py

+43-3
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020

2121
class RedisSearchTestCase(ModuleTestCase('../module.so')):
2222

23-
def createIndex(self, client, num_docs = 100):
23+
def createIndex(self, client, num_docs = 100, definition=None):
2424

2525
assert isinstance(client, Client)
2626
try:
2727
client.create_index((TextField('play', weight=5.0),
2828
TextField('txt'),
29-
NumericField('chapter')))
29+
NumericField('chapter')), definition=definition)
3030
except redis.ResponseError:
3131
client.drop_index()
32-
return self.createIndex(client, num_docs=num_docs)
32+
return self.createIndex(client, num_docs=num_docs, definition=definition)
3333

3434
chapters = {}
3535
bzfp = bz2.BZ2File(WILL_PLAY_TEXT)
@@ -781,6 +781,46 @@ def testAggregations(self):
781781
self.assertEqual(b'RediSearch', res[23])
782782
self.assertEqual(2, len(res[25]))
783783

784+
def testIndexDefiniontion(self):
785+
786+
conn = self.redis()
787+
788+
with conn as r:
789+
r.flushdb()
790+
client = Client('test', port=conn.port)
791+
792+
definition = IndexDefinition(async=True, prefix=['hset:', 'henry'],
793+
filter='@f1==32', language='English', language_field='play',
794+
score_field='chapter', score=0.5, payload_field='txt' )
795+
796+
self.assertEqual(['ON','HASH','ASYNC','PREFIX',2,'hset:','henry',
797+
'FILTER','@f1==32','LANGUAGE_FIELD','play','LANGUAGE','English',
798+
'SCORE_FIELD','chapter','SCORE',0.5,'PAYLOAD_FIELD','txt'],
799+
definition.args)
800+
801+
self.createIndex(client, num_docs=500, definition=definition)
802+
803+
804+
def testCreateClientDefiniontion(self):
805+
806+
conn = self.redis()
807+
808+
with conn as r:
809+
r.flushdb()
810+
client = Client('test', port=conn.port)
811+
812+
definition = IndexDefinition(prefix=['hset:', 'henry'])
813+
self.createIndex(client, num_docs=500, definition=definition)
814+
815+
info = client.info()
816+
self.assertEqual(494, int(info['num_docs']))
817+
818+
r.hset('hset:1', 'f1', 'v1');
819+
820+
info = client.info()
821+
self.assertEqual(495, int(info['num_docs']))
822+
823+
784824
if __name__ == '__main__':
785825

786826
unittest.main()

0 commit comments

Comments
 (0)