Update existing record shuffles the data

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

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

query for update

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

Result data after update

{
    "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.

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.

3 Likes

Yup, Christian is 100% correct.

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?

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

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

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