elasticsearch通过max聚合,按字段分组并按另一个分类

时间:2016-01-22 11:47:51

标签: elasticsearch

我有结构文档:

{group: 1, year: 2013, profit: 150} {group: 1, year: 2014, profit: 100} {group: 2, year: 2014, profit: 80} {group: 2, year: 2015, profit: 180}

我需要为每个群体找到最高年份,而不是按利润排序。所以我期待结果如:

{group: 2, year: 2015, profit: 180} {group: 1, year: 2014, profit: 150}

我发现如何在另一个内部创建聚合,通过top_hits获取数据但无法知道,最后如何按利润排序。

2 个答案:

答案 0 :(得分:0)

尝试使用sort by a metric对内部聚合进行排序。

示例:

GET /cars/transactions/_search
{
    "size" : 0,
    "aggs" : {
        "colors" : {
            "terms" : {
              "field" : "color",
              "order": {
                "avg_price" : "asc" 
              }
            },
            "aggs": {
                "avg_price": {
                    "avg": {"field": "price"} 
                }
            }
        }
    }
}

答案 1 :(得分:0)

您可以使用以下查询:

GET /index/type/_search
{
"size": 0, 
"aggs": {
  "aggregate_by_group": {
     "terms": {
        "field": "group",
          "order": {
           "max_profit": "desc"
        }
     },
     "aggs": {
        "max_profit": {
           "max": {
              "field": "profit"
           }
        },
          "top_category_hits": {
                "top_hits": {
                    "sort": [
                        {
                            "year": {
                                "order": "desc"
                            }
                        }
                    ],
                    "size" : 1
                }
            }
     }

   }
  }
}

输出将是:

 "aggregations": {
  "aggregate_by_group": {
     "doc_count_error_upper_bound": 0,
     "sum_other_doc_count": 0,
     "buckets": [
        {
           "key": 2,
           "doc_count": 2,
           "top_category_hits": {
              "hits": {
                 "total": 2,
                 "max_score": null,
                 "hits": [
                    {
                       "_index": "so1",
                       "_type": "so1",
                       "_id": "4",
                       "_score": null,
                       "_source": {
                          "group": 2,
                          "year": 2015,
                          "profit": 180
                       },
                       "sort": [
                          2015
                       ]
                    }
                 ]
              }
           },
           "max_profit": {
              "value": 180
           }
        },
        {
           "key": 1,
           "doc_count": 2,
           "top_category_hits": {
              "hits": {
                 "total": 2,
                 "max_score": null,
                 "hits": [
                    {
                       "_index": "so1",
                       "_type": "so1",
                       "_id": "2",
                       "_score": null,
                       "_source": {
                          "group": 1,
                          "year": 2014,
                          "profit": 100
                       },
                       "sort": [
                          2014
                       ]
                    }
                 ]
              }
           },
           "max_profit": {
              "value": 150
           }
        }
     ]
  }
}

您可以在此处看到结果按利润排序。 您将从sort值中获取所需的值,例如最高年份,并从max_profit汇总获取,而不是从_source获取。