ElasticSearch:在现有的对象Field数组中追加对象

时间:2016-07-29 12:08:52

标签: java elasticsearch spring-data-elasticsearch elasticsearch-api

我的字段映射:

"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$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)
请帮忙。

1 个答案:

答案 0 :(得分:2)

如果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

的脚本参数