# Update existing record shuffles the data

**URL:** https://discuss.elastic.co/t/update-existing-record-shuffles-the-data/326400
**Category:** Elasticsearch
**Created:** [February 24, 2023, 5:11am UTC](https://discuss.elastic.co/t/update-existing-record-shuffles-the-data/326400 "2023-02-24T05:11:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![srb.saurabhjain](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@srb.saurabhjain](https://discuss.elastic.co/u/srb.saurabhjain)
#### Post date: [February 24, 2023, 5:11am UTC](https://discuss.elastic.co/t/update-existing-record-shuffles-the-data/326400/1 "2023-02-24T05:11:24Z")

</div>

Hi team,

I am trying to update a field in the below data B.B1 but the result data is randomly shuffled when I fetch again.

Original

```auto
{
    "A": {
        "A1": "test"
    },
    "B": {
        "B1": "approved"
    },
    "C": {
        "C1": "5f25648c",
        "C2": {
            "firstName": "Test",
            "lastName": "Me"
        }
    },
    "D": "hello",
    "E": {
        "E1": "2017"
    }
}

```

query for update

```auto
POST /test/_update_by_query
{
  "script": {
    "source": "ctx._source.B.B1='rejected'"
  },
  "query": {
    "term": {
      "C.C1": "5f25648c"
    }
  }
}

```

Result data after update

```auto
{
    "D": "hello",
    "E": {
        "E1": "2017"
    },
    "B": {
        "B1": "rejected"
    },
    "C": {
        "C1": "5f25648c",
        "C2": {
            "firstName": "Test",
            "lastName": "Me"
        }
    },
     "A": {
        "A1": "test"
    }
}

```

How can I get the objects in same order as those were before ?  
Also what is the difference between `script.inline` and `script.source` in the update query both updating the record correctly.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [February 24, 2023, 6:14am UTC](https://discuss.elastic.co/t/update-existing-record-shuffles-the-data/326400/2 "2023-02-24T06:14:32Z")

</div>

Elasticsearch will always return exactly the source document that was indexed, but JSON serialisation does not provide any guarantee around which order the fields will written. If you retrieved the document and made the change client side you can control how it is serialised, but this is not possible when you perform a scripted update. When the field is changed by the script, the document is deserialised before the field is changed and/or added and it is then serialised again before being stored. At this point no ordering is guaranteed, so what you are seeing is as far as I know expected.

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [February 24, 2023, 6:17am UTC](https://discuss.elastic.co/t/update-existing-record-shuffles-the-data/326400/3 "2023-02-24T06:17:10Z")

</div>

Yup, Christian is 100% correct.

---

<div class="post-metadata">

### Author: ![srb.saurabhjain](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@srb.saurabhjain](https://discuss.elastic.co/u/srb.saurabhjain)
#### Post date: [February 26, 2023, 9:42pm UTC](https://discuss.elastic.co/t/update-existing-record-shuffles-the-data/326400/4 "2023-02-26T21:42:11Z")

</div>

Thanks, @Christian_Dahlqvist for the detailed response. I had one more query in question while doing the update I can use `script.inline` or `script.source` both resulting in the correct update. What is the actual difference between them? I tried to find out the difference but no luck.

Also, is there any other way of updating a particular field without using the script?

```auto
"script": {
    "source": "ctx._source.B.B1='rejected'"
  }

"script": {
    "inline": "ctx._source.B.B1='rejected'"
  }

```

---

<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: [March 26, 2023, 9:43pm UTC](https://discuss.elastic.co/t/update-existing-record-shuffles-the-data/326400/5 "2023-03-26T21:43:10Z")

</div>

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