0

i have a following document in elasticsearch

{
"uuid":"123",
"Email":"[email protected]",
"FirstName":"personFirstNmae",
"LastName":"personLastName",
"Inbox":{
"uuid":"1234",
"messageList":[
{
    "uuid":"321",
    "Subject":"subject1",
    "Body":"bodyText1",
    "ArtworkUuid":"101",
    "DateAndTime":"2015-10-15T10:59:12.096+05:00",
    "ReadStatusInt":0,
    "Delete":{
        "deleteStatus":0,
        "deleteReason":0
             }
},
{
    "uuid":"123",
    "Subject":"subject",
    "Body":"bodyText",
    "ArtworkUuid":"100",
    "DateAndTime":"2015-10-15T10:59:11.982+05:00",
    "ReadStatusInt":1,
    "Delete":{
        "deleteStatus":0,
        "deleteReason":0
          }
}
              ]
        }
}

and here is the mapping of the doc

 {
  "testdb" : {
    "mappings" : {
      "directUser" : {
        "properties" : {
          "Email" : {
            "type" : "string",
            "store" : true
          },
          "FirstName" : {
            "type" : "string",
            "store" : true
          },
          "Inbox" : {
            "type" : "nested",
            "include_in_parent" : true,
            "properties" : {
              "messageList" : {
                "type" : "nested",
                "include_in_parent" : true,
                "properties" : {
                  "ArtworkUuid" : {
                    "type" : "string",
                    "store" : true
                  },
                  "Body" : {
                    "type" : "string",
                    "store" : true
                  },
                  "DateAndTime" : {
                    "type" : "date",
                    "store" : true,
                    "format" : "dateOptionalTime"
                  },
                  "Delete" : {
                    "type" : "nested",
                    "include_in_parent" : true,
                    "properties" : {
                      "deleteReason" : {
                        "type" : "integer",
                        "store" : true
                      },
                      "deleteStatus" : {
                        "type" : "integer",
                        "store" : true
                      }
                    }
                  },
                  "ReadStatusInt" : {
                    "type" : "integer",
                    "store" : true
                  },
                  "Subject" : {
                    "type" : "string",
                    "store" : true
                  },
                  "uuid" : {
                    "type" : "string",
                    "store" : true
                  }
                }
              },
              "uuid" : {
                "type" : "string",
                "store" : true
              }
            }
          },

          "LastName" : {
            "type" : "string",
            "store" : true
          },
                    "uuid" : {
            "type" : "string",
            "store" : true
          }
        }
      }
    }
  }
}

Now i want to update the values of Inbox.messageList.Delete.deleteStatus and Inbox.messageList.Delete.deleteReason from 0 to 1 of the doc with uuid 321 (Inbox.messageList.uuid). i want to achieve something like this

{
"uuid":"123",
"Email":"[email protected]",
"FirstName":"personFirstNmae",
"LastName":"personLastName",
"Inbox":{
"uuid":"1234",
"messageList":[
{
    "uuid":"321",
    "Subject":"subject1",
    "Body":"bodyText1",
    "ArtworkUuid":"101",
    "DateAndTime":"2015-10-15T10:59:12.096+05:00",
    "ReadStatusInt":0,
    "Delete":{
        "deleteStatus":1,
        "deleteReason":1
             }
},
{
    "uuid":"123",
    "Subject":"subject",
    "Body":"bodyText",
    "ArtworkUuid":"100",
    "DateAndTime":"2015-10-15T10:59:11.982+05:00",
    "ReadStatusInt":1,
    "Delete":{
        "deleteStatus":0,
        "deleteReason":0
          }
}
              ]
        }
}

i am trying the following code to achieve my desired updated document

 var xb:XContentBuilder=XContentFactory.jsonBuilder().startObject()
                             .startObject("Inbox")
                            xb.startArray("messageList") 
                                xb.startObject();
                                    xb.startObject("Delete")
                                      xb.field("deleteStatus",1)
                                      xb.field("deleteReason",1)
                                    xb.endObject()
                                  xb.endObject();      

                              xb.endArray()
                           .endObject()
                          xb.endObject()

           val responseUpdate=client.prepareUpdate("testdb", "directUser", directUserObj.getUuid.toString())
         .setDoc(xb).execute().actionGet()

but from this code my doc becomes

{"uuid":"123",
"Email":"[email protected]",
"FirstName":"personFirstNmae",
"LastName":"personLastName",
,"Inbox":{
"uuid":"1234",
"messageList":[
   {
"Delete":{
"deleteStatus":1,
"deleteReason":1
}
   }
          ]
          }
}

and i do not want this, please help me how can i achieve my desired document , Iam using elasticsearch version 1.6

1

2 Answers 2

2

The best way I've found to update a single nested field is to use the elasticsearch update API that takes a (parameterised) script a la this answer. Last time I checked this kind of thing is only supported in groovy scripts, not lucene expression scripts (unfortunately). The reason your update produces the result it does is you are updating the whole nested object, not a specific nested item. Groovy script update will allow you to select and update the nested object with the specified ID.

Sign up to request clarification or add additional context in comments.

Comments

0

You can also have a look at the nested object that I have currently updated and how I used the UpdateRequest class in Java here.

Specifically for the JAVA API, it is also possible to update a nested document with this answer of PeteyPabPro.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.