简体   繁体   中英

how to update nested document inside array in elasticsearch

I have a following document

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

and here is the mapping

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

Now I want to update the value of deleteStatus from 0 to 1 this field is inside array nested document full path of this field is

Inbox.messageList.Delete.deleteStatus

i am trying to do it like this

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

but it gives the following error

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

i have already added this line

script.engine.groovy.inline.update: on

in elasticsearch yml file also when i try to do it like this

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

then the value is updated without any error and then my doc becomes

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

please guide me how can i update deleteStatus fields value from 0 to 1 and what am i doing wrong

Your script should be different:

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

And in your code, probably like this:

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

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