ElasticSearch:在现有对象字段数组中追加对象

ElasticSearch : Appending object in existing array of object Field

我的字段映射:

"Pincode": {

    "analyzer": "standard",
    "type": "string"

},
    "Residence_address": {

        "include_in_parent": true,


           "type": "nested",
            "properties": {
                "address": {
                    "analyzer": "standard",
                    "type": "string"
                },
                "Address_type": {
                    "analyzer": "standard",
                    "type": "string"
                },
                "Pincode": {
                    "analyzer": "standard",
                    "type": "string"
                }
            }

        }

我有数据:

    "Residence_address": [

        {
            "address": "safadfsdf",
            "Address_type": "Owned",
            "Pincode": "50005756768674"
        }
        ,
        {
            "address": "Collage of lkj",
            "Address_type": "Rented",
            "Pincode": "419005"
        }

    ],
"Pincode": [

    "11" ,"12"

]

当我输入密码时:

  "Pincode": ["15"] 

它工作正常。 对于普通数组字段(不是对象数组),附加工作正常。

但是当我想插入对象时:

 "Residence_address": [
                    {
                     "address": "abbbbbb",
                     "Address_type": "bcccccc",
                     "Pincode": "67999999"
                  }
               ]

当我使用此代码插入它时:

      updateRequest.script("ctx._source.Residence_address +=  ADD");
          JSONArray Address = (JSONArray) temp.get("Residence_address");              
          i = 0;
          while(i < Address.size()){
                  System.out.println(Address.get(i).toString());
                  updateRequest.addScriptParam("ADD", Address.get(i++).toString());
               }

它给我错误:

org.elasticsearch.index.mapper.MapperParsingException: object mapping [Residence_address] trying to serialize a value with no field associated with it, current value [{"address":"abbbbbb","Address_type":"bcccccc","Pincode":"67999999"}]
    at org.elasticsearch.index.mapper.object.ObjectMapper.serializeValue(ObjectMapper.java:702)
    at org.elasticsearch.index.mapper.object.ObjectMapper.parse(ObjectMapper.java:497)
    at org.elasticsearch.index.mapper.object.ObjectMapper.serializeValue(ObjectMapper.java:706)
    at org.elasticsearch.index.mapper.object.ObjectMapper.serializeNonDynamicArray(ObjectMapper.java:695)
    at org.elasticsearch.index.mapper.object.ObjectMapper.serializeArray(ObjectMapper.java:604)
    at org.elasticsearch.index.mapper.object.ObjectMapper.parse(ObjectMapper.java:489)
    at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:544)
    at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:493)
    at org.elasticsearch.index.shard.IndexShard.prepareIndex(IndexShard.java:493)
    at org.elasticsearch.action.index.TransportIndexAction.shardOperationOnPrimary(TransportIndexAction.java:192)
    at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$PrimaryPhase.performOnPrimary(TransportShardReplicationOperationAction.java:574)
    at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$PrimaryPhase.routeRequestOrPerformLocally(TransportShardReplicationOperationAction.java:444)
    at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$PrimaryPhase.doRun(TransportShardReplicationOperationAction.java:370)
    at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:36)
    at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction.doExecute(TransportShardReplicationOperationAction.java:112)
    at org.elasticsearch.action.index.TransportIndexAction.innerExecute(TransportIndexAction.java:136)
    at org.elasticsearch.action.index.TransportIndexAction.doExecute(TransportIndexAction.java:114)
    at org.elasticsearch.action.index.TransportIndexAction.doExecute(TransportIndexAction.java:63)
    at org.elasticsearch.action.support.TransportAction.execute(TransportAction.java:75)
    at org.elasticsearch.action.update.TransportUpdateAction.shardOperation(TransportUpdateAction.java:217)
    at org.elasticsearch.action.update.TransportUpdateAction.shardOperation(TransportUpdateAction.java:170)
    at org.elasticsearch.action.support.single.instance.TransportInstanceSingleOperationAction$AsyncSingleAction.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)

请帮忙。

如果你在Address中有多个地址,你添加很多相同参数名的参数ADD,这样是行不通的。

试试这个:

      String newAddrs = "";
      JSONArray Address = (JSONArray) temp.get("Residence_address");              
      int i = 0;
      while(i < Address.size()){
              newAddrs += "ADD" + i + ( i < Address.size() - 1 ? "," : "");
              updateRequest.addScriptParam("ADD"+i, Address.get(i++));
      }
      updateRequest.script("ctx._source.Residence_address += ["+newAddrs+"]");

因此,如果您有 3 个地址,您将得到一个如下所示的脚本:

ctx._source.Residence_address += [ ADD0, ADD1, ADD2 ]

您将拥有三个名为 ADD0ADD1ADD2

的脚本参数