如何在elasticsearch中更新数组内的嵌套文档

时间:2015-10-15 13:57:40

标签: java scala elasticsearch updates scala-2.11

我有以下文件

{
  "uuid": "123",
  "FirstName": "personFirstNmae",
  "LastName": "personLastName",
  "Inbox": {
    "uuid": "121",
    "messageList": [
      {
        "uuid": "321",
        "Subject": "subject1",
        "Delete": {
          "deleteStatus": 0,
          "deleteReason": 0
        }
      },
      {
        "uuid": "123",
        "Subject": "subject",
        "Delete": {
          "deleteStatus": 0,
          "deleteReason": 0
        }
      }
    ]
  }
}

这是映射

{
  "arteciatedb" : {
    "mappings" : {
      "directUser" : {
        "properties" : {
          "FirstName" : {
            "type" : "string",
            "store" : true
          },
          "Inbox" : {
            "type" : "nested",
            "include_in_parent" : true,
            "properties" : {
              "messageList" : {
                "type" : "nested",
                "include_in_parent" : true,
                "properties" : {
                  "Delete" : {
                    "type" : "nested",
                    "include_in_parent" : true,
                    "properties" : {
                      "deleteReason" : {
                        "type" : "integer",
                        "store" : true
                      },
                      "deleteStatus" : {
                        "type" : "integer",
                        "store" : true
                      }
                    }
                  },
                  "Subject" : {
                    "type" : "string",
                    "store" : true
                  },
                  "uuid" : {
                    "type" : "string",
                    "store" : true
                  }
                }
              },
              "uuid" : {
                "type" : "string",
                "store" : true
              }
            }
          },
          "Inbox.uuid" : {
            "type" : "string"
          },
          "LastName" : {
            "type" : "string",
            "store" : true
          },

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

现在我想将deleteStatus的值从0更新为1此字段位于数组嵌套文档中此字段的完整路径为

Inbox.messageList.Delete.deleteStatus

我想这样做

     var params:java.util.Map[String,Object] = Maps.newHashMap();
     params.put("updateMap","1")

val response = client.prepareUpdate("testdb", "directUser", directUserObj.getUuid)
    .setScript("ctx._source.Inbox.messageList.Delete.deleteStatus = updateMap",ScriptService.ScriptType.INLINE)
    .setScriptParams(params)
    .execute().actionGet();

但它会出现以下错误

org.elasticsearch.ElasticsearchIllegalArgumentException: failed to execute script
    at org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:202)
    at org.elasticsearch.action.update.TransportUpdateAction.shardOperation(TransportUpdateAction.java:176)
    at org.elasticsearch.action.update.TransportUpdateAction.shardOperation(TransportUpdateAction.java:170)
    at org.elasticsearch.action.support.single.instance.TransportInstanceSingleOperationAction$AsyncSingleAction$1.run(TransportInstanceSingleOperationAction.java:187)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.elasticsearch.script.groovy.GroovyScriptExecutionException: IllegalArgumentException[argument type mismatch]
    at org.elasticsearch.script.groovy.GroovyScriptEngineService$GroovyScript.run(GroovyScriptEngineService.java:278)
    at org.elasticsearch.action.update.UpdateHelper.prepare(UpdateHelper.java:198)
    ... 6 more

我已添加此行

script.engine.groovy.inline.update: on

在elasticsearch yml文件中 当我尝试这样做时

params.put("updateMap","12345")




     val response = client.prepareUpdate("testdb", "directUser", directUserObj.getUuid)
    .setScript("ctx._source.Inbox.uuid = updateMap",ScriptService.ScriptType.INLINE)
    .setScriptParams(params)
    .execute().actionGet();

然后更新值没有任何错误,然后我的文档变为

{
  "uuid": "123",
  "FirstName": "personFirstNmae",
  "LastName": "personLastName",
  "Inbox": {
    "uuid": "12345",
    "messageList": [
      {
        "uuid": "321",
        "Subject": "subject1",
        "Delete": {
          "deleteStatus": 0,
          "deleteReason": 0
        }
      },
      {
        "uuid": "123",
        "Subject": "subject",
        "Delete": {
          "deleteStatus": 0,
          "deleteReason": 0
        }
      }
    ]
  }
}

请指导我如何将deleteStatus字段值从0更新为1以及我做错了什么

1 个答案:

答案 0 :(得分:0)

您的脚本应该有所不同:

for(i in ctx._source.Inbox.messageList){i.Delete.deleteStatus=1}

在您的代码中,可能是这样的:

.setScript("for(i in ctx._source.Inbox.messageList){i.Delete.deleteStatus=updateMap}",ScriptService.ScriptType.INLINE)