BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
RangeQueryBuilder createTimeQuery = QueryBuilders.rangeQuery("createTime");
createTimeQuery.gte(startTime);
createTimeQuery.lte(endTime);
queryBuilder.must(createTimeQuery);
//过滤面授班
String skuPropertyId = commodityGoodsSupport.getTrainingTypePropertyIdByGoodsCategory(context);
String offLineValue = commodityGoodsSupport.getTrainingTypeOfflineClassPropertyValue(context, "");
String onLineValue = commodityGoodsSupport.getTrainingTypeOnlineClassPropertyValue(context, "");
if (CollectionUtils.isNotEmpty(searchDto.getSkuPropertyList())) {
if (searchDto.isQueryOffLineClass() && StringUtils.isNotBlank(offLineValue)) {
for (SkuPropertyKeyValue item : searchDto.getSkuPropertyList()) {
if (item.getPropertyId().equals(skuPropertyId)) {
item.setValue(offLineValue);
}
}
} else if (!searchDto.isQueryOffLineClass() && StringUtils.isNotBlank(onLineValue)) {
for (SkuPropertyKeyValue item : searchDto.getSkuPropertyList()) {
if (item.getPropertyId().equals(skuPropertyId)) {
item.setValue(onLineValue);
}
}
}
BoolQueryBuilder nestedBoolQuery;
for (SkuPropertyKeyValue item : searchDto.getSkuPropertyList()) {
if (StringUtils.isNotBlank(item.getPropertyId()) && StringUtils.isNotBlank(item.getValue())) {
nestedBoolQuery = QueryBuilders.boolQuery();
nestedBoolQuery.filter(QueryBuilders.termQuery("skuProperties.id", item.getPropertyId()));
nestedBoolQuery.filter(QueryBuilders.termQuery("skuProperties.value", item.getValue()));
queryBuilder.filter(QueryBuilders.nestedQuery("skuProperties", nestedBoolQuery, ScoreMode.None));
}
}
}
queryBuilder.filter(QueryBuilders.termQuery("projectId", context.getProjectId()));
queryBuilder.filter(QueryBuilders.termQuery("subProjectId", context.getSubProjectId()));
// 培训班id
if (StringUtils.isNotBlank(searchDto.getTrainClassId())) {
QueryBuilder trainClassQuery = QueryBuilders.termQuery("trainClassId", searchDto.getTrainClassId());
queryBuilder.must(trainClassQuery);
}
//地区,
if(StringUtils.isNotBlank(searchDto.getRegionPath()) && searchDto.getRegionPath().split("/").length>3){
QueryBuilder trainClassQuery = QueryBuilders.termQuery("unitRegion.countryId", searchDto.getRegionPath().split("/")[3]);
queryBuilder.must(trainClassQuery);
}
SumAggregationBuilder swapInCountMetrics = AggregationBuilders.sum("swapInCountMetrics").field("swapInCount");
SumAggregationBuilder openCountMetrics = AggregationBuilders.sum("openCountMetrics").field("openCount");
SumAggregationBuilder swapOutCountMetrics = AggregationBuilders.sum("swapOutCountMetrics")
.field("swapOutCount");
SumAggregationBuilder refundCountMetrics = AggregationBuilders.sum("refundCountMetrics").field("refundCount");
AggregationBuilder aggregation = AggregationBuilders.terms("trainingGroup").field("unitRegion.countryId")
.subAggregation(swapInCountMetrics).subAggregation(openCountMetrics).subAggregation(swapOutCountMetrics)
.subAggregation(refundCountMetrics);
SearchResponse sr = client.prepareSearch().setIndices(super.entityInformation.getIndexName())
.setTypes(super.entityInformation.getType()).setQuery(queryBuilder)// 过滤条件
.addAggregation(aggregation)// 聚合
.execute().actionGet();
Terms trainingGroup = sr.getAggregations().get("trainingGroup");// 可以统计当前匹配的聚合结果集
return trainingGroup;
#获取查询结果
Terms trainingGroup = getBucketAreaInfo(context, searchDto, startTime, endTime);//获取班级聚合的数据
List<Bucket> bucketList = trainingGroup.getBuckets();
for (Bucket b : bucketList) {
PureOpenBucketDto dto = new PureOpenBucketDto();
dto.setCountryId(b.getKeyAsString());
Aggregations aggs = b.getAggregations();
Sum swapInSum = aggs.get("swapInCountMetrics");
Sum swapOutSum = aggs.get("swapOutCountMetrics");
Sum openSum = aggs.get("openCountMetrics");
Sum refundSum = aggs.get("refundCountMetrics");
dto.setSwapInCount(((Double) swapInSum.getValue()).intValue());
dto.setSwapOutCount(((Double) swapOutSum.getValue()).intValue());
dto.setOpenCount(((Double) openSum.getValue()).intValue());
dto.setRefundCount(((Double) refundSum.getValue()).intValue());
dtoList.add(dto);
}
return dtoList;