From 2cf686c29f545742645e63546163e0e75a26fd42 Mon Sep 17 00:00:00 2001 From: zlt Date: Fri, 10 May 2019 23:44:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=91=A8=E8=B6=8B=E5=8A=BF?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/AggregationServiceImpl.java | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/zlt-business/search-center/search-server/src/main/java/com/central/search/service/impl/AggregationServiceImpl.java b/zlt-business/search-center/search-server/src/main/java/com/central/search/service/impl/AggregationServiceImpl.java index 0bbef9a..83072ea 100644 --- a/zlt-business/search-center/search-server/src/main/java/com/central/search/service/impl/AggregationServiceImpl.java +++ b/zlt-business/search-center/search-server/src/main/java/com/central/search/service/impl/AggregationServiceImpl.java @@ -128,6 +128,26 @@ public class AggregationServiceImpl implements IAggregationService { .dateRange("currWeek") .field("timestamp") .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( //聚合查询30天内的数据 @@ -148,29 +168,8 @@ public class AggregationServiceImpl implements IAggregationService { .terms("operatingSystem") .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) - .execute().actionGet(); - + .get(); Aggregations aggregations = response.getAggregations(); Map result = new HashMap<>(9); @@ -179,7 +178,6 @@ public class AggregationServiceImpl implements IAggregationService { setCurrMonth(result, aggregations); setTermsData(result, aggregations, "browser"); setTermsData(result, aggregations, "operatingSystem"); - setStatWeek(result, aggregations); return result; } /** @@ -199,6 +197,8 @@ public class AggregationServiceImpl implements IAggregationService { InternalDateRange currWeek = aggregations.get("currWeek"); InternalDateRange.Bucket bucket = currWeek.getBuckets().get(0); result.put("currWeek_pv", bucket.getDocCount()); + //赋值周趋势统计 + setStatWeek(result, bucket.getAggregations()); } /** * 赋值月统计 -- GitLab