简体   繁体   English

ElasticSearch结果中的平均聚合

[英]ElasticSearch average aggregation in results

I want to get average price in top 10 sale products, so I do the following query: 我想获得前10大促销产品的平均价格,因此我执行以下查询:

{
    "aggs": {
        "top_sale_avg_price": {
            "avg": {"field": "price"},
            "aggs": {
                "top_sale_hits": {
                    "top_hits": {
                        "sort": [{"buy_count": {"order": "desc"}}],
                        "size": 10
                    }
                }
            }
        }
    }
}

Then I get an error, it says "AggregationInitializationException[Aggregator [top_sale_avg_price] of type [avg] cannot accept sub-aggregations]" 然后我得到一个错误,它说"AggregationInitializationException[Aggregator [top_sale_avg_price] of type [avg] cannot accept sub-aggregations]"

Top hits will find the ten documents that best match your query (ie score highest). 热门搜索将找到与您的查询最匹配(即得分最高)的十个文档。 The sort is then performed on this result set - so top hits won't work for you. 然后对这个结果集进行排序-因此,热门歌曲对您不起作用。

You also can't nest top_hits and avg inside each other - you can only nest inside a bucket (eg terms, histogram, range, etc.) 您也不能将top_hitsavg彼此嵌套-您只能嵌套在存储桶中(例如,术语,直方图,范围等)

To meet your requirements, you could run 2 queries. 为了满足您的要求,您可以运行2个查询。 The first to find the matching documents (top ten sale products): 首先找到匹配的文件(十大销售产品):

{
  "fields" : ["_id"],
  "sort": [
    { "buy_count": { "order": "desc" }}
   ],
  "size":10,
  "query": {
    "match_all": {}
  }
}'

The second query to get an average sale price - you'll need to populate the ids. 第二个查询以获取平均销售价格-您需要填充ID。

{
  "filter" : {
    "ids": {
        "values": [ "07cfbc09-360b-10d8-e053-28ab48a5c296", "07cf438e-7830-19ec-e053-28ab48a59c0b"]}
  },
  "aggs" : {
    "top_sale_avg_price" : {
      "avg": {"field" : "price" }  }
   }
}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM