Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion redisearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"""
from .result import Result
from .document import Document
from .client import Client, NumericField, TextField, GeoField, TagField
from .client import Client, NumericField, TextField, GeoField, TagField, IndexDefinition
from .query import Query, NumericFilter, GeoFilter, SortbyField
from .auto_complete import AutoCompleter, Suggestion

Expand Down
60 changes: 59 additions & 1 deletion redisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,62 @@ def __init__(self, name, separator=',', no_index=False):
Field.__init__(self, name, *args)


class IndexDefinition(object):
"""
IndexDefinition is used to define a index definition for automatic indexing on Hash update
"""

ON = 'ON'
HASH = 'HASH'
ASYNC = 'ASYNC'
PREFIX = 'PREFIX'
FILTER = 'FILTER'
LANGUAGE_FIELD = 'LANGUAGE_FIELD'
LANGUAGE = 'LANGUAGE'
SCORE_FIELD = 'SCORE_FIELD'
SCORE = 'SCORE'
PAYLOAD_FIELD = 'PAYLOAD_FIELD'

def __init__(self, async=False, prefix=[], filter=None, language_field=None, language=None, score_field=None, score=1.0, payload_field=None):

args = [self.ON, self.HASH]

if async:
args.append(self.ASYNC)

if len(prefix) > 0:
args.append(self.PREFIX)
args.append(len(prefix))
for p in prefix:
args.append(p)

if filter is not None:
args.append(self.FILTER)
args.append(filter)

if language_field is not None:
args.append(self.LANGUAGE_FIELD)
args.append(language_field)

if language is not None:
args.append(self.LANGUAGE)
args.append(language)

if score_field is not None:
args.append(self.SCORE_FIELD)
args.append(score_field)

if score is not None:
args.append(self.SCORE)
args.append(score)

if payload_field is not None:
args.append(self.PAYLOAD_FIELD)
args.append(payload_field)

self.args = args


class Client(object):
"""
A client for the RediSearch module.
Expand Down Expand Up @@ -198,7 +254,7 @@ def batch_indexer(self, chunk_size=100):
return Client.BatchIndexer(self, chunk_size=chunk_size)

def create_index(self, fields, no_term_offsets=False,
no_field_flags=False, stopwords=None):
no_field_flags=False, stopwords=None, definition=None):
"""
Create the search index. The index must not already exist.

Expand All @@ -211,6 +267,8 @@ def create_index(self, fields, no_term_offsets=False,
"""

args = [self.CREATE_CMD, self.index_name]
if definition is not None:
args += definition.args
if no_term_offsets:
args.append(self.NOOFFSETS)
if no_field_flags:
Expand Down
46 changes: 43 additions & 3 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@

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

def createIndex(self, client, num_docs = 100):
def createIndex(self, client, num_docs = 100, definition=None):

assert isinstance(client, Client)
try:
client.create_index((TextField('play', weight=5.0),
TextField('txt'),
NumericField('chapter')))
NumericField('chapter')), definition=definition)
except redis.ResponseError:
client.drop_index()
return self.createIndex(client, num_docs=num_docs)
return self.createIndex(client, num_docs=num_docs, definition=definition)

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

def testIndexDefiniontion(self):

conn = self.redis()

with conn as r:
r.flushdb()
client = Client('test', port=conn.port)

definition = IndexDefinition(async=True, prefix=['hset:', 'henry'],
filter='@f1==32', language='English', language_field='play',
score_field='chapter', score=0.5, payload_field='txt' )

self.assertEqual(['ON','HASH','ASYNC','PREFIX',2,'hset:','henry',
'FILTER','@f1==32','LANGUAGE_FIELD','play','LANGUAGE','English',
'SCORE_FIELD','chapter','SCORE',0.5,'PAYLOAD_FIELD','txt'],
definition.args)

self.createIndex(client, num_docs=500, definition=definition)


def testCreateClientDefiniontion(self):

conn = self.redis()

with conn as r:
r.flushdb()
client = Client('test', port=conn.port)

definition = IndexDefinition(prefix=['hset:', 'henry'])
self.createIndex(client, num_docs=500, definition=definition)

info = client.info()
self.assertEqual(494, int(info['num_docs']))

r.hset('hset:1', 'f1', 'v1');

info = client.info()
self.assertEqual(495, int(info['num_docs']))


if __name__ == '__main__':

unittest.main()