简体   繁体   中英

how to update nested field values in elasticsearch using java api

i have a following document in elasticsearch

{
"uuid":"123",
"Email":"mail@example.com",
"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":"mail@example.com",
"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":"mail@example.com",
"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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM