Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Elasearch time query and grouping statistics

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

/ * query data from ES * ES time if not set, the default is UTC time, which is 8 hours different from Beijing time. When querying statistics, if statistics are not made, the data will definitely be inaccurate. * ES version: 6.4.1. The javaApi used is the statistical result of Elasearch High Level Rest Client6.4 * @ param monthDate (patten:yyyy-MM) * @ return List. * / public List queryOrderCountDataFromEs (String monthDate) {try {/ * conditional query (time range) * / String startTime = DateUtil.getMonthFirstDay (DateUtil.dateStr2Date (monthDate + "- 01", DateUtil.PATTERN_DTSHORTLINE); String endTime = DateUtil.getMonthLastDay (monthDate + "- 01", DateUtil.PATTERN_DTSHORTLINE)) Date s = DateUtil.dateStr2Date (startTime + "00:00:00", DateUtil.PATTERN_SIMPLE); Date e = DateUtil.dateStr2Date (endTime + "23:59:59", DateUtil.PATTERN_SIMPLE); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder (); BoolQueryBuilder boolQuery = QueryBuilders.boolQuery (); boolQuery.must (QueryBuilders.rangeQuery ("order_time") .gte (s) .lte (e)); searchSourceBuilder.query (boolQuery) / * Group aggregation * / TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms ("orderStatusCount"). Field ("order_status"); aggregationBuilder.size (ESConstants.ES_AGG_SIZE); aggregationBuilder.collectMode (Aggregator.SubAggCollectionMode.BREADTH_FIRST); DateHistogramAggregationBuilder field = AggregationBuilders.dateHistogram ("orderTime"). Field ("order_time") / * * pay attention to the problem of jet lag in time statistics. When making statistics, you can set the time zone and there is no need to set the offset. * the parameter offset offset is also useful at some times. It can define the beginning of the day. For example, it can be set from 3: 00 on the first day to 3: 00 on the next day as a day. By default, it starts at 0: 00 and ends at 0: 00 as the day's * / field.dateHistogramInterval (DateHistogramInterval.DAY) .timezone (DateTimeZone.getDefault ()); / .offset ("+ 8h"); aggregationBuilder.subAggregation (field) SearchSourceBuilder.aggregation (aggregationBuilder); SearchResponse response = client.searchDocument (ESConstants.ES_EC_ORDER_INDEX, ESConstants.ES_EC_ORDER_TYPE, searchSourceBuilder); / / result processing Terms byPath = response.getAggregations (). Get ("orderStatusCount"); List

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report