@@ -26,7 +26,7 @@ class Field(object):
26
26
def __init__ (self , name , * args ):
27
27
self .name = name
28
28
self .args = args
29
-
29
+
30
30
def redis_args (self ):
31
31
return [self .name ] + list (self .args )
32
32
@@ -87,14 +87,15 @@ class TagField(Field):
87
87
TagField is a tag-indexing field with simpler compression and tokenization.
88
88
See http://redisearch.io/Tags/
89
89
"""
90
- def __init__ (self , name , separator = ',' , no_index = False ):
90
+
91
+ def __init__ (self , name , separator = ',' , no_index = False ):
91
92
args = [Field .TAG , Field .SEPARATOR , separator ]
92
93
93
94
if no_index :
94
95
args .append (Field .NOINDEX )
95
96
96
97
Field .__init__ (self , name , * args )
97
-
98
+
98
99
99
100
class Client (object ):
100
101
"""
@@ -108,6 +109,7 @@ class Client(object):
108
109
ALTER_CMD = 'FT.ALTER'
109
110
SEARCH_CMD = 'FT.SEARCH'
110
111
ADD_CMD = 'FT.ADD'
112
+ ADDHASH_CMD = "FT.ADDHASH"
111
113
DROP_CMD = 'FT.DROP'
112
114
EXPLAIN_CMD = 'FT.EXPLAIN'
113
115
DEL_CMD = 'FT.DEL'
@@ -120,7 +122,6 @@ class Client(object):
120
122
GET_CMD = 'FT.GET'
121
123
MGET_CMD = 'FT.MGET'
122
124
123
-
124
125
NOOFFSETS = 'NOOFFSETS'
125
126
NOFIELDS = 'NOFIELDS'
126
127
STOPWORDS = 'STOPWORDS'
@@ -156,6 +157,20 @@ def add_document(self, doc_id, nosave=False, score=1.0, payload=None,
156
157
if self .current_chunk >= self .chunk_size :
157
158
self .commit ()
158
159
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
+
159
174
def commit (self ):
160
175
"""
161
176
Manually commit and flush the batch indexing query
@@ -182,7 +197,7 @@ def batch_indexer(self, chunk_size=100):
182
197
return Client .BatchIndexer (self , chunk_size = chunk_size )
183
198
184
199
def create_index (self , fields , no_term_offsets = False ,
185
- no_field_flags = False , stopwords = None ):
200
+ no_field_flags = False , stopwords = None ):
186
201
"""
187
202
Create the search index. The index must not already exist.
188
203
@@ -203,7 +218,7 @@ def create_index(self, fields, no_term_offsets=False,
203
218
args += [self .STOPWORDS , len (stopwords )]
204
219
if len (stopwords ) > 0 :
205
220
args += list (stopwords )
206
-
221
+
207
222
args .append ('SCHEMA' )
208
223
209
224
args += list (itertools .chain (* (f .redis_args () for f in fields )))
@@ -230,7 +245,7 @@ def drop_index(self):
230
245
Drop the index if it exists
231
246
"""
232
247
return self .redis .execute_command (self .DROP_CMD , self .index_name )
233
-
248
+
234
249
def _add_document (self , doc_id , conn = None , nosave = False , score = 1.0 , payload = None ,
235
250
replace = False , partial = False , language = None , no_create = False , ** fields ):
236
251
"""
@@ -260,6 +275,25 @@ def _add_document(self, doc_id, conn=None, nosave=False, score=1.0, payload=None
260
275
args += list (itertools .chain (* fields .items ()))
261
276
return conn .execute_command (* args )
262
277
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
+
263
297
def add_document (self , doc_id , nosave = False , score = 1.0 , payload = None ,
264
298
replace = False , partial = False , language = None , no_create = False , ** fields ):
265
299
"""
@@ -281,10 +315,27 @@ def add_document(self, doc_id, nosave=False, score=1.0, payload=None,
281
315
- **fields** kwargs dictionary of the document fields to be saved and/or indexed.
282
316
NOTE: Geo points shoule be encoded as strings of "lon,lat"
283
317
"""
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 ,
285
319
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
+ )
288
339
289
340
def delete_document (self , doc_id , conn = None ):
290
341
"""
@@ -315,12 +366,12 @@ def load_document(self, id):
315
366
def get (self , * ids ):
316
367
"""
317
368
Returns the full contents of multiple documents.
318
-
369
+
319
370
### Parameters
320
-
371
+
321
372
- **ids**: the ids of the saved documents.
322
373
"""
323
-
374
+
324
375
return self .redis .execute_command ('FT.MGET' , self .index_name , * ids )
325
376
326
377
def info (self ):
@@ -386,7 +437,8 @@ def aggregate(self, query):
386
437
elif isinstance (query , Cursor ):
387
438
has_schema = False
388
439
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 ()
390
442
else :
391
443
raise ValueError ('Bad query' , query )
392
444
0 commit comments