4

我得到了一个意想不到的结果,我有点迷茫。

添加了这些文档

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。我不知道为什么它的分组如此奇怪

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

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

4

1 回答 1

1

为此,您必须使用 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"
      ]
    }
  }
}
于 2018-06-13T15:09:25.127 回答