之前写的有问题,修改一下取值范围,并且解析数据时如果x轴为空时需要用0占位,不然生成的图表柱状图会错位
两个维度聚合,并且取数量前10的地区
年龄:20以下, 21-30, 31-40, 41-50, 51-60, 60以上 聚合
现加入年龄未知,存数据时存 -1
//注意:range时 to为开区间 from为闭区间
private AggregationBuilder getAggChartB(String termOne, String termTwo) {
AggregationBuilder termX =null;
AggregationBuilder termY =null;
if ("age".equals(termOne)){
AggregationBuilder age = AggregationBuilders.range("termX")
.field("age").addUnboundedTo(0).addRange(0,20).addRange(20,31).addRange(31,41).addRange(41,51).addRange(51,61).addUnboundedFrom(61);
termX = age;
}else{
if ("location".equals(termOne)) {
termX = AggregationBuilders.terms("termX").field(termOne).size(100000).order(BucketOrder.count(false)).size(10);
}else{
termX = AggregationBuilders.terms("termX").field(termOne).size(100000);
}
}
if ("age".equals(termTwo)){
AggregationBuilder age = AggregationBuilders.range("termY")
.field("age").addUnboundedTo(0).addRange(0,20).addRange(20,31).addRange(31,41).addRange(41,51).addRange(51,61).addUnboundedFrom(61);
termY = age;
}else{
if ("location".equals(termTwo)) {
termY = AggregationBuilders.terms("termY").field(termTwo).size(100000).order(BucketOrder.count(false)).size(10);
}else{
termY = AggregationBuilders.terms("termY").field(termTwo).size(100000);
}
}
return termX.subAggregation(termY);
}
结果解析:
结果有4种情况:
维度1--维度2
age--age
age--!age
!age--age
!age--!age
//拼接聚合条件
AggregationBuilder termsBuilder = getAggChartB(chartBX,chartBY);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//拼查询条件
sourceBuilder.query(queryBuilder)
.size(10000)
.aggregation(termsBuilder);
SearchRequest searchRequest = new SearchRequest(Constant.CUSTOM_BEHAVIOR_ES_INDEX)
.types(Constant.TABLE_OF_SEAT_QUALITY_TYPE)
.searchType(SearchType.QUERY_THEN_FETCH)
.source(sourceBuilder);
//查询
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregations aggregations = response.getAggregations();
if ("age".equals(chartBX)&&"age".equals(chartBY)){
ParsedRange termX = aggregations.get("termX");
//存放 每个第二级 的 x,y 用来拼接数据
JSONArray jsonArrayKeyValue = new JSONArray();
//存放第二级的x轴
JSONArray jsonArrayKey = new JSONArray();
for (Range.Bucket bucket : termX.getBuckets()) {
String keyX = bucket.getKeyAsString();
if ("*-0.0".equals(keyX)) {
keyX = "未知";
}else if("0.0-20.0".equals(keyX)){
keyX = "20岁以下";
}else if("20.0-31.0".equals(keyX)){
keyX = "20-30";
}else if("31.0-41.0".equals(keyX)){
keyX = "31-40";
}else if("41.0-51.0".equals(keyX)){
keyX = "41-50";
}else if("51.0-61.0".equals(keyX)){
keyX = "51-60";
}else if ("61.0-*".equals(keyX)){
keyX = "60岁以上";
}
jsonArrX.add(keyX);
Range termY = bucket.getAggregations().get("termY");
Map<String, Object> hashMap = new HashMap<>();
for (Range.Bucket termYBucket : termY.getBuckets()) {
String key = termYBucket.getKeyAsString();
if ("*-0.0".equals(key)) {
key = "未知";
}else if("0.0-20.0".equals(key)){
key = "20岁以下";
}else if("20.0-31.0".equals(key)){
key = "20-30";
}else if("31.0-41.0".equals(key)){
key = "31-40";
}else if("41.0-51.0".equals(key)){
key = "41-50";
}else if("51.0-61.0".equals(key)){
key = "51-60";
}else if ("61.0-*".equals(key)){
key = "60岁以上";
}
long count = termYBucket.getDocCount();
//存 第二级 的 x轴,y轴
hashMap.put(key, count);
if (!jsonArrayKey.contains(key)){
jsonArrayKey.add(key);
}
}
jsonArrayKeyValue.add(hashMap);
}
for (Object o : jsonArrayKey) {
Map<String, Object> hashMap = new HashMap<>();
//存二级value
JSONArray jsonArray = new JSONArray();
for (Object o1 : jsonArrayKeyValue) {
JSONObject keyValueMap = JSONObject.fromObject(o1);
if (keyValueMap.containsKey(o.toString())) {
int key = keyValueMap.getInt(o.toString());
jsonArray.add(key);
}else{
jsonArray.add(0);
}
}
hashMap.put("name",o.toString());
hashMap.put("data",jsonArray);
jsonArrData.add(hashMap);
jsonArrY.add(o.toString());
}
} else if ("age".equals(chartBX)&&!"age".equals(chartBY)) {
ParsedRange termX = aggregations.get("termX");
//存放 每个第二级 的 x,y 用来拼接数据
JSONArray jsonArrayKeyValue = new JSONArray();
//存放第二级的x轴
JSONArray jsonArrayKey = new JSONArray();
for (Range.Bucket bucket : termX.getBuckets()) {
String keyX = bucket.getKeyAsString();
if ("*-0.0".equals(keyX)) {
keyX = "未知";
}else if("0.0-20.0".equals(keyX)){
keyX = "20岁以下";
}else if("20.0-31.0".equals(keyX)){
keyX = "20-30";
}else if("31.0-41.0".equals(keyX)){
keyX = "31-40";
}else if("41.0-51.0".equals(keyX)){
keyX = "41-50";
}else if("51.0-61.0".equals(keyX)){
keyX = "51-60";
}else if ("61.0-*".equals(keyX)){
keyX = "60岁以上";
}
jsonArrX.add(keyX);
Terms termY = bucket.getAggregations().get("termY");
Map<String, Object> hashMap = new HashMap<>();
for (Terms.Bucket termYBucket : termY.getBuckets()) {
String key = termYBucket.getKeyAsString();
long count = termYBucket.getDocCount();
//存 第二级 的 x轴,y轴
hashMap.put(key, count);
if (!jsonArrayKey.contains(key)){
jsonArrayKey.add(key);
}
}
jsonArrayKeyValue.add(hashMap);
}
for (Object o : jsonArrayKey) {
Map<String, Object> hashMap = new HashMap<>();
//存二级value
JSONArray jsonArray = new JSONArray();
for (Object o1 : jsonArrayKeyValue) {
JSONObject keyValueMap = JSONObject.fromObject(o1);
if (keyValueMap.containsKey(o.toString())) {
int key = keyValueMap.getInt(o.toString());
jsonArray.add(key);
}else{
jsonArray.add(0);
}
}
hashMap.put("name","".equals(o.toString())?"未知":o.toString());
hashMap.put("data",jsonArray);
jsonArrData.add(hashMap);
jsonArrY.add("".equals(o.toString())?"未知":o.toString());
}
}else if (!"age".equals(chartBX)&&"age".equals(chartBY)){
Terms termX = aggregations.get("termX");
//存放 每个第二级 的 x,y 用来拼接数据
JSONArray jsonArrayKeyValue = new JSONArray();
//存放第二级的x轴
JSONArray jsonArrayKey = new JSONArray();
for (Terms.Bucket bucket : termX.getBuckets()) {
String keyX = bucket.getKeyAsString();
jsonArrX.add("".equals(keyX)?"未知":keyX);
Range termY = bucket.getAggregations().get("termY");
Map<String, Object> hashMap = new HashMap<>();
for (Range.Bucket termYBucket : termY.getBuckets()) {
String key = termYBucket.getKeyAsString();
if ("*-0.0".equals(key)) {
key = "未知";
}else if("0.0-20.0".equals(key)){
key = "20岁以下";
}else if("20.0-31.0".equals(key)){
key = "20-30";
}else if("31.0-41.0".equals(key)){
key = "31-40";
}else if("41.0-51.0".equals(key)){
key = "41-50";
}else if("51.0-61.0".equals(key)){
key = "51-60";
}else if ("61.0-*".equals(key)){
key = "60岁以上";
}
long count = termYBucket.getDocCount();
//存 第二级 的 x轴,y轴
hashMap.put(key, count);
if (!jsonArrayKey.contains(key)){
jsonArrayKey.add(key);
}
}
jsonArrayKeyValue.add(hashMap);
}
for (Object o : jsonArrayKey) {
Map<String, Object> hashMap = new HashMap<>();
//存二级value
JSONArray jsonArray = new JSONArray();
for (Object o1 : jsonArrayKeyValue) {
JSONObject keyValueMap = JSONObject.fromObject(o1);
if (keyValueMap.containsKey(o.toString())) {
int key = keyValueMap.getInt(o.toString());
jsonArray.add(key);
}else{
jsonArray.add(0);
}
}
hashMap.put("name","".equals(o.toString())?"未知":o.toString());
hashMap.put("data",jsonArray);
jsonArrData.add(hashMap);
jsonArrY.add("".equals(o.toString())?"未知":o.toString());
}
}else {
Terms termX = aggregations.get("termX");
//存放 每个第二级 的 x,y 用来拼接数据
JSONArray jsonArrayKeyValue = new JSONArray();
//存放第二级的x轴
JSONArray jsonArrayKey = new JSONArray();
for (Terms.Bucket bucket : termX.getBuckets()) {
jsonArrX.add("".equals(bucket.getKeyAsString())?"未知":bucket.getKeyAsString());
Terms termY = bucket.getAggregations().get("termY");
Map<String, Object> hashMap = new HashMap<>();
for (Terms.Bucket termYBucket : termY.getBuckets()) {
String key = termYBucket.getKeyAsString();
long count = termYBucket.getDocCount();
//存 第二级 的 x轴,y轴
hashMap.put(key, count);
if (!jsonArrayKey.contains(key)){
jsonArrayKey.add(key);
}
}
jsonArrayKeyValue.add(hashMap);
}
//拼接 echarts所需数据结构
// [{name:'key1',type:'bar',data:[1,10,20,30]},{name:'key2',type:'bar',data:[1,10,20,30]}]
for (Object o : jsonArrayKey) {
Map<String, Object> hashMap = new HashMap<>();
//存二级value
JSONArray jsonArray = new JSONArray();
for (Object o1 : jsonArrayKeyValue) {
JSONObject keyValueMap = JSONObject.fromObject(o1);
if (keyValueMap.containsKey(o.toString())) {
int key = keyValueMap.getInt(o.toString());
jsonArray.add(key);
}else{
jsonArray.add(0);
}
}
hashMap.put("name","".equals(o.toString())?"未知":o.toString());
hashMap.put("data",jsonArray);
jsonArrData.add(hashMap);
jsonArrY.add("".equals(o.toString())?"未知":o.toString());
}
}
dataS.put("data", jsonArrData);
dataS.put("dataX", jsonArrX);
dataS.put("dataY", jsonArrY);