How can I the get updated document in the response

Viewed 43
let elasticsearch = require('elasticsearch');

let client = new elasticsearch.Client({
     host: "*********************",
});

let temp = await client.update({
    index: container,
    id: 1,
    type: '_doc',
    body: {"name": "rks", "visible": true},
    doc_as_upsert: true,

})

Response of this query returns

"result": {
    "_index": "container",
    "_type": "_doc",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

How do I return the updated document in the response. Is there any way/method to achieve this goal. Please help me

1 Answers

The best you can do is set "_source": true. This way you'll get back the document source.

For e.g.

POST test/_update/1
{
  "doc":{
    "hello": "world",
    "hey": "there"
  },
  "_source": true,
  "doc_as_upsert": true
}

Sample response:

{
  "_index" : "test",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1,
  "get" : {
    "_seq_no" : 2,
    "_primary_term" : 1,
    "found" : true,
    "_source" : {
      "hello" : "world",
      "hey" : "there"
    }
  }
}
Related