Elasticsearch:Scripted Metric Aggregation返回奇怪的分组

时间:2018-06-13 12:01:40

标签: elasticsearch lucene kibana search-engine elasticsearch-painless

我得到了意想不到的结果,我有点迷失了。

添加了这些文档

POST es_test/_doc/_bulk?pretty
{ "index": {}}
{ "firstname": "John", "lastname": "Doe", "age": 22, "birthdate": "1980-01-20T12:30:00Z" }
{ "index": {}}
{ "firstname": "May", "lastname": "Greenwood", "age": 19, "birthdate": "1980-01-20T12:30:00Z" }
{ "index": {}}
{ "firstname": "Marry", "lastname": "Hilake", "age": 32, "birthdate": "1970-01-20T12:30:00Z" }
{ "index": {}}
{ "firstname": "Mister", "lastname": "X", "age": 20, "birthdate": "1990-11-23T12:30:00Z" }

当我这样请求时,一切都很好

GET es_test/_doc/_search

当我添加无痛脚本时出现问题

GET es_test/_doc/_search    
{
  "size": 0,
  "aggs": {
    "user": {
      "scripted_metric": {
        "init_script" : "params._agg.transactions = []",
        "map_script" : "params._agg.transactions.add(params._source)"
      }
    }
  }
}

它的输出如下所示:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "user": {
      "value": [
        {
          "transactions": [
            {
              "firstname": "John",
              "birthdate": "1980-01-20T12:30:00Z",
              "age": 22,
              "lastname": "Doe"
            },
            {
              "firstname": "John",
              "birthdate": "1980-01-20T12:30:00Z",
              "age": 22,
              "lastname": "Doe"
            }
          ]
        },
        {
          "transactions": []
        },
        {
          "transactions": [
            {
              "firstname": "May",
              "birthdate": "1980-01-20T12:30:00Z",
              "age": 19,
              "lastname": "Greenwood"
            }
          ]
        },
        {
          "transactions": []
        },
        {
          "transactions": [
            {
              "firstname": "Marry",
              "birthdate": "1970-01-20T12:30:00Z",
              "age": 32,
              "lastname": "Hilake"
            }
          ]
        }
      ]
    }
  }
}

第一个transactions数组包括两次约翰,第二个是空的,然后是五月,再次空,最后结婚。 我不知道为什么它的分组如此奇怪

所需的输出将是包含所有用户的一个数组(John,May,Marry,Mister)。

感谢您的帮助,谢谢! 游戏

1 个答案:

答案 0 :(得分:0)

您必须使用reduce_script。这工作

GET es_test/_doc/_search
{
  "size": 0,
  "aggs": {
    "user": {
      "scripted_metric": {
        "init_script": "params._agg.transactions = []",
        "map_script": "params._agg.transactions.add(params._source)",
        "reduce_script": """
                ArrayList transactions = []; 
                for (a in params._aggs) { 
                  transactions.addAll(a.transactions) 
                } 
                return transactions
"""
      }
    }
  }
}

结果

{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "user": {
      "value": [
        {
          "firstname": "John",
          "birthdate": "1980-01-20T12:30:00Z",
          "age": 22,
          "lastname": "Doe"
        },
        {
          "firstname": "Marry",
          "birthdate": "1970-01-20T12:30:00Z",
          "age": 32,
          "lastname": "Hilake"
        },
        {
          "firstname": "Mister",
          "birthdate": "1990-11-23T12:30:00Z",
          "age": 20,
          "lastname": "X"
        },
        {
          "firstname": "Mister",
          "birthdate": "1990-11-23T12:30:00Z",
          "age": 20,
          "lastname": "X"
        }
      ]
    }
  }
}

更新,下面的工作,这是相同的聚合,但它只使用部分源,这是非常奇怪的。

GET es_test/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "user": {
      "scripted_metric": {
        "params": {
          "_agg": {}
        },
        "init_script": "params._agg.transactions = []",
        "map_script": "params._agg.transactions.add(params._source.firstname + ' ' + params._source.lastname)",
        "reduce_script": """
                ArrayList transactions = []; 
                for (a in params._aggs) { 
                  transactions.addAll(a.transactions) 
                } 
                return transactions
"""
      }
    }
  }
}

结果

{
  "took": 18,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "user": {
      "value": [
        "John Doe",
        "May Greenwood",
        "Marry Hilake",
        "Mister X"
      ]
    }
  }
}