Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

elasticsearch中对nested类型数据进行脚本合并出错

现在有如下的mapping结构

"mappings":{
    "properties":{
        "userId":{
            "type":"int"
        },
        "nickname":{
            "type":"keyword"
        },
        "huanbi":{
            "type":"nested",
            "properties":{
                "id":{
                    "type":"int"
                },
                "sumScore":{
                    "type":"int"
                },
                "evaluation":{
                    "type":"keyword"
                },
                "status":{
                    "type":"int"
                },
                "createdAt":{
                    "type":"date",
                    "format":"yyyy-MM-dd HH:mm:ss"
                }
            }
        }
    }
}

这里的huanbi为nested类型,我希望是将所有用户的huanbi都集中到一起再进行筛选。

{
  "query":{
    "exists": {
      "field":"huanbi"
    }
  },
  "aggs":{
    "allHuanbi":{
      "scripted_metric": {
        "init_script": "state.huanbi=new ArrayList();",
        "map_script": "state.huanbi.addAll(doc.huanbi);",
        "combine_script": "ArrayList huanbi_combine = new ArrayList();for(f in state.huanbi){huanbi_combine.add(f)} return huanbi_combine;", 
        "reduce_script": "ArrayList huanbi_reduce = new ArrayList();for(huanbi in states){huanbi_reduce.add(huanbi)} return huanbi_reduce;"
      }
    }
  }
}

在使用上述代码进行合并之后出现了如下错误

"failed_shards" : [
      {
        "shard" : 0,
        "index" : "dgcrm-allinone",
        "node" : "tt8lTblASDG_PxS4DdnpuQ",
        "reason" : {
          "type" : "script_exception",
          "reason" : "runtime error",
          "script_stack" : [
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:90)",
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
            "state.huanbi.addAll(doc.huanbi);",
            "                       ^---- HERE"
          ],
          "script" : "state.huanbi.addAll(doc.huanbi);",
          "lang" : "painless",
          "position" : {
            "offset" : 23,
            "start" : 0,
            "end" : 32
          },
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "No field found for [huanbi] in mapping with types []"
          }
        }
      }
    ]

请问各位大神这个错误是怎么导致的,因为同样的合并代码在纯数组上是可以正确运行的,是不是因为nested类型的特殊性导致代码失效?如果想要实现合并的功能需要进行怎么样的修改,还是说这个功能目前实现不了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

应该是mapping的阶段中的整型误写为了int导致的


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...