Skip to content

Commit 86085d7

Browse files
authored
fix #52 add support for NOCREATE (#53)
* fix #52 add support for NOCREATE
1 parent c682ce5 commit 86085d7

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

redisearch/client.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ def __del__(self):
144144
self.commit()
145145

146146
def add_document(self, doc_id, nosave=False, score=1.0, payload=None,
147-
replace=False, partial=False, **fields):
147+
replace=False, partial=False, no_create=False, **fields):
148148
"""
149149
Add a document to the batch query
150150
"""
151151
self.client._add_document(doc_id, conn=self.pipeline, nosave=nosave, score=score,
152152
payload=payload, replace=replace,
153-
partial=partial, **fields)
153+
partial=partial, no_create=no_create, **fields)
154154
self.current_chunk += 1
155155
self.total += 1
156156
if self.current_chunk >= self.chunk_size:
@@ -232,14 +232,14 @@ def drop_index(self):
232232
return self.redis.execute_command(self.DROP_CMD, self.index_name)
233233

234234
def _add_document(self, doc_id, conn=None, nosave=False, score=1.0, payload=None,
235-
replace=False, partial=False, language=None, **fields):
235+
replace=False, partial=False, language=None, no_create=False, **fields):
236236
"""
237237
Internal add_document used for both batch and single doc indexing
238238
"""
239239
if conn is None:
240240
conn = self.redis
241241

242-
if partial:
242+
if partial or no_create:
243243
replace = True
244244

245245
args = [self.ADD_CMD, self.index_name, doc_id, score]
@@ -252,14 +252,16 @@ def _add_document(self, doc_id, conn=None, nosave=False, score=1.0, payload=None
252252
args.append('REPLACE')
253253
if partial:
254254
args.append('PARTIAL')
255+
if no_create:
256+
args.append('NOCREATE')
255257
if language:
256258
args += ['LANGUAGE', language]
257259
args.append('FIELDS')
258260
args += list(itertools.chain(*fields.items()))
259261
return conn.execute_command(*args)
260262

261263
def add_document(self, doc_id, nosave=False, score=1.0, payload=None,
262-
replace=False, partial=False, language=None, **fields):
264+
replace=False, partial=False, language=None, no_create=False, **fields):
263265
"""
264266
Add a single document to the index.
265267
@@ -274,12 +276,15 @@ def add_document(self, doc_id, nosave=False, score=1.0, payload=None,
274276
This has the added benefit that any fields specified with `no_index`
275277
will not be reindexed again. Implies `replace`
276278
- **language**: Specify the language used for document tokenization.
279+
- **no_create**: if True, the document is only updated and reindexed if it already exists.
280+
If the document does not exist, an error will be returned. Implies `replace`
277281
- **fields** kwargs dictionary of the document fields to be saved and/or indexed.
278282
NOTE: Geo points shoule be encoded as strings of "lon,lat"
279283
"""
280284
return self._add_document(doc_id, conn=None, nosave=nosave, score=score,
281285
payload=payload, replace=replace,
282-
partial=partial, language=language, **fields)
286+
partial=partial, language=language,
287+
no_create=no_create,**fields)
283288

284289
def delete_document(self, doc_id, conn=None):
285290
"""

test/test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,30 @@ def testPartial(self):
436436
# values
437437
res = client.search('@f3:f3_val @f2:f2_val @f1:f1_val')
438438
self.assertEqual(1, res.total)
439+
440+
441+
def testNoCreate(self):
442+
client = self.getCleanClient('idx')
443+
client.create_index((TextField('f1'), TextField('f2'), TextField('f3')))
444+
445+
client.add_document('doc1', f1='f1_val', f2='f2_val')
446+
client.add_document('doc2', f1='f1_val', f2='f2_val')
447+
448+
client.add_document('doc1', f3='f3_val', no_create=True)
449+
client.add_document('doc2', f3='f3_val', no_create=True, partial=True)
450+
451+
for i in self.retry_with_reload():
452+
# Search for f3 value. All documents should have it
453+
res = client.search('@f3:f3_val')
454+
self.assertEqual(2, res.total)
455+
456+
# Only the document updated with PARTIAL should still have the f1 and f2
457+
# values
458+
res = client.search('@f3:f3_val @f2:f2_val @f1:f1_val')
459+
self.assertEqual(1, res.total)
460+
461+
with self.assertRaises(redis.ResponseError) as error:
462+
client.add_document('doc3', f2='f2_val', f3='f3_val', no_create=True)
439463

440464
def testExplain(self):
441465
client = self.getCleanClient('idx')

0 commit comments

Comments
 (0)