# Getting version conflict even while updating sequentially using update\_by\_query

**URL:** https://discuss.elastic.co/t/getting-version-conflict-even-while-updating-sequentially-using-update-by-query/188657
**Category:** Elasticsearch
**Created:** [July 3, 2019, 9:00am UTC](https://discuss.elastic.co/t/getting-version-conflict-even-while-updating-sequentially-using-update-by-query/188657 "2019-07-03T09:00:51Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Prakash\_Sharma1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prakash_sharma1/32/49330_2.png) [@Prakash\_Sharma1](https://discuss.elastic.co/u/Prakash_Sharma1)
#### Post date: [July 3, 2019, 9:00am UTC](https://discuss.elastic.co/t/getting-version-conflict-even-while-updating-sequentially-using-update-by-query/188657/1 "2019-07-03T09:00:51Z")

</div>

I am trying to update a document's nested type field using painless script by \_update\_by\_query. The script is executing sequentially that means the next \_update\_by\_query is run only when the previous one has successfully returned. But I am still getting version conflict when the two queries try to update the nested field by painless script. Also there is no other process updating the elastic search.

Following is the first \_update\_by\_query which successfully return

```
{
  "index": [
    "user_39"
  ],
  "body": {
    "script": {
      "source": "boolean done = false;
for (int i = 0; i < ctx._source.user.length; ++i){
if(ctx._source.user[i].id == params.id){
ctx._source.user[i] = params.obj; 
done=true;
}
}
if(done==false){
ctx._source.user.add(params.obj)
}",
      "params": {
        "id": 7018947,
        "obj": {
          "id": 7018947,
          "user_id": 3750511,
          "name": "User_1",
          "created_at": "2019-07-03T06:32:02.000Z",
          "updated_at": "2019-07-03T06:32:02.000Z"
        }
      },
      "lang": "painless"
    },
    "query": {
      "terms": {
        "_id": [
          232323
        ]
      }
    }
  }
}

```

Following is the next \_update\_by\_query which throws version conflict. Note that both the queries are trying to update the same document and same nested field array but different element and are running one after other.

```
{
      "index": [
        "user_39"
      ],
      "body": {
        "script": {
          "source": "boolean done = false;
    for (int i = 0; i < ctx._source.user.length; ++i){
    if(ctx._source.user[i].id == params.id){
    ctx._source.user[i] = params.obj; 
    done=true;
    }
    }
    if(done==false){
    ctx._source.user.add(params.obj)
    }",
          "params": {
            "id": 7018949,
            "obj": {
              "id": 7018949,
              "user_id": 3750511,
              "name": "User_2",
              "created_at": "2019-07-03T06:32:02.000Z",
              "updated_at": "2019-07-03T06:32:02.000Z"
            }
          },
          "lang": "painless"
        },
        "query": {
          "terms": {
            "_id": [
              232323
            ]
          }
        }
      }
    }

```

Here is the error object thrown by second query

```
"body": {
            "took": 7,
            "timed_out": false,
            "total": 1,
            "updated": 0,
            "deleted": 0,
            "batches": 1,
            "version_conflicts": 1,
            "noops": 0,
            "retries": {
                "bulk": 0,
                "search": 0
            },
            "throttled_millis": 0,
            "requests_per_second": -1,
            "throttled_until_millis": 0,
            "failures": [
                {
                    "index": "user_39",
                    "type": "_doc",
                    "id": "232323",
                    "cause": {
                        "type": "version_conflict_engine_exception",
                        "reason": "[232323]: version conflict, required seqNo [1394834], primary term [1]. current document has seqNo [1394835] and primary term [1]",
                        "index_uuid": "SDOxthUJRw2h8vryIoZxqQ",
                        "shard": "0",
                        "index": "user_39"
                    },
                    "status": 409
                }
            ]
        },

```

I also saw the pattern that only nested field updates are throwing conflict. Other non nested field updates with painless and update\_by\_query are running without conflict one after other. Anyone have any idea?

---

<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 31, 2019, 9:02am UTC](https://discuss.elastic.co/t/getting-version-conflict-even-while-updating-sequentially-using-update-by-query/188657/2 "2019-07-31T09:02:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
