ElasticSearch结果中的平均聚合 [英] ElasticSearch average aggregation in results

查看:456
本文介绍了ElasticSearch结果中的平均聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得十大销售产品的平均价格,所以我做了以下查询:

I want to get average price in top 10 sale products, so I do the following query:

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

然后我收到一个错误,它说AggregationInitializationException [Aggregator [top_sale_avg_price]类型[avg]不能接受子聚合]

Then I get an error, it says "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 (i.e. score highest). The sort is then performed on this result set - so top hits won't work for you.

你也不能嵌套 top_hits avg 相互之间 - 您只能嵌套在一个桶中(例如术语,直方图,范围等)。

You also can't nest top_hits and avg inside each other - you can only nest inside a bucket (e.g. terms, histogram, range, etc.)

为了满足您的要求,您可以运行2个查询。
首先找到匹配的文件(十大销售产品):

To meet your requirements, you could run 2 queries. The first to find the matching documents (top ten sale products):

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

获得平均销售价格的第二个查询 - 您需要填充ids。

The second query to get an average sale price - you'll need to populate the ids.

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

这篇关于ElasticSearch结果中的平均聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆