获得多个平均聚合

时间:2017-07-18 14:44:57

标签: elasticsearch lucene

我有一个查询,我通过智能手机分组,然后通过运营商分组。在这些嵌套聚合中,我想找到该特定智能手机的每个运营商的平均价格,评级和速度。这是我目前的查询:

{
  "aggs": {
    "group_by_smartphone": {
      "terms": {
        "field": "smartphone"
      },
      "aggs": {
        "group_by_carrier": {
          "terms": {
            "field": "carrier"
          },
          "aggs": {
            "group_by_avg_price": {
              "avg": {
                "field": "price"
              }
            }
          },

            "group_by_rating":{
              "avg":{
                "field": "rating"
              }
            }

        }
      }
    }
  }
}

使用此当前查询,我收到错误消息:

  

"输入":" parsing_exception",       "原因":"在[group_by_carrier]中找到两个聚合类型定义:[terms]和[group_by_rating]",

如何解决此问题以显示每个平均值的正确结果?

1 个答案:

答案 0 :(得分:1)

请改为尝试:

{
  "aggs": {
    "group_by_smartphone": {
      "terms": {
        "field": "smartphone"
      },
      "aggs": {
        "group_by_carrier": {
          "terms": {
            "field": "carrier"
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "price"
              }
            },
            "avg_rating":{
              "avg":{
                "field": "rating"
              }
            },
            "avg_speed":{
              "avg":{
                "field": "speed"
              }
            }
          }    
        }
      }
    }
  }
}

所有子聚合都需要位于相同的aggs结构内。