1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294 |
- package cn.cslg.pas.service.business.es;
- import cn.cslg.pas.common.dto.GetUnselectedDTO;
- 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._types.query_dsl.QueryBuilders;
- import co.elastic.clients.elasticsearch.core.SearchRequest;
- import co.elastic.clients.elasticsearch.core.SearchResponse;
- import co.elastic.clients.elasticsearch.core.search.Hit;
- 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.io.IOException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.concurrent.atomic.AtomicLong;
- 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", "SRH","PRCO");
- private final List<String> dateList = Arrays.asList("PD", "AD", "GD");
- private final List<String> numberList = Arrays.asList("QPN", "QDPN", "SFN", "IFN", "PFN");
- private final List<String> nestDateList = Arrays.asList("PRD");
- private final List<String> nestChildList = Arrays.asList("MAT","MRH","MIN");
- 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 = "";
- if (dateList.contains(countVO.getField())) {
- condition = this.getDateFormat(countVO.getField(), value);
- } else {
- 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,projectId,taskId);
- }
- 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",projectId);
- }
- for (EsCountVO countVO : countVOS) {
- String field = countVO.getField();
- Integer topN = countVO.getTopN();
- String valueOne = countVO.getValueOne();
- String valueTwo = countVO.getValueTwo();
- 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, projectId, taskId);
- 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);
- }
- //解除最大条数限制
- builder.trackTotalHits(i -> i.enabled(true));
- 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,valueOne,valueTwo);
- } 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,valueOne);
- if (!CollectionUtils.isEmpty(values)) {
- esCountDTO.setAllNumber(filtersAgg.nested().docCount());
- }
- });
- } else if (nestDateList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- filtersBuckets.forEach(filtersBucket -> {
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
- Aggregate termsAgg = filtersAgg.nested().aggregations().get("terms_agg");
- try {
- this.getDateCountDTOS(termsAgg, field, topN, format, detailDTOS, esCountDTO,valueOne,valueTwo);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- });
- } else {
- filtersBuckets.forEach(filtersBucket -> {
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
- Aggregate termsAgg = filtersAgg.nested().aggregations().get("terms_agg");
- this.getDateAnalysisDTOS(termsAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
- });
- }
- } else if (nestChildList.contains(field)) {
- filtersBuckets.forEach(filtersBucket -> {
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
- Aggregate childAgg = filtersAgg.children().aggregations().get("childAgg");
- this.getNestedCountDTOS(childAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values,valueOne);
- if (!CollectionUtils.isEmpty(values)) {
- esCountDTO.setAllNumber(childAgg.nested().docCount());
- }
- });
- } else if (childList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- Integer finalFieldType = fieldType;
- filtersBuckets.forEach(filtersBucket -> {
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
- try {
- this.getChildCountDTOS(filtersAgg, field, finalFieldType, topN, detailDTOS, esCountDTO,projectId,taskId);
- } catch (Exception e) {
- e.printStackTrace();
- }
- });
- } 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,valueOne);
- });
- }
- } else {
- this.getFilterCountDTO(agg, finalSearchCondition, detailDTOS);
- }
- } else {
- if (dateList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- this.getDateCountDTOS(agg, field, topN, format, detailDTOS, esCountDTO,valueOne,valueTwo);
- } 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,valueOne);
- if (!CollectionUtils.isEmpty(values)) {
- esCountDTO.setAllNumber(agg.nested().docCount());
- }
- } else if (nestDateList.contains(field)) {
- Aggregate termsAgg = agg.nested().aggregations().get("terms_agg");
- this.getDateCountDTOS(termsAgg, field, topN, format, detailDTOS, esCountDTO,valueOne,valueTwo);
- } else if (nestChildList.contains(field)) {
- Aggregate childAgg = agg.children().aggregations().get("childAgg");
- this.getNestedCountDTOS(childAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values,valueOne);
- if (!CollectionUtils.isEmpty(values)) {
- esCountDTO.setAllNumber(childAgg.nested().docCount());
- }
- } else if (childList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- this.getChildCountDTOS(agg, field, fieldType, topN, detailDTOS, esCountDTO, projectId, taskId);
- } 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,valueOne);
- }
- }
- esCountDTO.setCondition(searchCondition);
- esCountDTO.setAnalyseMap(map);
- }
- }
- public void getReturnData1(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,projectId,taskId);
- }
- 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",projectId);
- builder.query(query);
- }
- for (EsCountVO countVO : countVOS) {
- String field = countVO.getField();
- Integer topN = countVO.getTopN();
- String valueOne = countVO.getValueOne();
- String valueTwo = countVO.getValueTwo();
- 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, projectId, taskId);
- builder.aggregations("Agg", aggregation);
- //解除最大条数限制
- builder.trackTotalHits(i -> i.enabled(true));
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- Aggregate agg = response.aggregations().get("Agg");
- if (StringUtils.isNotEmpty(field)) {
- if (dateList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- this.getDateCountDTOS(agg, field, topN, format, detailDTOS, esCountDTO,valueOne,valueTwo);
- } 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,valueOne);
- if (!CollectionUtils.isEmpty(values)) {
- esCountDTO.setAllNumber(agg.nested().docCount());
- }
- } else if (nestDateList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- Aggregate termsAgg = agg.nested().aggregations().get("terms_agg");
- this.getDateCountDTOS(termsAgg, field, topN, format, detailDTOS, esCountDTO,valueOne,valueTwo);
- } else {
- Aggregate termsAgg = agg.nested().aggregations().get("terms_agg");
- this.getDateAnalysisDTOS(termsAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
- }
- } else if (nestChildList.contains(field)) {
- Aggregate childAgg = agg.children().aggregations().get("childAgg");
- this.getNestedCountDTOS(childAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values,valueOne);
- if (!CollectionUtils.isEmpty(values)) {
- esCountDTO.setAllNumber(childAgg.nested().docCount());
- }
- } else if (childList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- this.getChildCountDTOS(agg, field, fieldType, topN, detailDTOS, esCountDTO,projectId,taskId);
- } else {
- this.getChildAnalysisDTOS(agg, field, fieldType, topN,
- detailDTOS, esCountDTO, firstName, map, values);
- }
- } else if (numberList.contains(field)) {
- if (CollectionUtils.isEmpty(values)) {
- } else {
- this.getNumberAnalysisDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
- }
- } else {
- this.getTermCountDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values,valueOne);
- }
- } else {
- this.getFilterCountDTO(agg, searchCondition, detailDTOS);
- }
- 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, rangeVO.getProjectId(), rangeVO.getTaskId());
- }
- 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",projectId);
- }
- 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, Integer projectId, Integer taskId) 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.setProjectId(projectId);
- iEsCountAnalysisBuilder.setTaskId(taskId);
- 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,String valueOne) {
- List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
- long otherNum = 0l;
- if (StringUtils.isNotEmpty(valueOne)) {
- Aggregate filterAgg = agg.filter().aggregations().get("filter_agg");
- List<StringTermsBucket> list = filterAgg.sterms().buckets().array();
- for (StringTermsBucket bucket : list) {
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- String value = bucket.key().stringValue();
- if (StringUtils.isNotEmpty(value)) {
- dto.setName(value);
- dto.setNumber(bucket.docCount());
- dto.setTopN(topN);
- dto.setFirstName(firstName);
- if (dto.getNumber() > 0) {
- countDetailDTOS.add(dto);
- }
- } else {
- otherNum += 1;
- }
- }
- if (countDetailDTOS.size() > topN) {
- countDetailDTOS.remove(countDetailDTOS.size() - 1);
- }
- EsCountDetailDTO countDTO = new EsCountDetailDTO();
- countDTO.setField(field);
- countDTO.setName("其他");
- countDTO.setTopN(topN);
- Long count = filterAgg.sterms().sumOtherDocCount();
- if (count > 0) {
- countDTO.setNumber(count + otherNum);
- countDetailDTOS.add(countDTO);
- }
- esCountDTO.setRemainingNum(count);
- } else {
- List<StringTermsBucket> list = agg.sterms().buckets().array();
- for (StringTermsBucket bucket : list) {
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- Aggregate aggregate = bucket.aggregations().get("filter_agg");
- String value = bucket.key().stringValue();
- if (StringUtils.isNotEmpty(value)) {
- dto.setName(value);
- 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);
- }
- } else {
- otherNum += 1;
- }
- }
- if (countDetailDTOS.size() > topN) {
- countDetailDTOS.remove(countDetailDTOS.size() - 1);
- }
- EsCountDetailDTO countDTO = new EsCountDetailDTO();
- countDTO.setField(field);
- countDTO.setName("其他");
- countDTO.setTopN(topN);
- Long count = agg.sterms().sumOtherDocCount();
- if (count > 0) {
- countDTO.setNumber(count + otherNum);
- detailDTOS.add(countDTO);
- }
- esCountDTO.setRemainingNum(count);
- }
- 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,Integer projectId, Integer taskId) throws Exception {
- 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) {
- GetUnselectedDTO unselectedDTO = this.getUnselectedCustomNum(projectId, taskId);
- EsCountDetailDTO detail = new EsCountDetailDTO();
- detail.setField(field);
- detail.setName("未选择");
- detail.setNumber(unselectedDTO.getNumber().longValue());
- detail.setTopN(topN);
- if (!detailDTOS.contains(detail)) {
- detailDTOS.add(detail);
- }
- }
- }
- public GetUnselectedDTO getUnselectedCustomNum(Integer projectId, Integer taskId) throws Exception {
- GetUnselectedDTO dto = new GetUnselectedDTO();
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- Query q = null;
- if (taskId != null) {
- Query query = QueryBuilders.term(i -> i.field("project_task.task_id").value(taskId));
- q = QueryBuilders.hasChild(i -> i.type("task").query(query));
- } else {
- Query query = QueryBuilders.term(i -> i.field("project_id").value(projectId));
- q = QueryBuilders.hasChild(i -> i.type("project").query(query));
- }
- builder.size(9999);
- builder.query(q);
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- List<Hit<Patent>> hits = response.hits().hits();
- List<String> list = new ArrayList<>();
- for (Hit<Patent> hit : hits) {
- String id = hit.id();
- list.add(id);
- }
- if (!CollectionUtils.isEmpty(list)) {
- dto = this.ifExistChild(list);
- }
- return dto;
- }
- public GetUnselectedDTO ifExistChild(List<String> list) throws IOException {
- GetUnselectedDTO dto = new GetUnselectedDTO();
- List<String> existChildIds = new ArrayList<>();
- List<String> beinglessChildIds = new ArrayList<>();
- int count = 0;
- for (String id : list) {
- SearchRequest.Builder builder = new SearchRequest.Builder();
- //设置查询索引
- builder.index("patent");
- Query q1 = QueryBuilders.parentId(i -> i.type("project_customfield").id(id));
- Query q2 = QueryBuilders.exists(i -> i.field("custom_field"));
- Query bool = QueryBuilders.bool(i -> i.must(q1, q2));
- builder.query(bool);
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
- Long total = response.hits().total().value();
- if (total > 0) {
- existChildIds.add(id);
- } else {
- beinglessChildIds.add(id);
- count++;
- }
- }
- dto.setNumber(count);
- dto.setExistChildIds(existChildIds);
- dto.setBeinglessChildIds(beinglessChildIds);
- return dto;
- }
- /**
- * 获取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);
- }
- }
- });
- });
- 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, String valueOne, String valueTwo) throws ParseException {
- List<DateHistogramBucket> list = agg.dateHistogram().buckets().array();
- long all = Long.valueOf(String.valueOf(list.size()));
- esCountDTO.setAllNumber(all);
- 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());
- }
- if (StringUtils.isNotEmpty(valueOne) || StringUtils.isNotEmpty(valueTwo)) {
- if (dto.getNumber() > 0) {
- esCountDetailDTOS.add(dto);
- }
- } else {
- esCountDetailDTOS.add(dto);
- }
- });
- }
- if (!CollectionUtils.isEmpty(esCountDetailDTOS)) {
- if (StringUtils.isNotEmpty(valueOne) || StringUtils.isNotEmpty(valueTwo)) {
- all = Long.valueOf(String.valueOf(esCountDetailDTOS.size()));
- }
- long num = Long.valueOf(String.valueOf(topN));
- long finalNum = all - num;
- if (finalNum > 0) {
- esCountDTO.setRemainingNum(finalNum);
- } else {
- esCountDTO.setRemainingNum(0l);
- }
- List<EsCountDetailDTO> collect = esCountDetailDTOS.stream()
- .sorted(Comparator.comparing(EsCountDetailDTO::getName).reversed()).limit(topN).collect(Collectors.toList());
- detailDTOS.addAll(collect);
- }
- }
- /**
- * 获取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,String valueOne) {
- Aggregate termsAgg = agg.nested().aggregations().get("terms_agg");
- List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
- long otherNum = 0l;
- if (StringUtils.isNotEmpty(valueOne)) {
- Aggregate filterAgg = termsAgg.filter().aggregations().get("filter_agg");
- List<StringTermsBucket> list = filterAgg.sterms().buckets().array();
- for (StringTermsBucket bucket : list) {
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- String value = bucket.key().stringValue();
- if (StringUtils.isNotEmpty(value)) {
- dto.setName(value);
- dto.setNumber(bucket.docCount());
- dto.setTopN(topN);
- dto.setFirstName(firstName);
- if (dto.getNumber() > 0) {
- countDetailDTOS.add(dto);
- }
- } else {
- otherNum += 1;
- }
- }
- if (countDetailDTOS.size() > topN) {
- countDetailDTOS.remove(countDetailDTOS.size() - 1);
- }
- EsCountDetailDTO countDTO = new EsCountDetailDTO();
- countDTO.setField(field);
- countDTO.setName("其他");
- countDTO.setTopN(topN);
- Long count = filterAgg.sterms().sumOtherDocCount();
- if (count > 0) {
- countDTO.setNumber(count + otherNum);
- countDetailDTOS.add(countDTO);
- }
- esCountDTO.setRemainingNum(count);
- } else {
- List<StringTermsBucket> list = termsAgg.sterms().buckets().array();
- for (StringTermsBucket bucket : list) {
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- Aggregate aggregate = bucket.aggregations().get("filter_agg");
- String value = bucket.key().stringValue();
- if (StringUtils.isNotEmpty(value)) {
- dto.setName(value);
- 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);
- }
- } else {
- otherNum += 1;
- }
- }
- if (countDetailDTOS.size() > topN) {
- countDetailDTOS.remove(countDetailDTOS.size() - 1);
- }
- EsCountDetailDTO countDTO = new EsCountDetailDTO();
- countDTO.setField(field);
- countDTO.setName("其他");
- countDTO.setTopN(topN);
- Long count = termsAgg.sterms().sumOtherDocCount();
- if (count > 0) {
- countDTO.setNumber(count + otherNum);
- countDetailDTOS.add(countDTO);
- }
- esCountDTO.setRemainingNum(count);
- }
- 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);
- }
- }
- public String getDateFormat(String field,String value) {
- String condition = "";
- if (value.contains("Q") || value.contains("H") || value.length() == 9) {
- String year = value.substring(0, value.indexOf("-"));
- int nextYear = Integer.parseInt(year) + 1;
- String start = "";
- String end = "";
- if (value.contains("Q1")) {
- start = year + "-01";
- end = year + "-04";
- } else if (value.contains("Q2")) {
- start = year + "-04";
- end = year + "-07";
- } else if (value.contains("Q3")) {
- start = year + "-07";
- end = year + "-10";
- } else if (value.contains("Q4")) {
- start = year + "-10";
- end = nextYear + "-01";
- } else if (value.contains("H1")) {
- start = year + "-01";
- end = year + "-07";
- } else if (value.contains("H2")) {
- start = year + "-07";
- end = nextYear + "-01";
- } else if (value.length() == 9) {
- start = year;
- end = value.substring(value.indexOf("-") + 1);
- }
- condition = field + ">=" + start + " " + "AND" + " " + field + "<" + end;
- } else {
- condition = field + " = " + value;
- }
- return condition;
- }
- }
|