提交 2cf686c2 编写于 作者: zlt2000's avatar zlt2000

优化周趋势查询语句

上级 b0283aee
...@@ -128,6 +128,26 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -128,6 +128,26 @@ public class AggregationServiceImpl implements IAggregationService {
.dateRange("currWeek") .dateRange("currWeek")
.field("timestamp") .field("timestamp")
.addRange(currDt.minusDays(7), currDt) .addRange(currDt.minusDays(7), currDt)
.subAggregation(
//聚合并且按日期分组查询7天内的数据
AggregationBuilders
.dateHistogram("statWeek")
.field("timestamp")
.dateHistogramInterval(DateHistogramInterval.DAY)
.format(CommonConstant.DATE_FORMAT)
//时区相差8小时
.timeZone(DateTimeZone.forOffsetHours(8))
.minDocCount(0L)
.extendedBounds(new ExtendedBounds(
localDate.minusDays(6).format(DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT)),
localDate.format(DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT))
))
.subAggregation(
AggregationBuilders
.cardinality("uv")
.field("ip.keyword")
)
)
) )
.addAggregation( .addAggregation(
//聚合查询30天内的数据 //聚合查询30天内的数据
...@@ -148,29 +168,8 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -148,29 +168,8 @@ public class AggregationServiceImpl implements IAggregationService {
.terms("operatingSystem") .terms("operatingSystem")
.field("operatingSystem.keyword") .field("operatingSystem.keyword")
) )
.addAggregation(
//聚合并且按日期分组查询7天内的数据
AggregationBuilders
.dateHistogram("statWeek")
.field("timestamp")
.dateHistogramInterval(DateHistogramInterval.DAY)
.format(CommonConstant.DATE_FORMAT)
//时区相差8小时
.timeZone(DateTimeZone.forOffsetHours(8))
.minDocCount(0)
.extendedBounds(new ExtendedBounds(
localDate.minusDays(7).format(DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT)),
localDate.format(DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT))
))
.subAggregation(
AggregationBuilders
.cardinality("uv")
.field("ip.keyword")
)
)
.setSize(0) .setSize(0)
.execute().actionGet(); .get();
Aggregations aggregations = response.getAggregations(); Aggregations aggregations = response.getAggregations();
Map<String, Object> result = new HashMap<>(9); Map<String, Object> result = new HashMap<>(9);
...@@ -179,7 +178,6 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -179,7 +178,6 @@ public class AggregationServiceImpl implements IAggregationService {
setCurrMonth(result, aggregations); setCurrMonth(result, aggregations);
setTermsData(result, aggregations, "browser"); setTermsData(result, aggregations, "browser");
setTermsData(result, aggregations, "operatingSystem"); setTermsData(result, aggregations, "operatingSystem");
setStatWeek(result, aggregations);
return result; return result;
} }
/** /**
...@@ -199,6 +197,8 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -199,6 +197,8 @@ public class AggregationServiceImpl implements IAggregationService {
InternalDateRange currWeek = aggregations.get("currWeek"); InternalDateRange currWeek = aggregations.get("currWeek");
InternalDateRange.Bucket bucket = currWeek.getBuckets().get(0); InternalDateRange.Bucket bucket = currWeek.getBuckets().get(0);
result.put("currWeek_pv", bucket.getDocCount()); result.put("currWeek_pv", bucket.getDocCount());
//赋值周趋势统计
setStatWeek(result, bucket.getAggregations());
} }
/** /**
* 赋值月统计 * 赋值月统计
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册