diff --git a/elasticsearch_dsl/document.py b/elasticsearch_dsl/document.py index f77667dfd..6a3aa2b49 100644 --- a/elasticsearch_dsl/document.py +++ b/elasticsearch_dsl/document.py @@ -331,6 +331,7 @@ def update( script_id=None, scripted_upsert=False, upsert=None, + return_doc_meta=False, **fields ): """ @@ -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 """ @@ -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 @@ -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. @@ -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"] diff --git a/tests/test_integration/test_document.py b/tests/test_integration/test_document.py index a25279abe..83932f7e5 100644 --- a/tests/test_integration/test_document.py +++ b/tests/test_integration/test_document.py @@ -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")