123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107 |
- 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.getDatas(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.getDatas(searchCondition, condition, customFields, taskId, projectId,
- esCountVOS, detailDTOS, esCountDTO, value, map);
- }
- }
- } else {
- this.getDatas(searchCondition, "", customFields, taskId, projectId, countVOS,
- detailDTOS, esCountDTO, "", map);
- }
- esCountDTO.setDetailDTOS(detailDTOS);
- return esCountDTO;
- }
- public void getDatas(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);
- // }
- if (!CollectionUtils.isEmpty(customFields)) {
- String customFieldCondition = esService.parseCustomField(customFields, projectId, taskId);
- if (StringUtils.isNotEmpty(searchCondition)) {
- searchCondition = searchCondition + " AND " + customFieldCondition;
- } else {
- searchCondition = customFieldCondition;
- }
- }
- 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");
- Aggregate filterAgg = aggregate.filter().aggregations().get("filterAgg");
- List<StringTermsBucket> termsBuckets = filterAgg.sterms().buckets().array();
- termsBuckets.forEach(termsBucket -> {
- EsCountDetailDTO dto = new EsCountDetailDTO();
- dto.setField(field);
- dto.setName(termsBucket.key().stringValue());
- dto.setNumber(termsBucket.docCount());
- 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 q3 = QueryBuilders.term(i -> i.field("custom_field.if_new").value(1));
- Query bool = QueryBuilders.bool(i -> i.must(q1, q2,q3));
- 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;
- }
- }
|