|
@ -53,94 +53,94 @@ public class AggregationBuildHandler {
|
|
|
@Autowired
|
|
|
private CubeService cubeService;
|
|
|
|
|
|
/**
|
|
|
* 简单聚合
|
|
|
* @param aggType 聚合类型
|
|
|
* @param aggName 聚合名称
|
|
|
* @param fieldName 聚合字段
|
|
|
* @return
|
|
|
*/
|
|
|
public AbstractAggregationBuilder addAggregationBuilder(String aggType,String aggName, String fieldName){
|
|
|
AbstractAggregationBuilder builder = null;
|
|
|
if(aggType.equals("sum")){
|
|
|
builder = AggregationBuilders.sum(aggName).field(fieldName);
|
|
|
}else if(aggType.equals("count")){
|
|
|
builder = AggregationBuilders.count(aggName).field(fieldName);
|
|
|
}else if(aggType.equals("avg")){
|
|
|
builder = AggregationBuilders.avg(aggName).field(fieldName);
|
|
|
}else if(aggType.equals("max")){
|
|
|
builder = AggregationBuilders.max(aggName).field(fieldName);
|
|
|
}else if(aggType.equals("min")){
|
|
|
builder = AggregationBuilders.min(aggName).field(fieldName);
|
|
|
}
|
|
|
return builder;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 分组后,简单聚合
|
|
|
* @param aggType 聚合类型
|
|
|
* @param aggName 聚合名称
|
|
|
* @param fieldName 聚合字段
|
|
|
* @return
|
|
|
*/
|
|
|
public AbstractAggregationBuilder addTermAggregationBuilder(String termName, String termFieldName,String aggType,String aggName, String fieldName){
|
|
|
AbstractAggregationBuilder builder = null;
|
|
|
TermsBuilder termsBuilder = addTermsBuilder(termName,termFieldName);
|
|
|
if(aggType.equals("sum")){
|
|
|
builder = termsBuilder.subAggregation(AggregationBuilders.sum(aggName).field(fieldName));
|
|
|
}else if(aggType.equals("count")){
|
|
|
builder = termsBuilder.subAggregation(AggregationBuilders.count(aggName).field(fieldName));
|
|
|
}else if(aggType.equals("avg")){
|
|
|
builder = termsBuilder.subAggregation(AggregationBuilders.avg(aggName).field(fieldName));
|
|
|
}else if(aggType.equals("max")){
|
|
|
builder = termsBuilder.subAggregation(AggregationBuilders.max(aggName).field(fieldName));
|
|
|
}else if(aggType.equals("min")){
|
|
|
builder = termsBuilder.subAggregation(AggregationBuilders.min(aggName).field(fieldName));
|
|
|
}
|
|
|
return builder;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加分组
|
|
|
* @param termName
|
|
|
* @param fieldName
|
|
|
* @return
|
|
|
*/
|
|
|
public TermsBuilder addTermsBuilder(String termName, String fieldName){
|
|
|
TermsBuilder termBuilder = AggregationBuilders.terms(termName).field(fieldName);
|
|
|
return termBuilder;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param client
|
|
|
* @param boolQueryBuilder 查询的过滤条件
|
|
|
* @param aggBuilderList 聚合组
|
|
|
* 聚合组中成员是 单个的聚合查询,其中单个的聚合查询可以嵌套子聚合查询 如:
|
|
|
* 单个聚合 查询 :TermsBuilder firstAgg= AggregationBuilders.terms("player_count ").field("team");
|
|
|
* 带有子聚合的聚合查询 :
|
|
|
* TermsBuilder secondAgg= AggregationBuilders.terms("pos_count").field("position")
|
|
|
* .subAggregation(
|
|
|
* AggregationBuilders.dateHistogram("by_year").field("dateOfBirth").interval((DateHistogramInterval.YEAR))
|
|
|
* .subAggregation(
|
|
|
* AggregationBuilders.avg("avg_children").field("children")
|
|
|
* )
|
|
|
* );
|
|
|
* @return 聚合结果集
|
|
|
*/
|
|
|
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);
|
|
|
}
|
|
|
SearchResponse response = searchRequestBuilder.execute().actionGet();
|
|
|
Map<String, Aggregation> map = response.getAggregations().getAsMap();
|
|
|
client.close();
|
|
|
return map;
|
|
|
}
|
|
|
// /**
|
|
|
// * 简单聚合
|
|
|
// * @param aggType 聚合类型
|
|
|
// * @param aggName 聚合名称
|
|
|
// * @param fieldName 聚合字段
|
|
|
// * @return
|
|
|
// */
|
|
|
// public AbstractAggregationBuilder addAggregationBuilder(String aggType,String aggName, String fieldName){
|
|
|
// AbstractAggregationBuilder builder = null;
|
|
|
// if(aggType.equals("sum")){
|
|
|
// builder = AggregationBuilders.sum(aggName).field(fieldName);
|
|
|
// }else if(aggType.equals("count")){
|
|
|
// builder = AggregationBuilders.count(aggName).field(fieldName);
|
|
|
// }else if(aggType.equals("avg")){
|
|
|
// builder = AggregationBuilders.avg(aggName).field(fieldName);
|
|
|
// }else if(aggType.equals("max")){
|
|
|
// builder = AggregationBuilders.max(aggName).field(fieldName);
|
|
|
// }else if(aggType.equals("min")){
|
|
|
// builder = AggregationBuilders.min(aggName).field(fieldName);
|
|
|
// }
|
|
|
// return builder;
|
|
|
// }
|
|
|
|
|
|
|
|
|
// /**
|
|
|
// * 分组后,简单聚合
|
|
|
// * @param aggType 聚合类型
|
|
|
// * @param aggName 聚合名称
|
|
|
// * @param fieldName 聚合字段
|
|
|
// * @return
|
|
|
// */
|
|
|
// public AbstractAggregationBuilder addTermAggregationBuilder(String termName, String termFieldName,String aggType,String aggName, String fieldName){
|
|
|
// AbstractAggregationBuilder builder = null;
|
|
|
// TermsBuilder termsBuilder = addTermsBuilder(termName,termFieldName);
|
|
|
// if(aggType.equals("sum")){
|
|
|
// builder = termsBuilder.subAggregation(AggregationBuilders.sum(aggName).field(fieldName));
|
|
|
// }else if(aggType.equals("count")){
|
|
|
// builder = termsBuilder.subAggregation(AggregationBuilders.count(aggName).field(fieldName));
|
|
|
// }else if(aggType.equals("avg")){
|
|
|
// builder = termsBuilder.subAggregation(AggregationBuilders.avg(aggName).field(fieldName));
|
|
|
// }else if(aggType.equals("max")){
|
|
|
// builder = termsBuilder.subAggregation(AggregationBuilders.max(aggName).field(fieldName));
|
|
|
// }else if(aggType.equals("min")){
|
|
|
// builder = termsBuilder.subAggregation(AggregationBuilders.min(aggName).field(fieldName));
|
|
|
// }
|
|
|
// return builder;
|
|
|
// }
|
|
|
|
|
|
// /**
|
|
|
// * 添加分组
|
|
|
// * @param termName
|
|
|
// * @param fieldName
|
|
|
// * @return
|
|
|
// */
|
|
|
// public TermsBuilder addTermsBuilder(String termName, String fieldName){
|
|
|
// TermsBuilder termBuilder = AggregationBuilders.terms(termName).field(fieldName);
|
|
|
// return termBuilder;
|
|
|
// }
|
|
|
|
|
|
// /**
|
|
|
// *
|
|
|
// * @param client
|
|
|
// * @param boolQueryBuilder 查询的过滤条件
|
|
|
// * @param aggBuilderList 聚合组
|
|
|
// * 聚合组中成员是 单个的聚合查询,其中单个的聚合查询可以嵌套子聚合查询 如:
|
|
|
// * 单个聚合 查询 :TermsBuilder firstAgg= AggregationBuilders.terms("player_count ").field("team");
|
|
|
// * 带有子聚合的聚合查询 :
|
|
|
// * TermsBuilder secondAgg= AggregationBuilders.terms("pos_count").field("position")
|
|
|
// * .subAggregation(
|
|
|
// * AggregationBuilders.dateHistogram("by_year").field("dateOfBirth").interval((DateHistogramInterval.YEAR))
|
|
|
// * .subAggregation(
|
|
|
// * AggregationBuilders.avg("avg_children").field("children")
|
|
|
// * )
|
|
|
// * );
|
|
|
// * @return 聚合结果集
|
|
|
// */
|
|
|
// 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);
|
|
|
// }
|
|
|
// SearchResponse response = searchRequestBuilder.execute().actionGet();
|
|
|
// Map<String, Aggregation> map = response.getAggregations().getAsMap();
|
|
|
// client.close();
|
|
|
// return map;
|
|
|
// }
|
|
|
|
|
|
/**
|
|
|
* 表格型 - 数据结果解析
|
|
@ -149,15 +149,8 @@ public class AggregationBuildHandler {
|
|
|
* @return
|
|
|
*/
|
|
|
public List<Map<String, Object>> tableDataParsing(View view,Map<String, Aggregation> map){
|
|
|
// List<String> rowList = new LinkedList<>();
|
|
|
// rowList.add("town_terms");
|
|
|
// rowList.add("total");
|
|
|
// List<String> cloumnList = new ArrayList<>();
|
|
|
// cloumnList.add("total_count_result");
|
|
|
// cloumnList.add("sex_terms");
|
|
|
List<String> rowList = getRowDimensionList(view.getId());
|
|
|
List<String> cloumnList = getColumnDimensionList(view.getId());
|
|
|
|
|
|
List<ViewQuota> viewQuotas = viewQuotaDao.findByViewId(view.getId());
|
|
|
Map<String, Object> quotaMap = new HashMap<>();
|
|
|
for(ViewQuota viewQuota :viewQuotas){
|
|
@ -282,32 +275,22 @@ public class AggregationBuildHandler {
|
|
|
* @param map 聚合查询结果
|
|
|
* @return
|
|
|
*/
|
|
|
public Option ehartDataParsing(View view,Map<String, Aggregation> map){
|
|
|
public Option ehartDataParsing(View view,Map<String, Aggregation> map) throws Exception{
|
|
|
Option option = null;
|
|
|
ReportOption reportOption = new ReportOption();
|
|
|
List<String> xDataList = new LinkedList<>();
|
|
|
List<String> charTypes = new LinkedList<>();
|
|
|
List<String> lineNames = new LinkedList<>();
|
|
|
List<List<Object>> lineDataList = new LinkedList<>();
|
|
|
|
|
|
List<String> rowList = new LinkedList<>();
|
|
|
rowList.add("town_terms");
|
|
|
// rowList = getRowDimensionList(view.getId());
|
|
|
List<String> rowList = getRowDimensionList(view.getId());
|
|
|
Map<String,List<Object>> quotasDataMap = new LinkedHashMap<>();
|
|
|
if(rowList.size() != 1){
|
|
|
logger.debug("图表型指标维度配置有误");
|
|
|
return null;
|
|
|
throw new Exception("图表型指标维度配置有误");
|
|
|
}
|
|
|
String rowAggCode = rowList.get(0);
|
|
|
|
|
|
List<String> quotaList = new LinkedList<>();
|
|
|
Map<String,List<Object>> quotasDataMap = new LinkedHashMap<>();
|
|
|
List<ViewQuota> viewQuotas = new ArrayList<>();
|
|
|
ViewQuota vq = new ViewQuota();
|
|
|
vq.setChartType("1");
|
|
|
vq.setCode("total");
|
|
|
vq.setName("各区县总人数");
|
|
|
viewQuotas.add(vq);
|
|
|
// viewQuotas = viewQuotaDao.findByViewId(view.getId());
|
|
|
List<ViewQuota> viewQuotas = viewQuotaDao.findByViewId(view.getId());
|
|
|
for(ViewQuota viewQuota :viewQuotas){
|
|
|
quotaList.add(viewQuota.getCode());
|
|
|
charTypes.add(viewQuota.getChartType());
|
|
@ -318,14 +301,14 @@ public class AggregationBuildHandler {
|
|
|
Terms terms = (Terms)aggregation;
|
|
|
String key = "";
|
|
|
List<Terms.Bucket> buckets = terms.getBuckets();
|
|
|
for(String quotaCode: quotaList){
|
|
|
for(ViewQuota viewQuota: viewQuotas){
|
|
|
List<Object> quotaData = new ArrayList<>();
|
|
|
for(Terms.Bucket bucket : buckets){
|
|
|
key = bucket.getKeyAsString() != null ? bucket.getKeyAsString() : bucket.getKey().toString();
|
|
|
xDataList.add(key);
|
|
|
List<Aggregation> aggregationList = bucket.getAggregations().asList();
|
|
|
for(Aggregation subAgg : aggregationList){
|
|
|
if(subAgg.getName().contains(quotaCode)){
|
|
|
if(subAgg.getName().contains(viewQuota.getBasicFormulaType())){
|
|
|
Map<String, Object> subDataMap = getInternalAggValue(subAgg.getName(),subAgg);
|
|
|
if(subDataMap != null || subDataMap.size() > 0){
|
|
|
quotaData.add(subDataMap.get(subAgg.getName()));
|
|
@ -333,7 +316,7 @@ public class AggregationBuildHandler {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
quotasDataMap.put(quotaCode, quotaData);
|
|
|
quotasDataMap.put(viewQuota.getCode(), quotaData);
|
|
|
}
|
|
|
}
|
|
|
for (Map.Entry<String,List<Object>> entry : quotasDataMap.entrySet()) {
|