【问题标题】:how to update nested field values in elasticsearch using java api如何使用java api更新elasticsearch中的嵌套字段值
【发布时间】:2015-10-15 07:50:00
【问题描述】:

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

这里是文档的映射

 {
  "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.uuid) 将文档的 Inbox.messageList.Delete.deleteStatusInbox.messageList.Delete.deleteReason 的值从 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
}
   }
          ]
          }
}

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

【问题讨论】:

标签: java scala elasticsearch updates scala-2.11


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    专门针对 JAVA API,也可以使用 PeteyPabPro 的 answer 更新嵌套文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-16
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      相关资源
      最近更新 更多