Skip to content

Commit 10e03f5

Browse files
committed
[7.x] Add return_doc_meta option to Document.save() and .update()
1 parent c271026 commit 10e03f5

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

elasticsearch_dsl/document.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ def update(
331331
script_id=None,
332332
scripted_upsert=False,
333333
upsert=None,
334+
return_doc_meta=False,
334335
**fields
335336
):
336337
"""
@@ -356,6 +357,8 @@ def update(
356357
:arg doc_as_upsert: Instead of sending a partial doc plus an upsert
357358
doc, setting doc_as_upsert to true will use the contents of doc as
358359
the upsert value
360+
:arg return_doc_meta: set to ``True`` to return all metadata from the
361+
index API call instead of only the operation result
359362
360363
:return operation result noop/updated
361364
"""
@@ -415,9 +418,17 @@ def update(
415418
if "_" + k in meta:
416419
setattr(self.meta, k, meta["_" + k])
417420

418-
return meta["result"]
421+
return meta if return_doc_meta else meta["result"]
419422

420-
def save(self, using=None, index=None, validate=True, skip_empty=True, **kwargs):
423+
def save(
424+
self,
425+
using=None,
426+
index=None,
427+
validate=True,
428+
skip_empty=True,
429+
return_doc_meta=False,
430+
**kwargs
431+
):
421432
"""
422433
Save the document into elasticsearch. If the document doesn't exist it
423434
is created, it is overwritten otherwise. Returns ``True`` if this
@@ -430,6 +441,8 @@ def save(self, using=None, index=None, validate=True, skip_empty=True, **kwargs)
430441
:arg skip_empty: if set to ``False`` will cause empty values (``None``,
431442
``[]``, ``{}``) to be left on the document. Those values will be
432443
stripped out otherwise as they make no difference in elasticsearch.
444+
:arg return_doc_meta: set to ``True`` to return all metadata from the
445+
update API call instead of only the operation result
433446
434447
Any additional keyword arguments will be passed to
435448
``Elasticsearch.index`` unchanged.
@@ -459,4 +472,4 @@ def save(self, using=None, index=None, validate=True, skip_empty=True, **kwargs)
459472
if "_" + k in meta:
460473
setattr(self.meta, k, meta["_" + k])
461474

462-
return meta["result"]
475+
return meta if return_doc_meta else meta["result"]

tests/test_integration/test_document.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,40 @@ def test_update_script(write_client):
234234
assert w.views == 47
235235

236236

237+
def test_save_and_update_return_doc_meta(write_client):
238+
Wiki.init()
239+
w = Wiki(owner=User(name="Honza Kral"), _id="elasticsearch-py", views=42)
240+
resp = w.save(return_doc_meta=True)
241+
assert resp["_index"] == "test-wiki"
242+
assert resp["result"] == "created"
243+
assert set(resp.keys()) == {
244+
"_id",
245+
"_index",
246+
"_primary_term",
247+
"_seq_no",
248+
"_shards",
249+
"_type",
250+
"_version",
251+
"result",
252+
}
253+
254+
resp = w.update(
255+
script="ctx._source.views += params.inc", inc=5, return_doc_meta=True
256+
)
257+
assert resp["_index"] == "test-wiki"
258+
assert resp["result"] == "updated"
259+
assert set(resp.keys()) == {
260+
"_id",
261+
"_index",
262+
"_primary_term",
263+
"_seq_no",
264+
"_shards",
265+
"_type",
266+
"_version",
267+
"result",
268+
}
269+
270+
237271
def test_init(write_client):
238272
Repository.init(index="test-git")
239273

0 commit comments

Comments
 (0)