脚本化指标聚合返回错误结果

时间:2018-07-23 14:55:43

标签: elasticsearch elasticsearch-painless

我一直在使用Scripted Metric Aggregation的数据来玩here。 这是我的代码:

 GET hockey/_search?size=20
{
    "query" : {
        "bool": {
          "should": [
            {"match": {
              "first": "joe"
            }},
            {
              "match": {
                "first": "sam"
              }
            },
            {
              "match": {
                "first": "tj"
              }
            }
          ]
        }
    },
    "aggs": {
        "profit": {
            "scripted_metric": {
                "init_script" : "params._agg.docs = []",
                "map_script" : "params._agg.docs.add(doc)", 
                "combine_script" : "long age = 0; for (d in params._agg.docs) { age += d.born.date.year } return age",
                "reduce_script" : "long age = 0; for (a in params._aggs) { age += a } return age"
            }
        }
    }
}

(我确实知道还有其他方法可以实现相同的目标,但是我正在尝试并尝试学习无痛的工作方式) 如您所见,它应该总结“ born ”字段中的年份。但是,您可能会发现结果总和是错误的。 结果如下:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.9808292,
    "hits": [
      {
        "_index": "hockey",
        "_type": "player",
        "_id": "5",
        "_score": 0.9808292,
        "_source": {
          "first": "sam",
          "last": "bennett",
          "goals": [
            5,
            0,
            0
          ],
          "assists": [
            8,
            1,
            0
          ],
          "gp": [
            26,
            1,
            0
          ],
          "born": "1996/06/20"
        }
      },
      {
        "_index": "hockey",
        "_type": "player",
        "_id": "8",
        "_score": 0.9808292,
        "_source": {
          "first": "tj",
          "last": "brodie",
          "goals": [
            2,
            14,
            7
          ],
          "assists": [
            8,
            42,
            30
          ],
          "gp": [
            26,
            82,
            82
          ],
          "born": "1990/06/07"
        }
      },
      {
        "_index": "hockey",
        "_type": "player",
        "_id": "11",
        "_score": 0.6931472,
        "_source": {
          "first": "joe",
          "last": "colborne",
          "goals": [
            3,
            18,
            13
          ],
          "assists": [
            6,
            20,
            24
          ],
          "gp": [
            26,
            67,
            82
          ],
          "born": "1990/01/30"
        }
      }
    ]
  },
  "aggregations": {
    "profit": {
      "value": 5970
    }
  }
}

我一直在检查2次点击后会发生什么,然后总和就可以了。当我查询 match_all 总和也低于正确的数字。有困难吗?

编辑: 更奇怪的是,当我将代码更改为此(更改了代码的第3行和第4行,因此我没有将整个文档添加到参数中,而仅将 year 添加到参数中)时:

    "scripted_metric": {
        "init_script" : "params._agg.docs = []",
        "map_script" : "params._agg.docs.add(doc.born.date.year)", 
        "combine_script" : "long age = 0; for (d in params._agg.docs) { age += d } return age",
        "reduce_script" : "long age = 0; for (a in params._aggs) { age += a } return age"
    }

结果正确。

0 个答案:

没有答案