Skip to content

Commit

Permalink
Add return_doc_meta option to Document.save() and .update()
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmlarson committed Dec 4, 2020
1 parent e7f8a90 commit fac8787
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
19 changes: 16 additions & 3 deletions elasticsearch_dsl/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ def update(
script_id=None,
scripted_upsert=False,
upsert=None,
return_doc_meta=False,
**fields
):
"""
Expand All @@ -356,6 +357,8 @@ def update(
:arg doc_as_upsert: Instead of sending a partial doc plus an upsert
doc, setting doc_as_upsert to true will use the contents of doc as
the upsert value
:arg return_doc_meta: set to ``True`` to return all metadata from the
index API call instead of only the operation result
:return operation result noop/updated
"""
Expand Down Expand Up @@ -415,9 +418,17 @@ def update(
if "_" + k in meta:
setattr(self.meta, k, meta["_" + k])

return meta["result"]
return meta if return_doc_meta else meta["result"]

def save(self, using=None, index=None, validate=True, skip_empty=True, **kwargs):
def save(
self,
using=None,
index=None,
validate=True,
skip_empty=True,
return_doc_meta=False,
**kwargs
):
"""
Save the document into elasticsearch. If the document doesn't exist it
is created, it is overwritten otherwise. Returns ``True`` if this
Expand All @@ -430,6 +441,8 @@ def save(self, using=None, index=None, validate=True, skip_empty=True, **kwargs)
:arg skip_empty: if set to ``False`` will cause empty values (``None``,
``[]``, ``{}``) to be left on the document. Those values will be
stripped out otherwise as they make no difference in elasticsearch.
:arg return_doc_meta: set to ``True`` to return all metadata from the
update API call instead of only the operation result
Any additional keyword arguments will be passed to
``Elasticsearch.index`` unchanged.
Expand Down Expand Up @@ -459,4 +472,4 @@ def save(self, using=None, index=None, validate=True, skip_empty=True, **kwargs)
if "_" + k in meta:
setattr(self.meta, k, meta["_" + k])

return meta["result"]
return meta if return_doc_meta else meta["result"]
34 changes: 34 additions & 0 deletions tests/test_integration/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,40 @@ def test_update_script(write_client):
assert w.views == 47


def test_save_and_update_return_doc_meta(write_client):
Wiki.init()
w = Wiki(owner=User(name="Honza Kral"), _id="elasticsearch-py", views=42)
resp = w.save(return_doc_meta=True)
assert resp["_index"] == "test-wiki"
assert resp["result"] == "created"
assert set(resp.keys()) == {
"_id",
"_index",
"_primary_term",
"_seq_no",
"_shards",
"_type",
"_version",
"result",
}

resp = w.update(
script="ctx._source.views += params.inc", inc=5, return_doc_meta=True
)
assert resp["_index"] == "test-wiki"
assert resp["result"] == "updated"
assert set(resp.keys()) == {
"_id",
"_index",
"_primary_term",
"_seq_no",
"_shards",
"_type",
"_version",
"result",
}


def test_init(write_client):
Repository.init(index="test-git")

Expand Down

0 comments on commit fac8787

Please sign in to comment.