|
@ -1,6 +1,10 @@
|
|
|
|
|
|
package com.yihu.quota.util;
|
|
|
|
|
|
import com.yihu.quota.dao.view.ViewDao;
|
|
|
import com.yihu.quota.dao.view.ViewDimensionDao;
|
|
|
import com.yihu.quota.model.view.View;
|
|
|
import com.yihu.quota.model.view.ViewDimension;
|
|
|
import org.elasticsearch.action.search.SearchRequestBuilder;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.client.transport.TransportClient;
|
|
@ -14,6 +18,7 @@ import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
|
|
|
import org.elasticsearch.search.aggregations.metrics.min.InternalMin;
|
|
|
import org.elasticsearch.search.aggregations.metrics.sum.InternalSum;
|
|
|
import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.*;
|
|
@ -24,6 +29,11 @@ import java.util.*;
|
|
|
@Component
|
|
|
public class AggregationBuildHandler {
|
|
|
|
|
|
@Autowired
|
|
|
private ViewDimensionDao viewDimensionDao;
|
|
|
@Autowired
|
|
|
private ViewDao viewDao;
|
|
|
|
|
|
/**
|
|
|
* 简单聚合
|
|
|
* @param aggType 聚合类型
|
|
@ -103,7 +113,6 @@ public class AggregationBuildHandler {
|
|
|
public Map<String, Aggregation> structAggregationQuery(TransportClient client,String index,String type,
|
|
|
BoolQueryBuilder boolQueryBuilder,
|
|
|
LinkedList<AbstractAggregationBuilder> aggBuilderList){
|
|
|
|
|
|
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setTypes(type).setQuery(boolQueryBuilder);
|
|
|
for(AbstractAggregationBuilder aggBuilder : aggBuilderList){
|
|
|
searchRequestBuilder.addAggregation(aggBuilder);
|
|
@ -116,20 +125,44 @@ public class AggregationBuildHandler {
|
|
|
|
|
|
/**
|
|
|
* es bucket 数据结果解析
|
|
|
* @param rowList 行维度列表
|
|
|
* @param cloumnList 列维度列表
|
|
|
* @param
|
|
|
* @param map 聚合查询结果
|
|
|
* @return
|
|
|
*/
|
|
|
public List<Map<String, Object>> dataParsing( List<String> rowList, List<String> cloumnList,Map<String, Aggregation> map){
|
|
|
public List<Map<String, Object>> dataParsing(String viewCode ,Map<String, Aggregation> map){
|
|
|
List<String> rowList = new LinkedList<>();
|
|
|
rowList.add("区县_terms");
|
|
|
rowList.add("合计");
|
|
|
List<String> cloumnList = new ArrayList<>();
|
|
|
cloumnList.add("合计_count_result");
|
|
|
cloumnList.add("性别_terms");
|
|
|
|
|
|
View view = viewDao.findByCode(viewCode);
|
|
|
// 视图各组组内顶层行维度(升序)
|
|
|
List<ViewDimension> groupTopRowDimensionList = viewDimensionDao.getGroupTopRowDimensionList(view.getId());
|
|
|
view.setGroupTopRowDimensionList(groupTopRowDimensionList);
|
|
|
// 视图各组组内其他行维度(升序)
|
|
|
Map<Integer, List<ViewDimension>> groupOtherRowDimensionsMap = new HashMap<>(16);
|
|
|
for (ViewDimension viewDimension : groupTopRowDimensionList) {
|
|
|
rowList.add(viewDimension.getDimensionCode());
|
|
|
List<ViewDimension> otherRowDimensionList = viewDimensionDao.getGroupOtherRowDimensionList(view.getId(), viewDimension.getGroupRow());
|
|
|
for (ViewDimension otherViewDimension : otherRowDimensionList) {
|
|
|
rowList.add(otherViewDimension.getDimensionCode());
|
|
|
}
|
|
|
}
|
|
|
// 视图列维度(升序)
|
|
|
List<ViewDimension> colDimensionList = viewDimensionDao.getColDimensionList(view.getId());
|
|
|
for (ViewDimension colViewDimension : colDimensionList) {
|
|
|
cloumnList.add(colViewDimension.getDimensionCode());
|
|
|
}
|
|
|
|
|
|
List<Map<String, Object>> resultList = new LinkedList<Map<String, Object>>();
|
|
|
//合计行数据处理
|
|
|
Map<String, Object> sumResultMap = new HashMap<>();
|
|
|
sumResultMap.put("区县_terms","合计");
|
|
|
sumResultMap.put(rowList.get(0),"合计");//默认第一个行维度
|
|
|
for(String rowAggCode: rowList) {
|
|
|
if (rowAggCode.contains("合计")) {
|
|
|
for (String cloumnAggCode : cloumnList) {
|
|
|
|
|
|
for(String aggCode:map.keySet()) {
|
|
|
Aggregation aggregation = map.get(aggCode);
|
|
|
if (aggregation.getName().equals("合计_" + cloumnAggCode)) {
|
|
@ -204,22 +237,24 @@ public class AggregationBuildHandler {
|
|
|
*/
|
|
|
public Map<String, Object> getInternalAggValue(String key, Aggregation aggregation){
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
Object value = null;
|
|
|
if (aggregation instanceof InternalValueCount) {
|
|
|
InternalValueCount valueCount = (InternalValueCount) aggregation;
|
|
|
map.put(key, valueCount.getValue());
|
|
|
value = valueCount.getValue();
|
|
|
}else if (aggregation instanceof InternalSum) {
|
|
|
InternalSum valueCount = (InternalSum) aggregation;
|
|
|
map.put(key, valueCount.getValue());
|
|
|
value = valueCount.getValue();
|
|
|
}else if (aggregation instanceof InternalMax) {
|
|
|
InternalMax valueCount = (InternalMax) aggregation;
|
|
|
map.put(key, valueCount.getValue());
|
|
|
value = valueCount.getValue();
|
|
|
}else if (aggregation instanceof InternalMin) {
|
|
|
InternalMin valueCount = (InternalMin) aggregation;
|
|
|
map.put(key, valueCount.getValue());
|
|
|
value = valueCount.getValue();
|
|
|
}else if (aggregation instanceof InternalAvg) {
|
|
|
InternalAvg valueCount = (InternalAvg) aggregation;
|
|
|
map.put(key, valueCount.getValue());
|
|
|
value = valueCount.getValue();
|
|
|
}
|
|
|
map.put(key, value);
|
|
|
return map;
|
|
|
}
|
|
|
|
|
@ -233,7 +268,7 @@ public class AggregationBuildHandler {
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
while (gradeBucket.hasNext()) {
|
|
|
Terms.Bucket b = gradeBucket.next();
|
|
|
String subVal = aggCode+"-" + b.getKey();
|
|
|
String subVal = aggCode+ "-" + b.getKey();
|
|
|
for(Aggregation aggregation : b.getAggregations().asList()){
|
|
|
if (aggregation instanceof InternalValueCount) {
|
|
|
InternalValueCount valueCount = (InternalValueCount) aggregation;
|
|
@ -260,87 +295,6 @@ public class AggregationBuildHandler {
|
|
|
}
|
|
|
|
|
|
|
|
|
// /**
|
|
|
// * 递归解析 json
|
|
|
// * @param resultList
|
|
|
// * @param
|
|
|
// */
|
|
|
// private void expainAggregationResult(List<Map<String, Object>> resultList,Iterator<Terms.Bucket> gradeBucket,String aggKey){
|
|
|
// System.out.println("aggKey=" + aggKey);
|
|
|
// while (gradeBucket.hasNext()) {
|
|
|
// Terms.Bucket b = gradeBucket.next();
|
|
|
// String subVal = StringUtils.isEmpty(aggKey) ? b.getKey().toString() : (aggKey + ":" + b.getKey());
|
|
|
// for(Aggregation aggregation : b.getAggregations().asList()){
|
|
|
// System.out.println("bucketNmae = " + b.getKey().toString() + "; aggName=" + aggregation.getName());
|
|
|
// subVal += ";"+ aggregation.getName();
|
|
|
// if (aggregation instanceof InternalValueCount) {
|
|
|
// Map<String, Object> countMap = new HashMap<>();
|
|
|
// InternalValueCount valueCount = (InternalValueCount) aggregation;
|
|
|
// countMap.put(subVal, valueCount.getValue() );
|
|
|
// resultList.add(countMap);
|
|
|
// }else if (aggregation instanceof InternalSum) {
|
|
|
// Map<String, Object> sumMap = new HashMap<>();
|
|
|
// InternalSum valueCount = (InternalSum) aggregation;
|
|
|
// sumMap.put(subVal, valueCount.getValue() );
|
|
|
// resultList.add(sumMap);
|
|
|
// }else if (aggregation instanceof InternalMax) {
|
|
|
// Map<String, Object> maxMap = new HashMap<>();
|
|
|
// InternalMax valueCount = (InternalMax) aggregation;
|
|
|
// maxMap.put(subVal, valueCount.getValue() );
|
|
|
// resultList.add(maxMap);
|
|
|
// }else if (aggregation instanceof InternalMin) {
|
|
|
// Map<String, Object> minMap = new HashMap<>();
|
|
|
// InternalMin valueCount = (InternalMin) aggregation;
|
|
|
// minMap.put(subVal, valueCount.getValue() );
|
|
|
// resultList.add(minMap);
|
|
|
// }else if (aggregation instanceof InternalAvg) {
|
|
|
// Map<String, Object> avgMap = new HashMap<>();
|
|
|
// InternalAvg valueCount = (InternalAvg) aggregation;
|
|
|
// avgMap.put(subVal, valueCount.getValue() );
|
|
|
// resultList.add(avgMap);
|
|
|
// }else {
|
|
|
// Terms terms = (Terms)aggregation;
|
|
|
// expainAggregationResult(resultList,terms.getBuckets().iterator(),subVal);
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
// public void parsingAggregation(List<Map<String, Object>> resultList,Aggregation aggregation){
|
|
|
// if (aggregation instanceof InternalValueCount) {
|
|
|
// Map<String, Object> countMap = new HashMap<>();
|
|
|
// InternalValueCount valueCount = (InternalValueCount) aggregation;
|
|
|
// countMap.put(aggregation.getName(), valueCount.getValue() );
|
|
|
// System.out.println(valueCount.getName());
|
|
|
// resultList.add(countMap);
|
|
|
// }else if (aggregation instanceof InternalSum) {
|
|
|
// Map<String, Object> sumMap = new HashMap<>();
|
|
|
// InternalSum valueCount = (InternalSum) aggregation;
|
|
|
// sumMap.put(valueCount.getName(), valueCount.getValue() );
|
|
|
// System.out.println(valueCount.getName());
|
|
|
// resultList.add(sumMap);
|
|
|
// }else if (aggregation instanceof InternalMax) {
|
|
|
// Map<String, Object> maxMap = new HashMap<>();
|
|
|
// InternalMax valueCount = (InternalMax) aggregation;
|
|
|
// maxMap.put(valueCount.getName(), valueCount.getValue() );
|
|
|
// resultList.add(maxMap);
|
|
|
// }else if (aggregation instanceof InternalMin) {
|
|
|
// Map<String, Object> minMap = new HashMap<>();
|
|
|
// InternalMin valueCount = (InternalMin) aggregation;
|
|
|
// minMap.put(valueCount.getName(), valueCount.getValue() );
|
|
|
// resultList.add(minMap);
|
|
|
// }else if (aggregation instanceof InternalAvg) {
|
|
|
// Map<String, Object> avgMap = new HashMap<>();
|
|
|
// InternalAvg valueCount = (InternalAvg) aggregation;
|
|
|
// avgMap.put(valueCount.getName(), valueCount.getValue() );
|
|
|
// resultList.add(avgMap);
|
|
|
// }else {
|
|
|
// Terms terms = (Terms)aggregation;
|
|
|
// String termName = terms.getName();
|
|
|
// System.out.println("termName:" + termName);
|
|
|
// expainAggregationResult(resultList, terms.getBuckets().iterator(), termName);
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
|
|
|
|