Skip to content

Commit e3d4336

Browse files
amih90honzakral
authored andcommitted
Populate document.save/update result value
1 parent 868d74b commit e3d4336

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

elasticsearch_dsl/document.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ def update(self, using=None, index=None, detect_noop=True,
345345
:arg doc_as_upsert: Instead of sending a partial doc plus an upsert
346346
doc, setting doc_as_upsert to true will use the contents of doc as
347347
the upsert value
348+
349+
:return operation result noop/updated
348350
"""
349351
body = {
350352
'doc_as_upsert': doc_as_upsert,
@@ -406,6 +408,8 @@ def update(self, using=None, index=None, detect_noop=True,
406408
if '_' + k in meta:
407409
setattr(self.meta, k, meta['_' + k])
408410

411+
return meta['result']
412+
409413
def save(self, using=None, index=None, validate=True, skip_empty=True, **kwargs):
410414
"""
411415
Save the document into elasticsearch. If the document doesn't exist it
@@ -422,6 +426,8 @@ def save(self, using=None, index=None, validate=True, skip_empty=True, **kwargs)
422426
423427
Any additional keyword arguments will be passed to
424428
``Elasticsearch.index`` unchanged.
429+
430+
:return operation result created/updated
425431
"""
426432
if validate:
427433
self.full_clean()
@@ -445,8 +451,8 @@ def save(self, using=None, index=None, validate=True, skip_empty=True, **kwargs)
445451
if '_' + k in meta:
446452
setattr(self.meta, k, meta['_' + k])
447453

448-
# return True/False if the document has been created/updated
449-
return meta['result'] == 'created'
454+
return meta['result']
450455

451456
# limited backwards compatibility, to be removed in 7.0.0
452457
DocType = Document
458+

test_elasticsearch_dsl/test_integration/test_document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def test_update_object_field(write_client):
152152
w = Wiki(owner=User(name='Honza Kral'), _id='elasticsearch-py')
153153
w.save()
154154

155-
w.update(owner=[{'name': 'Honza'}, {'name': 'Nick'}])
155+
assert 'updated' == w.update(owner=[{'name': 'Honza'}, {'name': 'Nick'}])
156156
assert w.owner[0].name == 'Honza'
157157
assert w.owner[1].name == 'Nick'
158158

@@ -270,7 +270,7 @@ def test_save_updates_existing_doc(data_client):
270270

271271
elasticsearch_repo.new_field = 'testing-save'
272272
v = elasticsearch_repo.meta.version
273-
assert not elasticsearch_repo.save()
273+
assert 'updated' == elasticsearch_repo.save()
274274

275275
# assert version has been updated
276276
assert elasticsearch_repo.meta.version == v + 1

0 commit comments

Comments
 (0)