当前位置:   article > 正文

ElasticSearch 聚合查询之桶聚合_elasticsearch 桶聚合 按天分组

elasticsearch 桶聚合 按天分组
  • Terms Aggregation 根据字段项分组聚合
{
 "query": {
   "term": {
     "teamCityEn": {
       "value": "Memphis"
     }
   }
 },
 "aggs": {
   "ageTerms": {
     "terms": {
       "field": "age",
       "size": 10
     }
   }
 },
 "size": 0
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • order 分组聚合排序
{
 "query": {
   "term": {
     "teamCityEn": {
       "value": "Memphis"
     }
   }
 },
 "aggs": {
   "ageTerms": {
     "terms": {
       "field": "age",
       "size": 10,
       "order": {
         "_key": "desc" (根据文档数排序则 _count)
       }
     }
   }
 },
 "size": 0
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 分组平均值排序
{
 "aggs": {
   "ageTerms": {
     "terms": {
       "field": "teamCityEn",
       "size": 10,
       "order": {
         "agsAge": "desc"
       }
     },
     "aggs": {
       "agsAge": {
         "avg": {
           "field": "age"
         }
       }
     }
   }
 },
 "size": 0
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 筛选分组聚合
{
 "aggs": {
   "ageTerms": {
     "terms": {
       "field": "teamCityEn",
       "include": ["Milwaukee","Dallas","Los Angeles"], 
       "exclude": ["Miami"], 
       "size": 10,
       "order": {
         "agsAge": "desc"
       }
     },
     "aggs": {
       "agsAge": {
         "avg": {
           "field": "age"
         }
       }
     }
   }
 },
 "size": 0
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • Range Aggregation 范围分组聚合
{
  "aggs": {
    "ageRage": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "to": 25,
            "key": "黄金周期"
          },
          {
            "from": 25,
            "to": 35,
            "key": "衰退期"
          },{
            "from": 35,
            "key": "退役"
          }
        ]
      }
    }
  },
  "size": 0
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 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
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 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
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/889832
推荐阅读
相关标签
  

闽ICP备14008679号