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



我有一个下面的文档

{
  "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();

,然后更新值没有任何错误,然后我的doc变成

{
  "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,我做错了什么

你的脚本应该是不同的:

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)

相关内容

  • 没有找到相关文章

最新更新