123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951 |
- package cn.cslg.pas.service.business.es;
- import cn.cslg.pas.common.dto.business.EsCountDTO;
- import cn.cslg.pas.common.dto.business.EsCountDetailDTO;
- import cn.cslg.pas.common.dto.es.EsCustomFieldValueDTO;
- import cn.cslg.pas.common.dto.es.EsDateRangeDTO;
- import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
- import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
- import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
- import cn.cslg.pas.common.vo.EsConfigVO;
- import cn.cslg.pas.common.vo.business.EsAllCountVO;
- import cn.cslg.pas.common.vo.business.EsCountVO;
- import cn.cslg.pas.common.vo.es.EsDateRangeVO;
- import cn.cslg.pas.domain.es.Patent;
- import cn.cslg.pas.factorys.EsCountAnalyseBuilderFactory.EsCountAnalysisBuilderFactory;
- import cn.cslg.pas.factorys.EsCountAnalyseBuilderFactory.IEsCountAnalysisBuilder;
- import cn.cslg.pas.service.business.CommonService;
- import cn.cslg.pas.service.query.FormatQueryService;
- import co.elastic.clients.elasticsearch.ElasticsearchClient;
- import co.elastic.clients.elasticsearch._types.aggregations.*;
- import co.elastic.clients.elasticsearch._types.query_dsl.Query;
- import co.elastic.clients.elasticsearch.core.SearchRequest;
- import co.elastic.clients.elasticsearch.core.SearchResponse;
- import com.alibaba.fastjson.JSON;
- import lombok.RequiredArgsConstructor;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.stereotype.Service;
- import org.springframework.util.CollectionUtils;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.stream.Collectors;
- @Service
- @RequiredArgsConstructor(onConstructor_ = {@Lazy})
- public class EsCountService {
- private final List<String> childList = Arrays.asList("field");
- private final List<String> nestedList = Arrays.asList("PA", "IN", "PE", "SAT", "MAT", "SRH", "MRH", "MIN");
- private final List<String> dateList = Arrays.asList("PD", "AD", "GD");
- private final List<String> numberList = Arrays.asList("QPN", "QDPN", "SFN", "IFN", "PFN");
- private final ElasticsearchClient client;
- @Autowired
- private EsCountAnalysisBuilderFactory esCountAnalysisBuilderFactory;
- @Autowired
- private FormatQueryService formatQueryService;
- @Autowired
- private EsService esService;
- /**
- * 聚合统计
- *
- * @param vo
- * @return
- * @throws Exception
- */
- public EsCountDTO esCountAnalysis(EsAllCountVO vo) throws Exception {
- List<EsCountVO> countVOS = vo.getCountVOS();
- String searchCondition = vo.getCondition();
- List<EsCustomFieldValueDTO> customFields = vo.getCustomFields();
- Integer taskId = vo.getTaskId();
- Integer projectId = vo.getProjectId();
- EsCountDTO esCountDTO = new EsCountDTO();
- HashMap<String, List<EsCountDetailDTO>> map = new HashMap<>();
- List<EsCountDetailDTO> detailDTOS = new ArrayList<>();
- if (countVOS.size() > 1) {
- EsCountVO countVO = countVOS.get(0);
- List<String> values = countVO.getValues();
- values.removeIf(i -> i.equals("其他"));
- List<EsCountVO> esCountVOS = new ArrayList<>();
- esCountVOS.add(countVOS.get(1));
- if (StringUtils.isNotEmpty(countVO.getFieldId())) {
- if (!CollectionUtils.isEmpty(values)) {
- for (String value : values) {
- EsCustomFieldValueDTO valueDTO = new EsCustomFieldValueDTO();
- valueDTO.setFieldId(countVO.getFieldId());
- valueDTO.setFieldValue(Arrays.asList(value));
- customFields.add(valueDTO);
- this.getReturnData(searchCondition, "",customFields, taskId, projectId,
- esCountVOS, detailDTOS, esCountDTO, value, map);
- }
- }
- } else {
- for (String value : values) {
- String condition = countVO.getField() + " = " + value;
- this.getReturnData(searchCondition, condition, customFields, taskId, projectId,
- esCountVOS, detailDTOS, esCountDTO, value, map);
- }
- }
- } else {
- this.getReturnData(searchCondition, "", customFields, taskId, projectId, countVOS,
- detailDTOS, esCountDTO, "", map);
- }
- esCountDTO.setDetailDTOS(detailDTOS);
- return esCountDTO;
- }
- public void getReturnData(String searchCondition,String condition, List<EsCustomFieldValueDTO> customFields, Integer taskId,
- Integer projectId, List<EsCountVO> countVOS, List<EsCountDetailDTO> detailDTOS,
- EsCountDTO esCountDTO, String firstName, Map<String, List<EsCountDetailDTO>> map) throws Exception {
- if (StringUtils.isNotEmpty(condition)) {
- if (searchCondition != null && !"".equals(searchCondition.trim())) {
- searchCondition = condition + " AND " + searchCondition;
- } else {
- searchCondition = condition;
- }
- }
- if (!CollectionUtils.isEmpty(customFields)) {
- searchCondition = esService.parseCustomField(customFields);
- }
- searchCondition = this.getCondition(searchCondition, taskId, projectId);
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- //设置查询索引
- Query query = null;
- if (StringUtils.isNotEmpty(searchCondition)) {
- //1. 解析检索条件
- treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
- //格式化检索式
- //3. 从es中检索数据
- query = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
- }
- for (EsCountVO countVO : countVOS) {
- String field = countVO.getField();
- Integer topN = countVO.getTopN();
- Integer fieldType = null;
- String type = countVO.getFieldType();
- if (StringUtils.isNotEmpty(type) && type.equals("tree")) {
- fieldType = 6;
- }
- String format = countVO.getFormat();
- List<String> values = countVO.getValues();
- Aggregation aggregation = this.getAggregation(countVO);
- if (query != null) {
- Query finalQuery = query;
- Aggregation finalAggregation = aggregation;
- Aggregation filtersAgg = new Aggregation.Builder().filters(new FiltersAggregation.Builder()
- .filters(i -> i.array(Arrays.asList(finalQuery))).build())
- .aggregations(new HashMap() {{
- put("filters_agg", finalAggregation);
- }}).build();
- builder.aggregations("Agg", filtersAgg);
- } else {
- builder.aggregations("Agg", aggregation);
- }
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- Aggregate agg = response.aggregations().get("Agg");
- if (query != null) {
- String finalSearchCondition = searchCondition;
- if (StringUtils.isNotEmpty(field)) {
- List<FiltersBucket> filtersBuckets = agg.filters().buckets().array();
- if (dateList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- filtersBuckets.forEach(filtersBucket -> {
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
- try {
- this.getDateCountDTOS(filtersAgg, field, topN, format, detailDTOS, esCountDTO);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- });
- } else {
- filtersBuckets.forEach(filtersBucket -> {
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
- this.getDateAnalysisDTOS(filtersAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
- });
- }
- } else if (nestedList.contains(field)) {
- filtersBuckets.forEach(filtersBucket -> {
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
- this.getNestedCountDTOS(filtersAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
- if (!CollectionUtils.isEmpty(values)) {
- esCountDTO.setAllNumber(filtersAgg.nested().docCount());
- }
- });
- } else if (childList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- Integer finalFieldType = fieldType;
- filtersBuckets.forEach(filtersBucket -> {
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
- this.getChildCountDTOS(filtersAgg, field, finalFieldType, topN, detailDTOS, esCountDTO);
- });
- } else {
- Integer finalFieldType1 = fieldType;
- filtersBuckets.forEach(filtersBucket -> {
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
- this.getChildAnalysisDTOS(filtersAgg, field, finalFieldType1, topN,
- detailDTOS, esCountDTO, firstName, map, values);
- });
- }
- } else if (numberList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- } else {
- filtersBuckets.forEach(filtersBucket -> {
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
- this.getNumberAnalysisDTOS(filtersAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
- });
- }
- } else {
- filtersBuckets.forEach(filtersBucket -> {
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
- this.getTermCountDTOS(filtersAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
- });
- }
- } else {
- this.getFilterCountDTO(agg, finalSearchCondition, detailDTOS);
- }
- } else {
- if (dateList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- this.getDateCountDTOS(agg, field, topN, format, detailDTOS, esCountDTO);
- } else {
- this.getDateAnalysisDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
- }
- } else if (nestedList.contains(field)) {
- this.getNestedCountDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
- if (!CollectionUtils.isEmpty(values)) {
- esCountDTO.setAllNumber(agg.nested().docCount());
- }
- } else if (childList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- this.getChildCountDTOS(agg, field, fieldType, topN, detailDTOS, esCountDTO);
- } else {
- this.getChildAnalysisDTOS(agg, field, fieldType, topN,
- detailDTOS, esCountDTO, firstName, map, values);
- }
- } else if (numberList.contains(field)) {
- this.getNumberAnalysisDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
- } else {
- this.getTermCountDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
- }
- }
- esCountDTO.setCondition(searchCondition);
- esCountDTO.setAnalyseMap(map);
- }
- }
- public EsDateRangeDTO getFieldRange(EsDateRangeVO rangeVO) throws Exception {
- String searchCondition = rangeVO.getCondition();
- String field = rangeVO.getField();
- List<EsCustomFieldValueDTO> customFields = rangeVO.getCustomFields();
- if (!CollectionUtils.isEmpty(customFields)) {
- searchCondition = esService.parseCustomField(customFields);
- }
- Integer taskId = rangeVO.getTaskId();
- Integer projectId = rangeVO.getProjectId();
- searchCondition = this.getCondition(searchCondition, taskId, projectId);
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- //设置查询索引
- Query query = null;
- if (StringUtils.isNotEmpty(searchCondition)) {
- //1. 解析检索条件
- treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
- //格式化检索式
- //3. 从es中检索数据
- query = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
- }
- String esField = "";
- if (StringUtils.isNotEmpty(field)) {
- switch (field) {
- case "AD":
- esField = "app_date";
- break;
- case "PD":
- esField = "public_date";
- break;
- case "GD":
- esField = "grant_date";
- break;
- }
- }
- EsDateRangeDTO rangeDTO = new EsDateRangeDTO();
- if (StringUtils.isNotEmpty(esField)) {
- String finalEsField = esField;
- Query finalQuery = query;
- Aggregation min = AggregationBuilders.min(i -> i.field(finalEsField).format("yyyy"));
- Aggregation filtersAgg = new Aggregation.Builder().filters(new FiltersAggregation.Builder()
- .filters(i -> i.array(Arrays.asList(finalQuery))).build())
- .aggregations(new HashMap() {{
- put("filters_agg", min);
- }}).build();
- Aggregation max = AggregationBuilders.max(i -> i.field(finalEsField).format("yyyy"));
- Aggregation maxFilters = new Aggregation.Builder().filters(new FiltersAggregation.Builder()
- .filters(i -> i.array(Arrays.asList(finalQuery))).build())
- .aggregations(new HashMap() {{
- put("max_agg", max);
- }}).build();
- builder.aggregations("minAgg", filtersAgg);
- builder.aggregations("maxAgg", maxFilters);
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- Aggregate minAgg = response.aggregations().get("minAgg");
- if (minAgg != null) {
- List<FiltersBucket> list = minAgg.filters().buckets().array();
- for (FiltersBucket bucket : list) {
- Aggregate aggregate = bucket.aggregations().get("filters_agg");
- String minValue = aggregate.min().valueAsString();
- rangeDTO.setMinDate(minValue);
- }
- }
- Aggregate maxAgg = response.aggregations().get("maxAgg");
- if (maxAgg != null) {
- List<FiltersBucket> list = maxAgg.filters().buckets().array();
- for (FiltersBucket bucket : list) {
- Aggregate aggregate = bucket.aggregations().get("max_agg");
- String maxValue = aggregate.max().valueAsString();
- rangeDTO.setMaxDate(maxValue);
- }
- }
- }
- return rangeDTO;
- }
- /**
- * 查询共用Aggregation
- *
- * @param countVO
- * @return
- * @throws Exception
- */
- public Aggregation getAggregation(EsCountVO countVO) throws Exception {
- String field = countVO.getField();
- Integer topN = countVO.getTopN();
- Boolean ifHaveChild = countVO.getIfHaveChild();
- String fieldId = countVO.getFieldId();
- Integer fieldType = null;
- String type = countVO.getFieldType();
- if (StringUtils.isNotEmpty(type) && type.equals("tree")) {
- fieldType = 6;
- }
- String format = countVO.getFormat();
- String valueOne = countVO.getValueOne();
- String valueTwo = countVO.getValueTwo();
- List<String> values = countVO.getValues();
- IEsCountAnalysisBuilder iEsCountAnalysisBuilder = null;
- String json = CommonService.readJsonFile("esCountAnalysis.json");
- List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
- EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getField().equals(field))
- .findFirst().orElse(null);
- Aggregation aggregation = null;
- if (esConfigVO != null) {
- iEsCountAnalysisBuilder = esCountAnalysisBuilderFactory.getClass(esConfigVO.getEsClass());
- iEsCountAnalysisBuilder.setField(esConfigVO.getEsField());
- iEsCountAnalysisBuilder.setValueOne(valueOne);
- iEsCountAnalysisBuilder.setValueTwo(valueTwo);
- iEsCountAnalysisBuilder.setValues(values);
- iEsCountAnalysisBuilder.setFieldId(fieldId);
- iEsCountAnalysisBuilder.setFieldType(String.valueOf(fieldType));
- iEsCountAnalysisBuilder.setTopN(topN);
- iEsCountAnalysisBuilder.setFormat(format);
- iEsCountAnalysisBuilder.setIfHaveChild(ifHaveChild);
- if (iEsCountAnalysisBuilder.getField().contains(".")) {
- String path = iEsCountAnalysisBuilder.getField()
- .substring(0, iEsCountAnalysisBuilder.getField().indexOf("."));
- iEsCountAnalysisBuilder.setPath(path);
- }
- aggregation = iEsCountAnalysisBuilder.createCountAnalyseAgg();
- }
- return aggregation;
- }
- /**
- * 获取筛选条件
- *
- * @param taskId
- * @param projectId
- * @return
- */
- public String getCondition(String searchCondition, Integer taskId, Integer projectId) {
- if (taskId != null) {
- if (searchCondition != null && !"".equals(searchCondition.trim())) {
- searchCondition = "taskId = " + taskId + " AND " + searchCondition;
- } else {
- searchCondition = "taskId = " + taskId;
- }
- } else {
- if (projectId != null) {
- if (searchCondition != null && !"".equals(searchCondition.trim())) {
- searchCondition = "projectId = " + projectId + " AND " + searchCondition;
- } else {
- searchCondition = "projectId = " + projectId;
- }
- }
- }
- return searchCondition;
- }
- /**
- * 获取Filter聚合返回数据
- *
- * @param agg
- * @param condition
- * @return
- */
- public void getFilterCountDTO(Aggregate agg, String condition, List<EsCountDetailDTO> detailDTOS) {
- EsCountDetailDTO filterDTO = new EsCountDetailDTO();
- filterDTO.setField("condition");
- filterDTO.setName(condition);
- filterDTO.setNumber(agg.filter().docCount());
- if (filterDTO.getNumber() > 0) {
- detailDTOS.add(filterDTO);
- }
- }
- /**
- * 获取Terms聚合后数据
- *
- * @param agg
- * @param field
- * @param detailDTOS
- */
- public void getTermCountDTOS(Aggregate agg, String field, Integer topN, List<EsCountDetailDTO> detailDTOS,
- EsCountDTO esCountDTO, String firstName,
- Map<String, List<EsCountDetailDTO>> map,List<String> values) {
- List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
- EsCountDetailDTO countDTO = new EsCountDetailDTO();
- countDTO.setField(field);
- countDTO.setName("其他");
- countDTO.setNumber(agg.sterms().sumOtherDocCount());
- countDTO.setTopN(topN);
- detailDTOS.add(countDTO);
- List<StringTermsBucket> list = agg.sterms().buckets().array();
- list.forEach(bucket -> {
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- Aggregate aggregate = bucket.aggregations().get("filter_agg");
- dto.setName(bucket.key().stringValue());
- dto.setNumber(bucket.docCount());
- dto.setTopN(topN);
- dto.setFirstName(firstName);
- if (aggregate != null) {
- dto.setNumber(aggregate.filter().docCount());
- }
- if (dto.getNumber() > 0) {
- countDetailDTOS.add(dto);
- }
- });
- List<String> strs = new ArrayList<>();
- if (!CollectionUtils.isEmpty(values)) {
- strs.addAll(values);
- }
- if (StringUtils.isNotEmpty(firstName)) {
- countDetailDTOS.removeIf(dto -> dto.getName().equals("其他"));
- if (countDetailDTOS.size() != values.size()) {
- for (EsCountDetailDTO detailDTO : countDetailDTOS) {
- strs.removeIf(i -> i.equals(detailDTO.getName()));
- }
- for (String value : strs) {
- EsCountDetailDTO detailDTO = new EsCountDetailDTO();
- detailDTO.setName(value);
- detailDTO.setFirstName(firstName);
- detailDTO.setField(field);
- detailDTO.setNumber(0L);
- detailDTO.setTopN(topN);
- countDetailDTOS.add(detailDTO);
- }
- } else if (countDetailDTOS.isEmpty()) {
- for (String value : values) {
- EsCountDetailDTO detailDTO = new EsCountDetailDTO();
- detailDTO.setName(value);
- detailDTO.setFirstName(firstName);
- detailDTO.setField(field);
- detailDTO.setNumber(0L);
- detailDTO.setTopN(topN);
- countDetailDTOS.add(detailDTO);
- }
- }
- map.put(firstName, countDetailDTOS);
- } else {
- detailDTOS.addAll(countDetailDTOS);
- }
- }
- /**
- * 获取children聚合后数据
- *
- * @param agg
- * @param field
- * @param detailDTOS
- */
- public void getChildCountDTOS(Aggregate agg, String field, Integer fieldType, Integer topN,
- List<EsCountDetailDTO> detailDTOS, EsCountDTO esCountDTO) {
- esCountDTO.setAllNumber(agg.children().docCount());
- Aggregate childAgg = agg.children().aggregations().get("childAgg");
- List<StringTermsBucket> list = childAgg.sterms().buckets().array();
- list.forEach(bucket -> {
- Aggregate aggregate = bucket.aggregations().get("termAgg");
- List<StringTermsBucket> termsBuckets = aggregate.sterms().buckets().array();
- termsBuckets.forEach(termsBucket -> {
- Aggregate termAgg = termsBucket.aggregations().get("filterAgg");
- long count = termAgg.filter().docCount();
- if (count > 0) {
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- dto.setName(termsBucket.key().stringValue());
- dto.setNumber(termsBucket.docCount());
- dto.setNumber(count);
- dto.setTopN(topN);
- if (dto.getNumber() > 0) {
- detailDTOS.add(dto);
- if (fieldType == null || fieldType != 6) {
- Long docCount = aggregate.sterms().sumOtherDocCount();
- EsCountDetailDTO detail = new EsCountDetailDTO();
- detail.setField(field);
- detail.setName("未选择");
- detail.setNumber(docCount);
- detail.setTopN(topN);
- if (!detailDTOS.contains(detail)) {
- detailDTOS.add(detail);
- }
- }
- }
- }
- });
- });
- }
- /**
- * 获取children分析后数据
- *
- * @param agg
- * @param field
- * @param detailDTOS
- */
- public void getChildAnalysisDTOS(Aggregate agg, String field, Integer fieldType, Integer topN,
- List<EsCountDetailDTO> detailDTOS, EsCountDTO esCountDTO,
- String firstName, Map<String, List<EsCountDetailDTO>> map,List<String> values) {
- List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
- Aggregate childAgg = agg.children().aggregations().get("childAgg");
- List<StringTermsBucket> list = childAgg.sterms().buckets().array();
- list.forEach(bucket -> {
- esCountDTO.setAllNumber(bucket.docCount());
- Aggregate aggregate = bucket.aggregations().get("termAgg");
- List<StringTermsBucket> termsBuckets = aggregate.sterms().buckets().array();
- termsBuckets.forEach(termsBucket -> {
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- dto.setName(termsBucket.key().stringValue());
- dto.setNumber(termsBucket.docCount());
- dto.setFirstName(firstName);
- dto.setTopN(topN);
- countDetailDTOS.add(dto);
- if (fieldType == null || fieldType != 6) {
- Long docCount = aggregate.sterms().sumOtherDocCount();
- EsCountDetailDTO detail = new EsCountDetailDTO();
- detail.setField(field);
- detail.setName("未选择");
- detail.setNumber(docCount);
- detail.setTopN(topN);
- if (!countDetailDTOS.contains(detail)) {
- countDetailDTOS.add(detail);
- }
- }
- // if (dto.getNumber() > 0) {
- // detailDTOS.add(dto);
- // if (fieldType == null || fieldType != 6) {
- // Long docCount = aggregate.sterms().sumOtherDocCount();
- // EsCountDetailDTO detail = new EsCountDetailDTO();
- // detail.setField(field);
- // detail.setName("未选择");
- // detail.setNumber(docCount);
- // detail.setTopN(topN);
- // if (!detailDTOS.contains(detail)) {
- // detailDTOS.add(detail);
- // }
- // }
- // }
- });
- });
- List<String> strs = new ArrayList<>();
- if (!CollectionUtils.isEmpty(values)) {
- strs.addAll(values);
- }
- if (StringUtils.isNotEmpty(firstName)) {
- countDetailDTOS.removeIf(dto -> dto.getName().equals("其他"));
- if (countDetailDTOS.size() != values.size()) {
- for (EsCountDetailDTO detailDTO : countDetailDTOS) {
- strs.removeIf(i -> i.equals(detailDTO.getName()));
- }
- for (String value : strs) {
- EsCountDetailDTO detailDTO = new EsCountDetailDTO();
- detailDTO.setName(value);
- detailDTO.setFirstName(firstName);
- detailDTO.setField(field);
- detailDTO.setNumber(0L);
- detailDTO.setTopN(topN);
- countDetailDTOS.add(detailDTO);
- }
- } else if (countDetailDTOS.isEmpty()) {
- for (String value : values) {
- EsCountDetailDTO detailDTO = new EsCountDetailDTO();
- detailDTO.setName(value);
- detailDTO.setFirstName(firstName);
- detailDTO.setField(field);
- detailDTO.setNumber(0L);
- detailDTO.setTopN(topN);
- countDetailDTOS.add(detailDTO);
- }
- }
- map.put(firstName, countDetailDTOS);
- } else {
- detailDTOS.addAll(countDetailDTOS);
- }
- }
- /**
- * 获取range分析后数据
- *
- * @param agg
- * @param field
- * @param detailDTOS
- */
- public void getNumberAnalysisDTOS(Aggregate agg, String field, Integer topN, List<EsCountDetailDTO> detailDTOS,
- EsCountDTO esCountDTO, String firstName,
- Map<String, List<EsCountDetailDTO>> map,List<String> values) {
- List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
- List<RangeBucket> list = agg.range().buckets().array();
- for (RangeBucket bucket : list) {
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- dto.setName(bucket.key());
- dto.setNumber(bucket.docCount());
- dto.setTopN(topN);
- dto.setFirstName(firstName);
- countDetailDTOS.add(dto);
- // if (dto.getNumber() > 0) {
- // detailDTOS.add(dto);
- // }
- }
- List<String> strs = new ArrayList<>();
- if (!CollectionUtils.isEmpty(values)) {
- strs.addAll(values);
- }
- if (StringUtils.isNotEmpty(firstName)) {
- countDetailDTOS.removeIf(dto -> dto.getName().equals("其他"));
- if (countDetailDTOS.size() != values.size()) {
- for (EsCountDetailDTO detailDTO : countDetailDTOS) {
- strs.removeIf(i -> i.equals(detailDTO.getName()));
- }
- for (String value : strs) {
- EsCountDetailDTO detailDTO = new EsCountDetailDTO();
- detailDTO.setName(value);
- detailDTO.setFirstName(firstName);
- detailDTO.setField(field);
- detailDTO.setNumber(0L);
- detailDTO.setTopN(topN);
- countDetailDTOS.add(detailDTO);
- }
- } else if (countDetailDTOS.isEmpty()) {
- for (String value : values) {
- EsCountDetailDTO detailDTO = new EsCountDetailDTO();
- detailDTO.setName(value);
- detailDTO.setFirstName(firstName);
- detailDTO.setField(field);
- detailDTO.setNumber(0L);
- detailDTO.setTopN(topN);
- countDetailDTOS.add(detailDTO);
- }
- }
- map.put(firstName, countDetailDTOS);
- } else {
- detailDTOS.addAll(countDetailDTOS);
- }
- }
- /**
- * 获取dateHistogram聚合后数据
- *
- * @param agg
- * @param field
- * @param detailDTOS
- */
- public void getDateCountDTOS(Aggregate agg, String field, Integer topN, String format, List<EsCountDetailDTO> detailDTOS, EsCountDTO esCountDTO) throws ParseException {
- List<DateHistogramBucket> list = agg.dateHistogram().buckets().array();
- esCountDTO.setAllNumber(Long.valueOf(String.valueOf(list.size())));
- List<EsCountDetailDTO> esCountDetailDTOS = new ArrayList<>();
- if (StringUtils.isNotEmpty(format) && format.equals("半年")) {
- List<EsCountDetailDTO> detailDTOList = new ArrayList<>();
- for (DateHistogramBucket bucket : list) {
- String year = bucket.keyAsString();
- SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
- String startMonth = year + "-01";
- Calendar calendar = Calendar.getInstance();
- Date date = monthFormat.parse(startMonth);
- calendar.setTime(date);
- calendar.add(Calendar.MONTH, 6);
- Date halfYearDate = calendar.getTime();
- Aggregate aggregate = bucket.aggregations().get("halfYearAgg");
- List<DateHistogramBucket> buckets = aggregate.dateHistogram().buckets().array();
- long h1Count = 0;
- long h2Count = 0;
- for (DateHistogramBucket histogramBucket : buckets) {
- String key = histogramBucket.keyAsString();
- Date month = monthFormat.parse(key);
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- dto.setName(year + "-H1");
- dto.setNumber(h1Count);
- dto.setTopN(topN);
- EsCountDetailDTO dto2 = new EsCountDetailDTO();
- dto2.setField(field);
- dto2.setName(year + "-H2");
- dto2.setNumber(h2Count);
- dto2.setTopN(topN);
- if (month.before(halfYearDate)) {
- h1Count += histogramBucket.docCount();
- dto.setNumber(h1Count);
- }
- if (month.after(halfYearDate)) {
- h2Count += histogramBucket.docCount();
- dto2.setNumber(h2Count);
- }
- detailDTOList.add(dto);
- detailDTOList.add(dto2);
- }
- }
- Map<String, List<EsCountDetailDTO>> map = detailDTOList.stream().collect(Collectors.groupingBy(EsCountDetailDTO::getName));
- for (String key : map.keySet()) {
- List<EsCountDetailDTO> dtoList = map.get(key);
- long sum = dtoList.stream().mapToLong(EsCountDetailDTO::getNumber).sum();
- EsCountDetailDTO detailDTO = dtoList.get(0);
- detailDTO.setNumber(sum);
- esCountDetailDTOS.add(detailDTO);
- }
- } else if (StringUtils.isNotEmpty(format) && format.equals("季")) {
- for (DateHistogramBucket bucket : list) {
- String yearMonth = bucket.keyAsString();
- String year = yearMonth.substring(0, yearMonth.indexOf("-"));
- String firstQuarter = year + "-01";
- String secondQuarter = year + "-04";
- String thirdQuarter = year + "-07";
- String forthQuarter = year + "-10";
- String firstQ = year + "-Q1";
- String secondQ = year + "-Q2";
- String thirdQ = year + "-Q3";
- String forthQ = year + "-Q4";
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- Aggregate aggregate = bucket.aggregations().get("filter_agg");
- if (yearMonth.equals(firstQuarter)) {
- dto.setName(firstQ);
- } else if (yearMonth.equals(secondQuarter)) {
- dto.setName(secondQ);
- } else if (yearMonth.equals(thirdQuarter)) {
- dto.setName(thirdQ);
- } else if (yearMonth.equals(forthQuarter)) {
- dto.setName(forthQ);
- }
- dto.setNumber(bucket.docCount());
- dto.setTopN(topN);
- if (aggregate != null) {
- dto.setNumber(aggregate.filter().docCount());
- }
- esCountDetailDTOS.add(dto);
- }
- } else {
- list.forEach(bucket -> {
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- Aggregate aggregate = bucket.aggregations().get("filter_agg");
- dto.setName(bucket.keyAsString());
- dto.setNumber(bucket.docCount());
- dto.setTopN(topN);
- if (aggregate != null) {
- dto.setNumber(aggregate.filter().docCount());
- }
- esCountDetailDTOS.add(dto);
- });
- }
- if (!CollectionUtils.isEmpty(esCountDetailDTOS)) {
- List<EsCountDetailDTO> collect = esCountDetailDTOS.stream()
- .sorted(Comparator.comparing(EsCountDetailDTO::getName).reversed()).limit(topN).collect(Collectors.toList());
- detailDTOS.addAll(collect);
- }
- // EsCountDetailDTO countDTO = new EsCountDetailDTO();
- // countDTO.setField(field);
- // countDTO.setName("其他");
- // if (list.size() > topN) {
- // int sum = list.size() - topN;
- // countDTO.setNumber(Long.valueOf(String.valueOf(sum)));
- //
- // } else {
- // countDTO.setNumber(0L);
- // }
- // countDTO.setTopN(topN);
- // detailDTOS.add(countDTO);
- }
- /**
- * 获取dateHistogram分析后数据
- *
- * @param agg
- * @param field
- * @param topN
- * @param detailDTOS
- */
- public void getDateAnalysisDTOS(Aggregate agg, String field, Integer topN, List<EsCountDetailDTO> detailDTOS,
- EsCountDTO esCountDTO, String firstName,
- Map<String, List<EsCountDetailDTO>> map,List<String> values) {
- List<RangeBucket> list1 = agg.dateRange().buckets().array();
- List<EsCountDetailDTO> esCountDetailDTOS = new ArrayList<>();
- for (RangeBucket bucket : list1) {
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setFirstName(firstName);
- dto.setField(field);
- dto.setName(bucket.key());
- dto.setNumber(bucket.docCount());
- dto.setTopN(topN);
- esCountDetailDTOS.add(dto);
- }
- if (!CollectionUtils.isEmpty(esCountDetailDTOS)) {
- List<EsCountDetailDTO> collect = esCountDetailDTOS.stream()
- .sorted(Comparator.comparing(EsCountDetailDTO::getName).reversed()).limit(topN).collect(Collectors.toList());
- List<String> strs = new ArrayList<>();
- if (!CollectionUtils.isEmpty(values)) {
- strs.addAll(values);
- }
- if (StringUtils.isNotEmpty(firstName)) {
- collect.removeIf(dto -> dto.getName().equals("其他"));
- if (collect.size() != values.size()) {
- for (EsCountDetailDTO detailDTO : collect) {
- strs.removeIf(i -> i.equals(detailDTO.getName()));
- }
- for (String value : strs) {
- EsCountDetailDTO detailDTO = new EsCountDetailDTO();
- detailDTO.setName(value);
- detailDTO.setFirstName(firstName);
- detailDTO.setField(field);
- detailDTO.setNumber(0L);
- detailDTO.setTopN(topN);
- collect.add(detailDTO);
- }
- } else if (collect.isEmpty()) {
- for (String value : values) {
- EsCountDetailDTO detailDTO = new EsCountDetailDTO();
- detailDTO.setName(value);
- detailDTO.setFirstName(firstName);
- detailDTO.setField(field);
- detailDTO.setNumber(0L);
- detailDTO.setTopN(topN);
- collect.add(detailDTO);
- }
- }
- map.put(firstName, collect);
- } else {
- detailDTOS.addAll(collect);
- }
- }
- }
- /**
- * 获取nested聚合后数据
- *
- * @param agg
- * @param field
- * @param detailDTOS
- */
- public void getNestedCountDTOS(Aggregate agg, String field, Integer topN, List<EsCountDetailDTO> detailDTOS,
- EsCountDTO esCountDTO, String firstName,
- Map<String, List<EsCountDetailDTO>> map, List<String> values) {
- Aggregate termsAgg = agg.nested().aggregations().get("terms_agg");
- List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
- EsCountDetailDTO countDTO = new EsCountDetailDTO();
- countDTO.setField(field);
- countDTO.setName("其他");
- countDTO.setNumber(termsAgg.sterms().sumOtherDocCount());
- countDTO.setTopN(topN);
- countDetailDTOS.add(countDTO);
- List<StringTermsBucket> list = termsAgg.sterms().buckets().array();
- list.forEach(bucket -> {
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- Aggregate aggregate = bucket.aggregations().get("filter_agg");
- dto.setName(bucket.key().stringValue());
- dto.setNumber(bucket.docCount());
- dto.setTopN(topN);
- dto.setFirstName(firstName);
- if (aggregate != null) {
- dto.setNumber(aggregate.filter().docCount());
- }
- countDetailDTOS.add(dto);
- // if (dto.getNumber() > 0) {
- // detailDTOS.add(dto);
- // }
- });
- List<String> strs = new ArrayList<>();
- if (!CollectionUtils.isEmpty(values)) {
- strs.addAll(values);
- }
- if (StringUtils.isNotEmpty(firstName)) {
- countDetailDTOS.removeIf(dto -> dto.getName().equals("其他"));
- if (countDetailDTOS.size() != values.size()) {
- for (EsCountDetailDTO detailDTO : countDetailDTOS) {
- strs.removeIf(i -> i.equals(detailDTO.getName()));
- }
- for (String value : strs) {
- EsCountDetailDTO detailDTO = new EsCountDetailDTO();
- detailDTO.setName(value);
- detailDTO.setFirstName(firstName);
- detailDTO.setField(field);
- detailDTO.setNumber(0L);
- detailDTO.setTopN(topN);
- countDetailDTOS.add(detailDTO);
- }
- } else if (countDetailDTOS.isEmpty()) {
- for (String value : values) {
- EsCountDetailDTO detailDTO = new EsCountDetailDTO();
- detailDTO.setName(value);
- detailDTO.setFirstName(firstName);
- detailDTO.setField(field);
- detailDTO.setNumber(0L);
- detailDTO.setTopN(topN);
- countDetailDTOS.add(detailDTO);
- }
- }
- map.put(firstName, countDetailDTOS);
- } else {
- detailDTOS.addAll(countDetailDTOS);
- }
- }
- }
|