Skip to content

Commit d34b6c2

Browse files
committed
feat: Added support for ADDHASH command
1 parent f8ec328 commit d34b6c2

File tree

3 files changed

+98
-16
lines changed

3 files changed

+98
-16
lines changed

API.md

+31
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,25 @@ Add a single document to the index.
195195
NOTE: Geo points shoule be encoded as strings of "lon,lat"
196196

197197

198+
### add\_document\_hash
199+
```py
200+
201+
def add_document_hash(self, doc_id, score=1.0, language=None, replace=False)
202+
203+
```
204+
205+
206+
207+
Add a hash document to the index.
208+
209+
### Parameters
210+
211+
- **doc_id**: the document's id. This has to be an existing HASH key in Redis that will hold the fields the index needs.
212+
- **score**: the document ranking, between 0.0 and 1.0
213+
- **replace**: if True, and the document already is in the index, we perform an update and reindex the document
214+
- **language**: Specify the language used for document tokenization.
215+
216+
198217
### aggregate
199218
```py
200219

@@ -361,6 +380,18 @@ def add_document(self, doc_id, nosave=False, score=1.0, payload=None, replace=Fa
361380
Add a document to the batch query
362381

363382

383+
### add\_document\_hash
384+
```py
385+
386+
def add_document_hash(self, doc_id, score=1.0, language=None, replace=False)
387+
388+
```
389+
390+
391+
392+
Add a hash document to the batch query
393+
394+
364395
### commit
365396
```py
366397

gendoc.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,4 @@ def generatedocs(module):
7171
print("Error while trying to import " + module)
7272

7373
if __name__ == '__main__':
74-
75-
print generatedocs(sys.argv[1])
74+
print(generatedocs(sys.argv[1]))

redisearch/client.py

+66-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Field(object):
2626
def __init__(self, name, *args):
2727
self.name = name
2828
self.args = args
29-
29+
3030
def redis_args(self):
3131
return [self.name] + list(self.args)
3232

@@ -87,14 +87,15 @@ class TagField(Field):
8787
TagField is a tag-indexing field with simpler compression and tokenization.
8888
See http://redisearch.io/Tags/
8989
"""
90-
def __init__(self, name, separator = ',', no_index=False):
90+
91+
def __init__(self, name, separator=',', no_index=False):
9192
args = [Field.TAG, Field.SEPARATOR, separator]
9293

9394
if no_index:
9495
args.append(Field.NOINDEX)
9596

9697
Field.__init__(self, name, *args)
97-
98+
9899

99100
class Client(object):
100101
"""
@@ -108,6 +109,7 @@ class Client(object):
108109
ALTER_CMD = 'FT.ALTER'
109110
SEARCH_CMD = 'FT.SEARCH'
110111
ADD_CMD = 'FT.ADD'
112+
ADDHASH_CMD = "FT.ADDHASH"
111113
DROP_CMD = 'FT.DROP'
112114
EXPLAIN_CMD = 'FT.EXPLAIN'
113115
DEL_CMD = 'FT.DEL'
@@ -120,7 +122,6 @@ class Client(object):
120122
GET_CMD = 'FT.GET'
121123
MGET_CMD = 'FT.MGET'
122124

123-
124125
NOOFFSETS = 'NOOFFSETS'
125126
NOFIELDS = 'NOFIELDS'
126127
STOPWORDS = 'STOPWORDS'
@@ -156,6 +157,20 @@ def add_document(self, doc_id, nosave=False, score=1.0, payload=None,
156157
if self.current_chunk >= self.chunk_size:
157158
self.commit()
158159

160+
def add_document_hash(
161+
self, doc_id, score=1.0, replace=False,
162+
):
163+
"""
164+
Add a hash to the batch query
165+
"""
166+
self.client._add_document_hash(
167+
doc_id, conn=self.pipeline, score=score, replace=replace,
168+
)
169+
self.current_chunk += 1
170+
self.total += 1
171+
if self.current_chunk >= self.chunk_size:
172+
self.commit()
173+
159174
def commit(self):
160175
"""
161176
Manually commit and flush the batch indexing query
@@ -182,7 +197,7 @@ def batch_indexer(self, chunk_size=100):
182197
return Client.BatchIndexer(self, chunk_size=chunk_size)
183198

184199
def create_index(self, fields, no_term_offsets=False,
185-
no_field_flags=False, stopwords = None):
200+
no_field_flags=False, stopwords=None):
186201
"""
187202
Create the search index. The index must not already exist.
188203
@@ -203,7 +218,7 @@ def create_index(self, fields, no_term_offsets=False,
203218
args += [self.STOPWORDS, len(stopwords)]
204219
if len(stopwords) > 0:
205220
args += list(stopwords)
206-
221+
207222
args.append('SCHEMA')
208223

209224
args += list(itertools.chain(*(f.redis_args() for f in fields)))
@@ -230,7 +245,7 @@ def drop_index(self):
230245
Drop the index if it exists
231246
"""
232247
return self.redis.execute_command(self.DROP_CMD, self.index_name)
233-
248+
234249
def _add_document(self, doc_id, conn=None, nosave=False, score=1.0, payload=None,
235250
replace=False, partial=False, language=None, no_create=False, **fields):
236251
"""
@@ -260,6 +275,25 @@ def _add_document(self, doc_id, conn=None, nosave=False, score=1.0, payload=None
260275
args += list(itertools.chain(*fields.items()))
261276
return conn.execute_command(*args)
262277

278+
def _add_document_hash(
279+
self, doc_id, conn=None, score=1.0, language=None, replace=False,
280+
):
281+
"""
282+
Internal add_document_hash used for both batch and single doc indexing
283+
"""
284+
if conn is None:
285+
conn = self.redis
286+
287+
args = [self.ADDHASH_CMD, self.index_name, doc_id, score]
288+
289+
if replace:
290+
args.append("REPLACE")
291+
292+
if language:
293+
args += ["LANGUAGE", language]
294+
295+
return conn.execute_command(*args)
296+
263297
def add_document(self, doc_id, nosave=False, score=1.0, payload=None,
264298
replace=False, partial=False, language=None, no_create=False, **fields):
265299
"""
@@ -281,10 +315,27 @@ def add_document(self, doc_id, nosave=False, score=1.0, payload=None,
281315
- **fields** kwargs dictionary of the document fields to be saved and/or indexed.
282316
NOTE: Geo points shoule be encoded as strings of "lon,lat"
283317
"""
284-
return self._add_document(doc_id, conn=None, nosave=nosave, score=score,
318+
return self._add_document(doc_id, conn=None, nosave=nosave, score=score,
285319
payload=payload, replace=replace,
286-
partial=partial, language=language,
287-
no_create=no_create,**fields)
320+
partial=partial, language=language,
321+
no_create=no_create, **fields)
322+
323+
def add_document_hash(
324+
self, doc_id, score=1.0, language=None, replace=False,
325+
):
326+
"""
327+
Add a hash document to the index.
328+
329+
### Parameters
330+
331+
- **doc_id**: the document's id. This has to be an existing HASH key in Redis that will hold the fields the index needs.
332+
- **score**: the document ranking, between 0.0 and 1.0
333+
- **replace**: if True, and the document already is in the index, we perform an update and reindex the document
334+
- **language**: Specify the language used for document tokenization.
335+
"""
336+
return self._add_document_hash(
337+
doc_id, conn=None, score=score, language=language, replace=replace,
338+
)
288339

289340
def delete_document(self, doc_id, conn=None):
290341
"""
@@ -315,12 +366,12 @@ def load_document(self, id):
315366
def get(self, *ids):
316367
"""
317368
Returns the full contents of multiple documents.
318-
369+
319370
### Parameters
320-
371+
321372
- **ids**: the ids of the saved documents.
322373
"""
323-
374+
324375
return self.redis.execute_command('FT.MGET', self.index_name, *ids)
325376

326377
def info(self):
@@ -386,7 +437,8 @@ def aggregate(self, query):
386437
elif isinstance(query, Cursor):
387438
has_schema = False
388439
has_cursor = True
389-
cmd = [self.CURSOR_CMD, 'READ', self.index_name] + query.build_args()
440+
cmd = [self.CURSOR_CMD, 'READ',
441+
self.index_name] + query.build_args()
390442
else:
391443
raise ValueError('Bad query', query)
392444

0 commit comments

Comments
 (0)