es时间单值分组和时间递增分组

以下几种应用场景

1.计算每个小时数据量(8月29到31号每个时段的数据量)

GET /core_petition/_search?size=0
{
"query": {
    "bool": {
      "filter": {
        "range": {
            "registorOn": {
              "gte": "2022-08-29 16:06:00",
              "lte": "2022-08-31 16:06:00"
            }
        }
      }
    }
  },
  "aggs": {
    "group_by_state": {
      "date_histogram": {
        "field": "registorOn",
        "calendar_interval":"hour",
        "min_doc_count": 0
      }
    }
  }
}

2.根据字段分组得出每年的数据量

会得到该字段各个时段的数据(相当于对字段分组再对时间分组)

GET /core_petition/_search?size=0
{
  "aggs": {
    "sourceWayCount": {
      "terms": {
        "field": "sourceWay",--分组字段
        "size": 100
      },
      "aggs": {
        "hourCount": {
          "date_histogram": {
            "field": "registorOn",
            "calendar_interval": "year",
            "format": "yyyy",
            "min_doc_count": 1
          }
        }
      }
    }
  },
  "query": {
    "range": {
      "registorOn": {
        "gte": "2021-01-01 00:00:00",
        "lte": "2022-08-04 23:59:59"
      }
    }
  }
}

3.将时间分组放于前可以得到按时间分组的值

得到各个时段该字段分组的数据(相当于对时间分组再对字段分组)

GET /core_petition/_search
{
   "query": {
            "bool": {
                "must": [{
                    "range": {
                        "registorOn": {
                            "gte": "2021-01-01 00:00:00",
                            "lte": "2022-08-01 00:00:00"
                        }
                    }
                }]
            }
        },"size": 0,
      "aggs": {
        "hourCount": {
          "date_histogram": {
            "field": "registorOn",
            "calendar_interval": "year",
            "format": "yyyy",
            "min_doc_count": 1
          },
      "aggs": {
          "typicalCount": {
            "terms": {
              "field": "complaintQuality",
              "size": 3
            }
          }
        }
      }
    }
}

calendar_interval:year, quarter, month, week, day, hour, minute, second(年份、季度、月、周、日、小时、分钟、秒)。
java代码:

  AbstractAggregationBuilder agg = AggregationBuilders.dateHistogram("date").calendarInterval(DateHistogramInterval.MONTH).field("registorOn").minDocCount(0);
        agg.subAggregation(aggBuilder);
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        Aggregations aggs = esTemplate.aggs(agg,query,EsPetition.class);
        ParsedDateHistogram sourceWayPath = aggs.get("date");
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值