如何使用java api更新elasticsearch中的嵌套字段值

时间:2015-10-15 07:50:01

标签: java scala elasticsearch updates scala-2.11

我在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
          }
}
              ]
        }
}

这是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
          }
        }
      }
    }
  }
}

现在,我想使用uuid 321(Inbox.messageList.Delete.deleteStatus)将Inbox.messageList.Delete.deleteReasonInbox.messageList.uuid的值从0更新为1。 我想实现这样的目标

{
"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
          }
}
              ]
        }
}

我正在尝试以下代码来实现我想要的更新文档

 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()

但是从这段代码我的文档变成了

{"uuid":"123",
"Email":"mail@example.com",
"FirstName":"personFirstNmae",
"LastName":"personLastName",
,"Inbox":{
"uuid":"1234",
"messageList":[
   {
"Delete":{
"deleteStatus":1,
"deleteReason":1
}
   }
          ]
          }
}

我不想要这个,请帮助我如何实现我想要的文档,Iam使用elasticsearch 1.6版

2 个答案:

答案 0 :(得分:2)

我发现更新单个嵌套字段的最佳方法是使用带有(参数化)脚本la this answer的elasticsearch update API。上次我检查这种事情只支持groovy脚本,而不是lucene表达脚本(不幸的是)。更新产生结果的原因是您正在更新整个嵌套对象,而不是特定的嵌套项。 Groovy脚本更新将允许您选择和更新具有指定ID的嵌套对象。

答案 1 :(得分:0)

您还可以查看我当前更新的嵌套对象以及我如何在Java here中使用UpdateRequest类。

特别是对于JAVA API,也可以使用此answer PeteyPabPro更新嵌套文档。