|
@ -5,13 +5,18 @@ import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.client.transport.TransportClient;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.*;
|
|
|
import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;
|
|
|
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.stereotype.Component;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Created by janseny on 2018/9/28.
|
|
@ -103,19 +108,99 @@ public class AggregationBuildHandler {
|
|
|
for(AbstractAggregationBuilder aggBuilder : aggBuilderList){
|
|
|
searchRequestBuilder.addAggregation(aggBuilder);
|
|
|
}
|
|
|
SearchResponse sr = searchRequestBuilder.execute().actionGet();
|
|
|
List<Map<String, Object>> matchRsult = new LinkedList<Map<String, Object>>();
|
|
|
|
|
|
//数据解析 TODO
|
|
|
// ValueCount sum = sr.getAggregations().get("count_result");
|
|
|
// double v = sum.getValue();
|
|
|
SearchResponse response = searchRequestBuilder.execute().actionGet();
|
|
|
List<Map<String, Object>> resultList = new LinkedList<Map<String, Object>>();
|
|
|
Map<String, Aggregation> map = response.getAggregations().getAsMap();
|
|
|
for(String key:map.keySet()){
|
|
|
Aggregation aggregation = map.get(key);
|
|
|
parsingAggregation(resultList,aggregation);
|
|
|
}
|
|
|
client.close();
|
|
|
return matchRsult;
|
|
|
return resultList;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 递归解析 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(valueCount.getName() + ":"+ 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(), "");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|