scripted_metric 聚合:map_script 和 combine_script 未调用

scripted_metric aggregation: map_script and combine_script not invoked

我刚刚开始使用 ElasticSearch (v1.5) scripted_metric 聚合,在尝试 example aggregation from the documentation.

时已经迷迷糊糊了

在我发现如何从脚本中 之后,发现只有 init_scriptreduce_script 被调用,而 map_scriptcombine_script 被省略,即使在将脚本减少到纯粹的日志记录之后也是如此。

[...]
  "aggs": {
    "test": {
      "scripted_metric": {
            "init_script" : "import  org.elasticsearch.common.logging.*; ESLogger logger=ESLoggerFactory.getLogger('myscript');logger.error('1')",
            "map_script" : "import  org.elasticsearch.common.logging.*; ESLogger logger=ESLoggerFactory.getLogger('myscript');logger.error('2')", 
            "combine_script" : "import  org.elasticsearch.common.logging.*; ESLogger logger=ESLoggerFactory.getLogger('myscript');logger.error('3')",
            "reduce_script" : "import  org.elasticsearch.common.logging.*; ESLogger logger=ESLoggerFactory.getLogger('myscript');logger.error('4')"
      }
    }
  }
[...]

日志

[2015-08-04 08:31:31,841][ERROR][myscript                 ] 1
[2015-08-04 08:31:31,841][ERROR][myscript                 ] 1
[2015-08-04 08:31:31,843][ERROR][myscript                 ] 1
[2015-08-04 08:31:31,844][ERROR][myscript                 ] 1
[2015-08-04 08:31:31,845][ERROR][myscript                 ] 1
[2015-08-04 08:31:32,020][ERROR][myscript                 ] 4

之前,我从文档示例的略微修改版本开始:

[...]
  "aggs": {
    "test": {
      "scripted_metric": {
         "init_script" : "_agg['transactions'] = []",
            "map_script" : "_agg.transactions.add(2)", 
            "combine_script" : "profit = 0; for (t in _agg.transactions) { profit += t }; return profit",
            "reduce_script" : "profit = 0; for (a in _aggs) { profit += a }; return profit"
      }
    }
  }
[...]

导致的 ReduceSearchPhaseException 失败
[Ambiguous method overloading for method java.lang.Integer#plus.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
    [class java.lang.Character]
    [class java.lang.String]
    [class java.lang.Number]]

发生这种情况是因为在 reduce_script_aggs 数组由五个 null 组成,如果 map_scriptcombine_script 不 运行。

但他们为什么不 运行?任何深入挖掘的想法都会受到赞赏。

如果应用的过滤器过于严格以至于找不到任何文档,就会发生这种情况。在我的例子中,一个 bool 过滤器隐藏在我的 Postman 的折叠上方,我花了一些时间才意识到这一点 - 抱歉,多么愚蠢的错误:-/

显然,mapcombine 在空搜索结果中没有任何内容。