Updating Inner Object using Transport Client Java

Hello - I'm trying to use Transport Client (java) to perform CRUD operations on ES document. I have two cases where I want to override whole inner object (meaning I want the existing inner object in ES to be overwritten) and another case is just update the inner object (meaning if the fields already exists in the innner object then it should update field value, else new fields should be added to the inner object). Below is my sample document in that "tags" field is the inner object.

{
   "user" : "xyz",
    "tags" : {
                     "code" : "SF",
                     "subsidiary" : "SF_DA"
                  }
}

Case # 1 :

As mentioned above, if I want to completely override the existing tags field the document should look as below.

{
   "user" : "xyz",
    "tags" : {
                     "company" : "PRO",
                     "city" : "Omaha"
                  }
}

Case # 2 :

As mentioned above, if I want to update existing fields in the tags field or add any new fields, the document should look as below

  {
       "user" : "xyz",
        "tags" : {
                         "code" : "ADP",
                         "subsidiary" : "SF_DA",
                         "city" : "Omaha"
                      }
    }

If I use prepareUpdate() api then I'm able to achieve only Case # 2 but not Case #1. Is there any way I can achieve Case # 1 as well with partial update of the inner object. Any response is highly appreciated. Thanks!

Why you don't want to send the full object? Is it that big?

Hello David - thanks for your reply. My object is very small it has two fields one user field and another tags field (which is composition object in main object in java terms but in ES document tags is like inner object). I would like to update only tags (but as I explained above I have to achieve two cases). Pls do let me know if you need more info on this. Thanks!

Hello David - the reason why I don't want to send full object is I'll not have other info while updating meaning copy of the existing document. To update whole document I need to query it first and then do full update. Just checking if there is any better way to do, so that I can avoid the query. Thanks!

Behind the scene, update is:

  • GET the doc by its id
  • apply changes to this doc
  • PUT the full doc again

The only reasons to use the update API IMO are:

  • reduce the network load (if you have very big documents - that's why I asked)
  • minimize the risk of version conflict if you are using this feature

But coming to your original question, I think that it's not related to the Java client.
Could you create a full script which helps to reproduce your problem?

Also in case you missed it, here are some docs about the update API: https://www.elastic.co/guide/en/elasticsearch/guide/master/partial-updates.html#partial-updates