# Document is not updated although the response is successful

**URL:** <https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483>\
**Category:** Elasticsearch\
**Created:** [July 27, 2016, 8:44am UTC](https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483 "2016-07-27T08:44:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)\
**Post date:** [July 27, 2016, 8:44am UTC](https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483/1 "2016-07-27T08:44:04Z")

</div>

Hey,

I am having a weird scenario, in which sometimes Elasticsearch claims to update a document successfully but it actually didn't do any thing.

So this is my code:

```
  override def addTerms(id: String, terms: Set[String])) = {
    val addTermsScript =
      s"""if (ctx._source.names == null)
          | ctx._source.names=newItems
          |else
          | ctx._source.names<< newItems;
          |ctx._source.names = ctx._source.names.flatten().unique();""".stripMargin
    client.execute(update id id in indexName / documentType retryOnConflict RETRIES script {
      script(addTermsScript).params(Map("newItems" -> terms.toArray))
    }).map(res => {
      Logger.info(s"Add terms: $terms response on id: $id - Shared info: ${res.getShardInfo}")
    })
  }

```

The response was :  
Add terms: Set("term1") response on id: some\_id - Shared info: "\_shards"{"total":2,"successful":1,"failed":0}

But when looking on the document, the field was not updated. This code is working, but once in a while I get this false update.

Am I looking on the correct portion of the response?  
Any idea?

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [July 27, 2016, 12:40pm UTC](https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483/2 "2016-07-27T12:40:05Z")

</div>

Hi @tpraizler,

I kind of get your Scala code but let's take Scala out of the question here and take step back. Here is a complete example that you can run in Sense:

```auto
DELETE /update-test

PUT /update-test/type1/1
{
    "items" : ["foo"]
}

```

If we retrieve the document now with `GET /update-test/type1/1` the array will contain just "foo". Notice that the `version` field says "1":

```auto
{
   "_index": "update-test",
   "_type": "type1",
   "_id": "1",
   "_version": 1,
   "forced_refresh": false,
   "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
   },
   "created": true
}

```

Now we do an update (script slightly modified from your version):

```auto
POST /update-test/type1/1/_update
{
   "script": {
      "inline": "ctx._source.items = (ctx._source.items == null) ? newItems : ctx._source.items + newItems",
      "lang": "groovy",
      "params": {
         "newItems": [
            "bar",
            "baz"
         ]
      }
   }
}

```

We get as response:

```auto
{
   "_index": "update-test",
   "_type": "type1",
   "_id": "1",
   "_version": 2,
   "forced_refresh": false,
   "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
   }
}

```

> Am I looking on the correct portion of the response?

The important bit in the response is the `_version` field which shows that the document's version has been updated.

Daniel

---

<div class="post-metadata">

**Author:** ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)\
**Post date:** [July 27, 2016, 1:32pm UTC](https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483/3 "2016-07-27T13:32:14Z")

</div>

Thanks @danielmitterdorfer! Very helpful!

In my app, at the point where the above method is invoked, I don't have the version of the current document in elasticsearch.  
I don't want to read the document before update (performance penalty) \ add version to this class coupling it to elasticsaerch.

Do you have any other suggestion on how to handle this?  
Is it possible for an update request to do nothing, and return the current version of the document?

Thanks!

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [July 27, 2016, 1:46pm UTC](https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483/4 "2016-07-27T13:46:40Z")

</div>

Hi @tpraizler,

> In my app, at the point where the above method is invoked, I don't have the version of the current document in elasticsearch.

I just showed this for demonstration purposes. Actually, the update request should just be fire-and-forget. The only thing that I'd check is that "failed" is 0. I am not sure how you determined that some documents were not updated. Do you have a very long refresh interval maybe? (you can also [force a refresh on update](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/docs-update.html#_parameters_3) with a parameter but this will have performance implications so I'd just do this for testing!).

> Is it possible for an update request to do nothing, and return the current version of the document?

Yes, this is possible. This will return the current document as is:

```auto
POST /update-test/type1/1/_update
{
    "doc": { }
}

```

(but you can achieve the same by just retrieving the document ;)).

You can even avoid the update in a scripted update, by specifying `ctx.op = \"none\"`(see [docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_scripted_updates)).

Daniel

---

<div class="post-metadata">

**Author:** ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)\
**Post date:** [July 27, 2016, 1:57pm UTC](https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483/5 "2016-07-27T13:57:56Z")

</div>

Thanks.

Amm, in my case I update a document (with the method in the first post), and get failed : 0.  
But the document was not updated.

This is my problem, the update does not fail, but does not update anything.  
It happens once in a while, not every update. sounds like a bug?

anything else beside the failed property that might indicate if the update was successful?

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [July 27, 2016, 2:04pm UTC](https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483/6 "2016-07-27T14:04:03Z")

</div>

Hi @tpraizler,

> This is my problem, the update does not fail, but does not update anything.

How do you verify that? The updated document will not appear in search results until the next refresh (that's why I've asked about your refresh interval).

> anything else beside the failed property that might indicate if the update was successful?

I'd log the document id and the version that is returned in every response if you really suspect a problem. This should help you to understand whether the document has been updated (I understand that you cannot use this as a kind of post-condition check in your application but it should at least get you started in debugging the problem I hope).

Daniel

---

<div class="post-metadata">

**Author:** ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)\
**Post date:** [July 27, 2016, 2:15pm UTC](https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483/7 "2016-07-27T14:15:56Z")

</div>

Thanks @danielmitterdorfer.

My refresh interval is 1 second (the default).  
And I verified it by querying this document manually using sense (The best verification..🙂 )

So I will need to log the version when I get the document, and also when update that document? meaning the 2 operations should be logged?

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [July 27, 2016, 2:24pm UTC](https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483/8 "2016-07-27T14:24:38Z")

</div>

Hi @tpraizler,

> My refresh interval is 1 second (the default).  
> And I verified it by querying this document manually using sense (The best verification.. )

Ok, that rules out a lot of problems. 🙂

> So I will need to log the version when I get the document, and also when update that document?

Provided that this is the only code path that updates these documents you just need to log after the response of the update. I imagine that your log file looks something like:

```auto
doc id [17] updated to version [3]
doc id [18] updated to version [2]
[...]
doc id [17] updated to version [3]

```

The expectation is that all lines are unique. If you see something like the last line in the example log then you know that something is odd and you need to dig deeper.

Daniel

---

<div class="post-metadata">

**Author:** ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)\
**Post date:** [July 27, 2016, 2:35pm UTC](https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483/9 "2016-07-27T14:35:24Z")

</div>

I see! Make sense!  
I will do that and update if I find anything.

Thanks a lot @danielmitterdorfer

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [July 27, 2016, 2:35pm UTC](https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483/10 "2016-07-27T14:35:46Z")

</div>

You're welcome. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:32pm UTC](https://discuss.elastic.co/t/document-is-not-updated-although-the-response-is-successful/56483/11 "2017-07-05T22:32:28Z")

</div>


