数据可视化 数据多维度检索 多维度统计

数据可视化 数据多维度检索 多维度统计_第1张图片
数据可视化 数据多维度检索 多维度统计_第2张图片后续功能实现完整后会记录下来 采用es + vue + echarts来实现

最近在写多维度统计 该功能的设计思路是只要有业务数据想根据不同维度来统计,可以把mysql或其他数据源的数据按照维度写入到es的index中,然后通过配置一些过滤条件项,和分享项,即可完成各种范围统计

页面效果如下:

数据可视化 数据多维度检索 多维度统计_第3张图片

java代码如下:

package com.fuxin.dataanalysis.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fuxin.dataanalysis.bean.base.NameValue;
import com.fuxin.dataanalysis.bean.db.TSearchAggsItem;
import com.fuxin.dataanalysis.bean.dto.BaseIndexSummaryDTO;
import com.fuxin.dataanalysis.bean.dto.MultAnalysisResultDTO;
import com.fuxin.dataanalysis.bean.req.BaseIndexConditonReqDTO;
import com.fuxin.dataanalysis.bean.req.MultStatisReqDTO;
import com.fuxin.dataanalysis.bean.req.UserDistributionConditionReqDTO;
import com.fuxin.dataanalysis.enums.AggTypeEnum;
import com.fuxin.dataanalysis.mapper.TSearchAggsItemMapper;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedLongTerms;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Classname EsStatisticsService
 * @Description
 * @Date 2022/1/11 13:11
 * @Created gengxiao-
 */
@Slf4j
@Service
public class EsStatisticsService {

    @Resource
    RestHighLevelClient restHighLevelClient;

    @Resource
    TSearchAggsItemMapper tSearchAggsItemMapper;


    /**
     *  多维度统计
     * */
    public MultAnalysisResultDTO multAnalysis(MultStatisReqDTO multStatisReqDTO) throws IOException {
        //组装条件请求es服务器 拿到response结果
        SearchResponse response = doMultAnalysis(multStatisReqDTO);
        //构建表格头
        HashMap<String, NameValue<Integer>> tableHeards  = buildTableHeader(multStatisReqDTO.getGroupConditions());
        MultAnalysisResultDTO multAnalysisResultDTO = new MultAnalysisResultDTO();
        List<Map<String,String>> tableBody = convertResponseToTable(multStatisReqDTO,response);
        multAnalysisResultDTO.setTableBodyMap(tableBody);
        List<MultAnalysisResultDTO.TableMergeCondition> tableMergeConditions = new ArrayList<>();
        //构建合并条件
        multAnalysisResultDTO.setTableMergeConditonList(tableMergeConditions);
        buildRowMergeConditions(multStatisReqDTO,tableHeards,multAnalysisResultDTO);
        multAnalysisResultDTO.setTableHeaderMap(tableHeards);
        Map<Integer, List<MultAnalysisResultDTO.TableMergeCondition>> collect = tableMergeConditions.stream().collect(Collectors.groupingBy(MultAnalysisResultDTO.TableMergeCondition::getMergeColumnIndex));
        Collection<List<MultAnalysisResultDTO.TableMergeCondition>> values = collect.values();
        multAnalysisResultDTO.setCollectionList(values);
        return multAnalysisResultDTO;
    }

    private void buildRowMergeConditions(MultStatisReqDTO multStatisReqDTO, HashMap<String, NameValue<Integer>> tableHeards, MultAnalysisResultDTO multAnalysisResultDTO) {
        List<MultAnalysisResultDTO.TableMergeCondition> tableMergeConditions = multAnalysisResultDTO.getTableMergeConditonList();
        //表头
        //key列名 value.key 列名 value.value 列序号
        //根据列下标索引来拿列
        Map<Integer,String> tableHeaderMapByIdx = new HashMap<>();
        for (String columnName : tableHeards.keySet()) { tableHeaderMapByIdx.put(tableHeards.get(columnName).getValue(),columnName); }
        //列表数据
        List<Map<String, String>> tableBodyMap = multAnalysisResultDTO.getTableBodyMap();
        //根据列来驱动行 列是按照顺序排序好的 采用LinkedHashMap
        for (String columnName : tableHeards.keySet()) {
            //当前列
            NameValue<Integer> curColumn = tableHeards.get(columnName);
            MultAnalysisResultDTO.TableMergeCondition tableMergeCondition = new MultAnalysisResultDTO.TableMergeCondition();
            //当前列的数据
            tableMergeCondition.setMergeColumnIndex(curColumn.getValue()); //设置当前列下标
            tableMergeCondition.setStartRowIndex(0); //默认为0 第一行
            Map<String,String> prevTableItem = new HashMap<>();
            for (int i = 0; i < tableBodyMap.size(); i++) {
                Map<String, String> tableItem = tableBodyMap.get(i);
                if(i == 0){
                    prevTableItem = tableItem;
                    continue; //第一行忽略
                }
                //判断是否要截断 我们会比较三个字段
                boolean truncate = false;
                //当前列索引
                Integer curColumnIdx = curColumn.getValue();

                Integer pColumnIdex = curColumnIdx;
                String  pColumnIdexName = tableHeaderMapByIdx.get(pColumnIdex);

                Integer ppColumnIdex = curColumnIdx-1;
                String  ppColumnIdexName = tableHeaderMapByIdx.get(ppColumnIdex);

                Integer pppColumnIdex = curColumnIdx-2;
                String  pppColumnIdexName = tableHeaderMapByIdx.get(pppColumnIdex);

                Integer ppppColumnIdex = curColumnIdx-3;
                String  ppppColumnIdexName = tableHeaderMapByIdx.get(ppppColumnIdex);


                if(!StringUtils.isEmpty(pColumnIdexName)){
                    if(!prevTableItem.get(pColumnIdexName).equals(tableItem.get(pColumnIdexName))){
                        truncate = true;
                    }
                }

                if(!StringUtils.isEmpty(ppColumnIdexName)){
                    if(!prevTableItem.get(ppColumnIdexName).equals(tableItem.get(ppColumnIdexName))){
                        truncate = true;
                    }
                }

                if(!StringUtils.isEmpty(pppColumnIdexName)){
                    if(!prevTableItem.get(pppColumnIdexName).equals(tableItem.get(pppColumnIdexName))){
                        truncate = true;
                    }
                }

                if(!StringUtils.isEmpty(ppppColumnIdexName)){
                    if(!prevTableItem.get(ppppColumnIdexName).equals(tableItem.get(ppppColumnIdexName))){
                        truncate = true;
                    }
                }

                //截断
                if(truncate){
                    tableMergeCondition.setMergeRowLength(i);
                    tableMergeConditions.add(tableMergeCondition);

                    tableMergeCondition = new MultAnalysisResultDTO.TableMergeCondition();
                    tableMergeCondition.setMergeColumnIndex(curColumn.getValue());
                    tableMergeCondition.setStartRowIndex(i);

                    prevTableItem = tableItem;

                    continue;
                }

            }

        }

    }

    private HashMap<String, NameValue<Integer>> buildTableHeader(List<MultStatisReqDTO.GroupCondition> groupConditions) {
        HashMap<String, NameValue<Integer>> tableHeards = new LinkedHashMap<>();
        int count = 0;
        for (MultStatisReqDTO.GroupCondition groupCondition : groupConditions) {
            String name = groupCondition.getAggFiled();
            tableHeards.put(name,new NameValue<Integer>(name,count++ ));
            tableHeards.put(name+"Count",new NameValue<Integer>(name+"Count",count++ ));
        }
        return tableHeards;
    }

    private List<Map<String,String>> convertResponseToTable(MultStatisReqDTO multStatisReqDTO,SearchResponse response) {
        List<Map<String,String>> tableBodyMap = new ArrayList<>();
        MultStatisReqDTO.GroupCondition parentBucket = multStatisReqDTO.getGroupConditions().get(0);
        //写一个递归呢得哈哈
        ParsedStringTerms parsedStringTerms = (ParsedStringTerms) response.getAggregations().get(parentBucket.getAggFiled() + "_bucket");
        for (Terms.Bucket bucket : parsedStringTerms.getBuckets()) {
            buildItem(multStatisReqDTO.getGroupConditions(),0,bucket,tableBodyMap,null,new HashMap<>());
        }
        return tableBodyMap;
    }

    private SearchResponse doMultAnalysis(MultStatisReqDTO multStatisReqDTO) throws IOException {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        SearchRequest searchRequest = new SearchRequest(multStatisReqDTO.getIndexName());
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder);
        List<MultStatisReqDTO.FilterCondition> filterConditions = multStatisReqDTO.getFilterConditions();
        for (MultStatisReqDTO.FilterCondition filterCondition : filterConditions) {
            boolQueryBuilder.filter(QueryBuilders.termQuery(filterCondition.getFiledName(),filterCondition.getValue()));
        }
        //多级聚合
        List<MultStatisReqDTO.GroupCondition> groupConditions = multStatisReqDTO.getGroupConditions();
        AggregationBuilder pAgg = null;
        AggregationBuilder last = null;
        for (MultStatisReqDTO.GroupCondition groupCondition : groupConditions) {


            TSearchAggsItem tSearchAggsItem = tSearchAggsItemMapper.selectById(groupCondition.getSearchAggsItemsId());


            //考虑聚合类型

            if(pAgg == null){
                pAgg = buildAggItem(tSearchAggsItem);
                last = pAgg;
            }else{
                AggregationBuilder agg = buildAggItem(tSearchAggsItem);
                last.subAggregation(agg);
                last = agg;
            }
        }
        searchSourceBuilder.aggregation(pAgg);
        searchRequest.source(searchSourceBuilder);
        //结果集解析
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        return response;
    }

    public AggregationBuilder buildAggItem(TSearchAggsItem groupCondition){
        AggregationBuilder agg = null;
        switch (AggTypeEnum.getByValue(groupCondition.getGroupType())){
            case DATE:
                agg = AggregationBuilders.terms(groupCondition.getGroupValue() + "_bucket")
                        .field(groupCondition.getGroupValue()).format(JSON.parseObject(groupCondition.getRuleJson()).getString("template"));
                break;
            case TERM:
                agg = AggregationBuilders.terms(groupCondition.getGroupValue() + "_bucket")
                        .field(groupCondition.getGroupValue());
                break;
            case DATE_RANGE:
                agg = AggregationBuilders.terms(groupCondition.getGroupValue() + "_bucket")
                        .field(groupCondition.getGroupValue()).format(JSON.parseObject(groupCondition.getRuleJson()).getString("template"));
                break;
            case INT_RANGE:
                agg = AggregationBuilders.range(groupCondition.getGroupValue() + "_bucket");
                AggregationBuilders.range(groupCondition.getGroupValue() + "_bucket");
                String aggTypeRuleJson = groupCondition.getRuleJson();
                JSONArray jsonObject = JSON.parseArray(aggTypeRuleJson);
                for (Object o : jsonObject) {
                    JSONObject js = (JSONObject) o;
                    int flag = -1;
                    if(js.get("from") != null){
                        flag = 1;
                    }
                    if(js.get("to") != null){
                        flag = 2;
                    }
                    if(js.get("from") != null && js.get("to") != null){
                        flag = 3;
                    }
                    if(flag ==1 ){
                        ((RangeAggregationBuilder) agg).addUnboundedFrom(Double.parseDouble(js.getString("from")));
                    }else if(flag == 2){
                        ((RangeAggregationBuilder) agg).addUnboundedTo(Double.parseDouble(js.getString("to")));
                    }else if (flag == 3){
                        ((RangeAggregationBuilder) agg).addRange(Double.parseDouble(js.getString("from")),Double.parseDouble(js.getString("to")));
                    }
                }
                break;
            default:
                throw new RuntimeException("聚合类型不存在,请联系系统管理员核对!");
        }

        return agg;

    }

    public void buildItem(List<MultStatisReqDTO.GroupCondition> groupConditions, int level, Terms.Bucket bucket,
                          List<Map<String,String>> tableBody,
                          Map<String, NameValue<String>> cloneParents,
                          Map<String, NameValue<String>> parents){
        //当前桶
        Aggregations aggregations = bucket.getAggregations();
        //parent
        String keys = bucket.getKeyAsString();
        parents.put(groupConditions.get(level).getAggFiled(),new NameValue<>(keys,String.valueOf(bucket.getDocCount())));
        //children
        Iterator<Aggregation> iterator = aggregations.iterator();
        if(iterator.hasNext()){
            while (iterator.hasNext()){
                //TODO 这么写有问题
                //TODO 这块可以往父类靠
                Aggregation next = iterator.next();
                List<? extends Terms.Bucket> buckets = new ArrayList<>();
                if(next instanceof ParsedStringTerms){
                    buckets = ((ParsedStringTerms)next).getBuckets();
                }else if(next instanceof ParsedLongTerms){
                    buckets = ((ParsedLongTerms)next).getBuckets();
                }
                //这块的逻辑比较重要
                for (Terms.Bucket nextBucket : buckets) {
                    //爸父级的数据拷贝到cloneMap里面
                    HashMap<String, NameValue<String>> cloneMap = new HashMap<>();
                    for (String s : parents.keySet()) {
                        cloneMap.put(s,parents.get(s));
                    }
                    //记录当前数据
                    //groupConditions.get(level+1) 这个含义是拿到具体层的属性
                    cloneMap.put(groupConditions.get(level+1).getAggFiled(),new NameValue<String>(nextBucket.getKeyAsString(),String.valueOf(nextBucket.getDocCount())));
                    //看看当前的桶中是否还有子桶
                    buildItem(groupConditions,level+1,nextBucket,tableBody,parents,cloneMap);
                }
            }
        }else{
            //TODO 考虑getKeyAsString和getKey有啥区别 单纯的使用getKeyAsString是否可以
            //构建列表行记录
            HashMap<String, String> itemMap = new HashMap<>();
            for (String key : cloneParents.keySet()) {
                itemMap.put(key,cloneParents.get(key).getName());
                itemMap.put(key+"Count",cloneParents.get(key).getValue());
            }
            itemMap.put(groupConditions.get(level).getAggFiled(),bucket.getKeyAsString());
            itemMap.put(groupConditions.get(level).getAggFiled() + "Count",String.valueOf(bucket.getDocCount()));
            tableBody.add(itemMap);
            return;
        }
    }



    /**
     * 用户省份人员分布
     * */
    public List<Map<String,String>> getUserDistribution(UserDistributionConditionReqDTO userDistributionConditionReqDTO) throws IOException {

        if(userDistributionConditionReqDTO.getLevel() == 1){
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            AggregationBuilder groupByType = AggregationBuilders.terms("province_bucket").field("homeProvince");
            SearchRequest searchRequest = new SearchRequest("customer_all_info5");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(boolQueryBuilder);
            searchSourceBuilder.aggregation(groupByType);
            searchRequest.source(searchSourceBuilder);
            SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            ParsedStringTerms parsed = (ParsedStringTerms)response.getAggregations().get("province_bucket");
            List<? extends Terms.Bucket> buckets = parsed.getBuckets();
            return buckets.stream().map(item -> {
                HashMap<String, String> map = new HashMap<>();
                map.put("name",(String)((Terms.Bucket) item).getKey());
                map.put("value",String.valueOf(((Terms.Bucket) item).getDocCount()));
                return map;
            }).collect(Collectors.toList());
        }else if(userDistributionConditionReqDTO.getLevel() == 2){
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",userDistributionConditionReqDTO.getProvince()));
            AggregationBuilder groupByType = AggregationBuilders.terms("city_bucket").field("homeCity");
            SearchRequest searchRequest = new SearchRequest("customer_all_info5");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(boolQueryBuilder);
            searchSourceBuilder.aggregation(groupByType);
            searchRequest.source(searchSourceBuilder);
            SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            ParsedStringTerms parsed = (ParsedStringTerms)response.getAggregations().get("city_bucket");
            List<? extends Terms.Bucket> buckets = parsed.getBuckets();
            return buckets.stream().map(item -> {
                HashMap<String, String> map = new HashMap<>();
                map.put("name",(String)((Terms.Bucket) item).getKey());
                map.put("value",String.valueOf(((Terms.Bucket) item).getDocCount()));
                return map;
            }).collect(Collectors.toList());
        }else{
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",userDistributionConditionReqDTO.getProvince()));
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeCity",userDistributionConditionReqDTO.getCity()));

            AggregationBuilder groupByType = AggregationBuilders.terms("county_bucket").field("homeCounty.keyword");
            SearchRequest searchRequest = new SearchRequest("customer_all_info5");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(boolQueryBuilder);
            searchSourceBuilder.aggregation(groupByType);
            searchRequest.source(searchSourceBuilder);
            SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            ParsedStringTerms parsed = (ParsedStringTerms)response.getAggregations().get("county_bucket");
            List<? extends Terms.Bucket> buckets = parsed.getBuckets();
            return buckets.stream().map(item -> {
                HashMap<String, String> map = new HashMap<>();
                map.put("name",(String)((Terms.Bucket) item).getKey());
                map.put("value",String.valueOf(((Terms.Bucket) item).getDocCount()));
                return map;
            }).collect(Collectors.toList());
        }
    }

    public List<Map<String,String>> getUserDistributionByProvince() throws IOException {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        AggregationBuilder groupByType = AggregationBuilders.terms("province_bucket").field("homeProvince");
        SearchRequest searchRequest = new SearchRequest("customer_all_info5");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.aggregation(groupByType);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        ParsedStringTerms parsed = (ParsedStringTerms)response.getAggregations().get("province_bucket");
        List<? extends Terms.Bucket> buckets = parsed.getBuckets();
        return buckets.stream().map(item -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("name",(String)((Terms.Bucket) item).getKey());
            map.put("value",String.valueOf(((Terms.Bucket) item).getDocCount()));
            return map;
        }).collect(Collectors.toList());
    }

    public List<Map<String,String>> getUserDistributionByCity() throws IOException {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        AggregationBuilder groupByType = AggregationBuilders.terms("province_bucket").field("homeProvince");
        SearchRequest searchRequest = new SearchRequest("customer_all_info5");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.aggregation(groupByType);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        ParsedStringTerms parsed = (ParsedStringTerms)response.getAggregations().get("province_bucket");
        List<? extends Terms.Bucket> buckets = parsed.getBuckets();
        return buckets.stream().map(item -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("name",(String)((Terms.Bucket) item).getKey());
            map.put("value",String.valueOf(((Terms.Bucket) item).getDocCount()));
            return map;
        }).collect(Collectors.toList());
    }

    /**
     * 获取基本指标数据 根据国家或省或城市
     * */
    public BaseIndexSummaryDTO getBaseIndexByLevel(BaseIndexConditonReqDTO baseIndexConditonReqDTO) throws IOException {
//        Integer level = baseIndexConditonReqDTO.getLevel();
        //level = 0  是统计全国的
        //level == 1 是统计某个省份的
        //level == 2 是统计某个城市的
        return BaseIndexSummaryDTO.builder()
                .sexChart(buildSexChart(baseIndexConditonReqDTO))
                .degreeChart(buildDegreeChart(baseIndexConditonReqDTO))
                .workTypeChart(buildWorkTypeChart(baseIndexConditonReqDTO))
                .marriageChart(buildMarriageChart(baseIndexConditonReqDTO))
                .nationChart(buildNationChart(baseIndexConditonReqDTO))
                .memberLevelChart(buildMemberLevelChart(baseIndexConditonReqDTO))
                .build();
    }

    private List<Map<String,String>> buildSexChart(BaseIndexConditonReqDTO baseIndexConditonReqDTO) throws IOException {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        if(baseIndexConditonReqDTO.getLevel() == 2){
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",baseIndexConditonReqDTO.getProvince()));
        }else if(baseIndexConditonReqDTO.getLevel() == 3){
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",baseIndexConditonReqDTO.getProvince()))
                    .filter(QueryBuilders.termQuery("homeCity",baseIndexConditonReqDTO.getCity()));
        }
        AggregationBuilder groupByType = AggregationBuilders.terms("sex_bucket").field("sex");
        SearchRequest searchRequest = new SearchRequest("customer_all_info5");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.aggregation(groupByType);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        ParsedStringTerms parsed = (ParsedStringTerms)response.getAggregations().get("sex_bucket");
        List<? extends Terms.Bucket> buckets = parsed.getBuckets();
        return buckets.stream().map(item -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("name",(String)((Terms.Bucket) item).getKey());
            map.put("value",String.valueOf(((Terms.Bucket) item).getDocCount()));
            return map;
        }).collect(Collectors.toList());

    }

    private List<Map<String,String>> buildDegreeChart(BaseIndexConditonReqDTO baseIndexConditonReqDTO) throws IOException {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        if(baseIndexConditonReqDTO.getLevel() == 2){
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",baseIndexConditonReqDTO.getProvince()));
        }else if(baseIndexConditonReqDTO.getLevel() == 3){
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",baseIndexConditonReqDTO.getProvince()))
                    .filter(QueryBuilders.termQuery("homeCity",baseIndexConditonReqDTO.getCity()));
        }
        AggregationBuilder groupByType = AggregationBuilders.terms("degree_bucket").field("degree");
        SearchRequest searchRequest = new SearchRequest("customer_all_info5");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.aggregation(groupByType);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        ParsedStringTerms parsed = (ParsedStringTerms)response.getAggregations().get("degree_bucket");
        List<? extends Terms.Bucket> buckets = parsed.getBuckets();
        return buckets.stream().map(item -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("name",(String)((Terms.Bucket) item).getKey());
            map.put("value",String.valueOf(((Terms.Bucket) item).getDocCount()));
            return map;
        }).collect(Collectors.toList());
    }

    private List<Map<String,String>> buildWorkTypeChart(BaseIndexConditonReqDTO baseIndexConditonReqDTO) throws IOException {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        if(baseIndexConditonReqDTO.getLevel() == 2){
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",baseIndexConditonReqDTO.getProvince()));
        }else if(baseIndexConditonReqDTO.getLevel() == 3){
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",baseIndexConditonReqDTO.getProvince()))
                    .filter(QueryBuilders.termQuery("homeCity",baseIndexConditonReqDTO.getCity()));
        }
        AggregationBuilder groupByType = AggregationBuilders.terms("work_type_bucket").field("workType");
        SearchRequest searchRequest = new SearchRequest("customer_all_info5");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.aggregation(groupByType);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        ParsedStringTerms parsed = (ParsedStringTerms)response.getAggregations().get("work_type_bucket");
        List<? extends Terms.Bucket> buckets = parsed.getBuckets();
        return buckets.stream().map(item -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("name",(String)((Terms.Bucket) item).getKey());
            map.put("value",String.valueOf(((Terms.Bucket) item).getDocCount()));
            return map;
        }).collect(Collectors.toList());
    }

    private List<Map<String,String>> buildNationChart(BaseIndexConditonReqDTO baseIndexConditonReqDTO) throws IOException {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if(baseIndexConditonReqDTO.getLevel() == 2){
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",baseIndexConditonReqDTO.getProvince()));
        }else if(baseIndexConditonReqDTO.getLevel() == 3){
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",baseIndexConditonReqDTO.getProvince()))
                    .filter(QueryBuilders.termQuery("homeCity",baseIndexConditonReqDTO.getCity()));
        }
        AggregationBuilder groupByType = AggregationBuilders.terms("nation_bucket").field("nationality");
        SearchRequest searchRequest = new SearchRequest("customer_all_info5");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.aggregation(groupByType);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        ParsedStringTerms parsed = (ParsedStringTerms)response.getAggregations().get("nation_bucket");
        List<? extends Terms.Bucket> buckets = parsed.getBuckets();
        return buckets.stream().map(item -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("name",(String)((Terms.Bucket) item).getKey());
            map.put("value",String.valueOf(((Terms.Bucket) item).getDocCount()));
            return map;
        }).collect(Collectors.toList());
    }

    private List<Map<String,String>> buildMarriageChart(BaseIndexConditonReqDTO baseIndexConditonReqDTO) throws IOException {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if(baseIndexConditonReqDTO.getLevel() == 2){
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",baseIndexConditonReqDTO.getProvince()));
        }else if(baseIndexConditonReqDTO.getLevel() == 3){
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",baseIndexConditonReqDTO.getProvince()))
                    .filter(QueryBuilders.termQuery("homeCity",baseIndexConditonReqDTO.getCity()));
        }
        AggregationBuilder groupByType = AggregationBuilders.terms("marriage_bucket").field("marriage");
        SearchRequest searchRequest = new SearchRequest("customer_all_info5");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.aggregation(groupByType);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        ParsedStringTerms parsed = (ParsedStringTerms)response.getAggregations().get("marriage_bucket");
        List<? extends Terms.Bucket> buckets = parsed.getBuckets();
        return buckets.stream().map(item -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("name",(String)((Terms.Bucket) item).getKey());
            map.put("value",String.valueOf(((Terms.Bucket) item).getDocCount()));
            return map;
        }).collect(Collectors.toList());
    }

    private List<Map<String,String>> buildMemberLevelChart(BaseIndexConditonReqDTO baseIndexConditonReqDTO) throws IOException {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if(baseIndexConditonReqDTO.getLevel() == 2){
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",baseIndexConditonReqDTO.getProvince()));
        }else if(baseIndexConditonReqDTO.getLevel() == 3){
            boolQueryBuilder.filter(QueryBuilders.termQuery("homeProvince",baseIndexConditonReqDTO.getProvince()))
                    .filter(QueryBuilders.termQuery("homeCity",baseIndexConditonReqDTO.getCity()));
        }
        AggregationBuilder groupByType = AggregationBuilders.terms("member_level_chart").field("memberLevel");
        SearchRequest searchRequest = new SearchRequest("customer_all_info5");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.aggregation(groupByType);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        ParsedStringTerms parsed = (ParsedStringTerms)response.getAggregations().get("member_level_chart");
        List<? extends Terms.Bucket> buckets = parsed.getBuckets();
        return buckets.stream().map(item -> {
            HashMap<String, String> map = new HashMap<>();
            String keyAsString = ((Terms.Bucket) item).getKeyAsString();


            map.put("name",(String)((Terms.Bucket) item).getKey());
            if(!StringUtils.isEmpty(keyAsString)){
                map.put("name",keyAsString);
            }
            map.put("value",String.valueOf(((Terms.Bucket) item).getDocCount()));
            return map;
        }).collect(Collectors.toList());
    }


    public static void main(String[] args) {
        Date date = new Date(428860800000l);
        System.out.println(new SimpleDateFormat("YYYY-mm-dd").format(date));

        HashMap<String, String> ruleJson = new HashMap<>();
        ruleJson.put("template","yyyy");
        System.out.println(JSON.toJSONString(ruleJson));
    }
}


你可能感兴趣的:(系统设计,java)