/**
* 从ES中查询数据
* ES的时间如果不设置的话,默认是UTC时间,与北京时间相差8个小时,在查询统计的时候,如果不做统计的话,数据肯定是不准确的。
* ES版本:6.4.1。使用的javaApi是 Elasearch High Level Rest Client6.4
* @param monthDate (patten:yyyy-MM)
* @return List<SysCountResultVo> 统计结果。
*/
public List<SysCountResultVo> queryOrderCountDataFromEs(String monthDate) {
try {
/**
* 条件查询(时间范围)
*/
String startTime = DateUtil.getMonthFirstDay(DateUtil.dateStr2Date(monthDate + "-01", DateUtil.PATTERN_DTSHORTLINE));
String endTime = DateUtil.getMonthLastDay(DateUtil.dateStr2Date(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);
/**
* 分组聚合
*/
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");
/*
*时间统计的时候,注意时差问题。统计的时候,设置时区即可,不需要设置偏移量。
*offset偏移量这个参数,在某些时刻也是有用的,它可以自己定义一天的开始,比如设置从第一天的3点到第二天的3点为一天,默认都是从0点开始0点结束算做一天的
*/
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);
//结果处理
Terms byPath = response.getAggregations().get("orderStatusCount");
List<? extends Terms.Bucket> buckets = byPath.getBuckets();
List<SysCountResultVo> resultVos = new ArrayList<>();
for (Terms.Bucket bucket : buckets) {
Histogram agg = bucket.getAggregations().get("orderTime");
for (Histogram.Bucket entry : agg.getBuckets()) {
SysCountResultVo sysCountVo = new SysCountResultVo();
sysCountVo.setLocalPath(bucket.getKeyAsString());
long t = ((DateTime)entry.getKey()).getMillis();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(t);
sysCountVo.setDimension(DateUtil.date2DateStr(cal.getTime(),DateUtil.PATTERN_SIMPLE));
sysCountVo.setTimes(entry.getDocCount());
resultVos.add(sysCountVo);
}
}
return resultVos;
} catch (Exception e) {
logger.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>从ES中查询数据失败",e);
return null;
}
}