原文链接https://blog.youkuaiyun.com/he37176427/article/details/104714284
只写了后台,前端请求带上dateType给后台,后端返回封装了json字符串的map
GsonUtil.getJsonStringByObject()是封装的Gson.toJson的方法
前端接受解析结果 并设置echatrs参数 即可完成图表绘制
/**
* 消息趋势统计 dateType由前端传递 包括年月周
* 按年则统计过去12个月
* 按月则统计过去30天
* 按周则统计过去7天
**/
public Map<String, String> msgTrendCount(String dateType) {
//每个索引的时间field name 可能不同 根据索引设置
String rangeField = "messageSendTime";
//索引名
String index = EsIndexName.TG_MESSAGE.getIndexName();
return dateHistogram(rangeField, dateType, index);
}
private Map<String, String> dateHistogram(String rangeField, String dateType, String... indices) {
Map<String, String> map = new HashMap<>(4);
try {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String from;
//extended bounds参数
String boundsStart;
String boundsEnd;
String to = formatter.format(now);
String format = "yyyy-MM-dd";
//bounds的格式化类型需要和format相同
DateTimeFormatter boundsFormatter = DateTimeFormatter.ofPattern(format);
//间隔 年类型较为特殊
DateHistogramInterval interval = DateHistogramInterval.DAY;
switch (dateType) {
case "week": {
from = formatter.format(now.minusDays(6));
boundsStart = boundsFormatter.format(now.minusDays(6));
boundsEnd = boundsFormatter.format(now);
break;
}
case "year": {
from = formatter.format(now.minusMonths(11));
format = "yyyy-MM";
boundsFormatter = DateTimeFormatter.ofPattern(format);
interval = DateHistogramInterval.MONTH;
boundsStart = boundsFormatter.format(now.minusMonths(11));
boundsEnd = boundsFormatter.format(now);
break;
}
case "month":
default: {
from = formatter.format(now.minusDays(29));
boundsStart = boundsFormatter.format(now.minusDays(29));
boundsEnd = boundsFormatter.format(now);
break;
}
}
//范围查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.rangeQuery(rangeField).from(from).to(to));
//dateHistogram
AggregationBuilder aggregationBuilder = AggregationBuilders.dateHistogram("dateHistogram")//自定义名称
.dateHistogramInterval(interval)//设置间隔
.minDocCount(0)//返回空桶
.field(rangeField)//指定时间字段
.format(format)//设定返回格式
.extendedBounds(new ExtendedBounds(boundsStart, boundsEnd));//设定范围
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//指定size为0 不返回文档 因为只需要数量
searchSourceBuilder.query(boolQueryBuilder).aggregation(aggregationBuilder).size(0);
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(searchSourceBuilder);
searchRequest.indices(indices);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
Aggregation agg = searchResponse.getAggregations().get("dateHistogram");
List<? extends Histogram.Bucket> buckets = ((Histogram) agg).getBuckets();
List<String> dateList = new ArrayList<>(30);
List<String> countList = new ArrayList<>(30);
for (Histogram.Bucket bucket : buckets) {
// maybe:如果不是年 则将key中的年份去掉
dateList.add(bucket.getKeyAsString());
countList.add(String.valueOf(bucket.getDocCount()));
}
map.put("chartTime", GsonUtil.getJsonStringByObject(dateList));
map.put("chartCount", GsonUtil.getJsonStringByObject(countList));
} catch (Exception e) {
log.error("统计日期直方图出错:" + e.getMessage());
}
return map;
}
之前费劲写的好多代码来做这个统计,分别用日期去一天天的查数量,最近学习了解了es自带的 date_histogram
完全契合需求,遂将原笨拙的代码删除改为es的自带聚合 (果然人还是要多读书呀。。。)
速度上目前文档数量不大,没有差别,但预计随着后期文档数量增加,肯定是es的聚合更加高效。
附上自己写的
//创建search请求
SearchRequest searchRequest = new SearchRequest("final_edition_mail_log");
//用SearchSourceBuilder来构造查询请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
List<SuspiciousAccount> list = new ArrayList<SuspiciousAccount>();
boolQueryBuilder
.must( QueryBuilders.termsQuery("fullName.keyword", param.get("orgName")))
.must(QueryBuilders.termsQuery("abroad.keyword","OUT"))//国外
//.must(QueryBuilders.existsQuery("credit"))
.must(QueryBuilders.termsQuery("behavior","1"))
.filter(QueryBuilders.rangeQuery("create_date")
.timeZone("GMT+8")
.gte(DateUtil.getFirstDayOfMonth()).lte(DateUtil.getLastDayOfMonth()));
AggregationBuilder aggregationBuilder = AggregationBuilders.dateHistogram("create_date")//自定义名称
.fixedInterval(DateHistogramInterval.DAY)//设置间隔
.minDocCount(1)//返回空桶
.field("create_date")//指定时间字段
;
sourceBuilder.query(boolQueryBuilder).aggregation(aggregationBuilder).size(0);
searchRequest.source(sourceBuilder);
//发送请求
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregation agg = searchResponse.getAggregations().get("create_date");
List<? extends Histogram.Bucket> buckets = ((Histogram) agg).getBuckets();
for (Histogram.Bucket bucket : buckets) {
SuspiciousAccount account = new SuspiciousAccount();
String longTime = bucket.getKeyAsString();
log.info(""+bucket.getKeyAsString());
log.info(String.valueOf(bucket.getDocCount()));
account.setDayOf(DateUtil.timeStamp2Date(String.valueOf(Long.parseLong(longTime)/1000),"yyyy-MM-dd"));
account.setSuccessSum(String.valueOf(bucket.getDocCount()));
list.add(account);
}
GET final_edition_mail_log/_search
{
"size" : 0,
"aggs": {
"sales": {
"date_histogram": {
"field": "create_date",
"interval": "day",
"format": "yyyy-MM-dd"
}
}
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10000,
"relation" : "gte"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"sales" : {
"buckets" : [
{
"key_as_string" : "yyyy-MM-dd1588809600000",
"key" : 1588809600000,
"doc_count" : 4064865
},
{
"key_as_string" : "yyyy-MM-dd1588896000000",
"key" : 1588896000000,
"doc_count" : 8501193
}
]
}
}
}