ElasticSearch 聚合查询之桶聚合

  • Terms Aggregation 根据字段项分组聚合
{
 "query": {
   "term": {
     "teamCityEn": {
       "value": "Memphis"
     }
   }
 },
 "aggs": {
   "ageTerms": {
     "terms": {
       "field": "age",
       "size": 10
     }
   }
 },
 "size": 0
}
  • order 分组聚合排序
{
 "query": {
   "term": {
     "teamCityEn": {
       "value": "Memphis"
     }
   }
 },
 "aggs": {
   "ageTerms": {
     "terms": {
       "field": "age",
       "size": 10,
       "order": {
         "_key": "desc" (根据文档数排序则 _count)
       }
     }
   }
 },
 "size": 0
}
  • 分组平均值排序
{
 "aggs": {
   "ageTerms": {
     "terms": {
       "field": "teamCityEn",
       "size": 10,
       "order": {
         "agsAge": "desc"
       }
     },
     "aggs": {
       "agsAge": {
         "avg": {
           "field": "age"
         }
       }
     }
   }
 },
 "size": 0
}
  • 筛选分组聚合
{
 "aggs": {
   "ageTerms": {
     "terms": {
       "field": "teamCityEn",
       "include": ["Milwaukee","Dallas","Los Angeles"], 
       "exclude": ["Miami"], 
       "size": 10,
       "order": {
         "agsAge": "desc"
       }
     },
     "aggs": {
       "agsAge": {
         "avg": {
           "field": "age"
         }
       }
     }
   }
 },
 "size": 0
}
  • Range Aggregation 范围分组聚合
{
  "aggs": {
    "ageRage": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "to": 25,
            "key": "黄金周期"
          },
          {
            "from": 25,
            "to": 35,
            "key": "衰退期"
          },{
            "from": 35,
            "key": "退役"
          }
        ]
      }
    }
  },
  "size": 0
}
  • Date Range Aggregation 时间范围分组聚合
{
  "aggs": {
    "birthDayRange":{
      "date_range": {
        "field": "birthDay",
        "format": "yyyy-MM-dd", 
        "ranges": [
          {
             "to": "1980-01-01"
          },
          {
            "from": "1980-01-01",
            "to": "1990-01-01"
          },
          {
            "from": "1990-01-01"
          }
        ]
      }
    }
  },
  "size": 0
}
  • Date Histogram Aggregation 时间柱状图聚合

按天、月、年等进行聚合统计。可按 year (1y), quarter (1q), month (1M), week (1w), day(1d), hour (1h), minute (1m), second (1s) 间隔聚合

{
  "aggs": {
    "birthDayRange":{
      "date_histogram": {
        "field": "birthDay",
        "format": "yyyy-MM-dd", 
        "calendar_interval": "month"
      }
    }
  },
  "size": 0
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值