elasticsearch-7. 聚合

2018-04-30  本文已影响0人  donglq

聚合

功能

例子

GET /bank/_search
{
  "size": 0,
  "aggs": { //聚合
    "group_by_state": { //聚合名字
      "terms": { 
        "field": "state.keyword", //按照state.keyword字段聚合
        "order": { //排序
          "average_balance": "desc" //按照average_balance倒序排序
        }
      },
      "aggs": { //聚合
        "average_balance": { //聚合名字
          "avg": { //平均值
            "field": "balance" //计算balance字段的平均值
          }
        }
      }
    }
  }
}
GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_age": { //通过age分组
      "range": {
        "field": "age", //按照age字段
        "ranges": [ //年龄分段范围
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 40
          },
          {
            "from": 40,
            "to": 50
          }
        ]
      },
      "aggs": { 
        "group_by_gender": { //按照gender聚合
          "terms": {
            "field": "gender.keyword"
          },
          "aggs": {
            "average_balance": { //按照balance聚合
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
  }
}

上一篇下一篇

猜你喜欢

热点阅读