EsCountService.java 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. package cn.cslg.pas.service.business.es;
  2. import cn.cslg.pas.common.dto.GetUnselectedDTO;
  3. import cn.cslg.pas.common.dto.business.EsCountDTO;
  4. import cn.cslg.pas.common.dto.business.EsCountDetailDTO;
  5. import cn.cslg.pas.common.dto.es.EsCustomFieldValueDTO;
  6. import cn.cslg.pas.common.dto.es.EsDateRangeDTO;
  7. import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
  8. import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
  9. import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
  10. import cn.cslg.pas.common.vo.EsConfigVO;
  11. import cn.cslg.pas.common.vo.business.EsAllCountVO;
  12. import cn.cslg.pas.common.vo.business.EsCountVO;
  13. import cn.cslg.pas.common.vo.es.EsDateRangeVO;
  14. import cn.cslg.pas.domain.es.Patent;
  15. import cn.cslg.pas.factorys.EsCountAnalyseBuilderFactory.EsCountAnalysisBuilderFactory;
  16. import cn.cslg.pas.factorys.EsCountAnalyseBuilderFactory.IEsCountAnalysisBuilder;
  17. import cn.cslg.pas.service.business.CommonService;
  18. import cn.cslg.pas.service.query.FormatQueryService;
  19. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  20. import co.elastic.clients.elasticsearch._types.aggregations.*;
  21. import co.elastic.clients.elasticsearch._types.query_dsl.IdsQuery;
  22. import co.elastic.clients.elasticsearch._types.query_dsl.Query;
  23. import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
  24. import co.elastic.clients.elasticsearch.core.SearchRequest;
  25. import co.elastic.clients.elasticsearch.core.SearchResponse;
  26. import co.elastic.clients.elasticsearch.core.search.Hit;
  27. import com.alibaba.fastjson.JSON;
  28. import lombok.RequiredArgsConstructor;
  29. import org.apache.commons.lang3.StringUtils;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.context.annotation.Lazy;
  32. import org.springframework.stereotype.Service;
  33. import org.springframework.util.CollectionUtils;
  34. import java.io.IOException;
  35. import java.text.ParseException;
  36. import java.text.SimpleDateFormat;
  37. import java.util.*;
  38. import java.util.stream.Collectors;
  39. @Service
  40. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  41. public class EsCountService {
  42. private final List<String> childList = Arrays.asList("field");
  43. private final List<String> nestedList = Arrays.asList("PA", "IN", "PE", "SAT", "SRH","PRCO");
  44. private final List<String> dateList = Arrays.asList("PD", "AD", "GD");
  45. private final List<String> numberList = Arrays.asList("QPN", "QDPN", "SFN", "IFN", "PFN");
  46. private final List<String> nestDateList = Arrays.asList("PRD");
  47. private final List<String> nestChildList = Arrays.asList("MAT","MRH","MIN");
  48. private final ElasticsearchClient client;
  49. @Autowired
  50. private EsCountAnalysisBuilderFactory esCountAnalysisBuilderFactory;
  51. @Autowired
  52. private FormatQueryService formatQueryService;
  53. @Autowired
  54. private EsService esService;
  55. /**
  56. * 聚合统计
  57. *
  58. * @param vo
  59. * @return
  60. * @throws Exception
  61. */
  62. public EsCountDTO esCountAnalysis(EsAllCountVO vo) throws Exception {
  63. List<EsCountVO> countVOS = vo.getCountVOS();
  64. String searchCondition = vo.getCondition();
  65. List<EsCustomFieldValueDTO> customFields = vo.getCustomFields();
  66. Integer taskId = vo.getTaskId();
  67. Integer projectId = vo.getProjectId();
  68. EsCountDTO esCountDTO = new EsCountDTO();
  69. HashMap<String, List<EsCountDetailDTO>> map = new HashMap<>();
  70. List<EsCountDetailDTO> detailDTOS = new ArrayList<>();
  71. if (countVOS.size() > 1) {
  72. EsCountVO countVO = countVOS.get(0);
  73. List<String> values = countVO.getValues();
  74. values.removeIf(i -> i.equals("其他"));
  75. List<EsCountVO> esCountVOS = new ArrayList<>();
  76. esCountVOS.add(countVOS.get(1));
  77. if (StringUtils.isNotEmpty(countVO.getFieldId())) {
  78. if (!CollectionUtils.isEmpty(values)) {
  79. for (String value : values) {
  80. EsCustomFieldValueDTO valueDTO = new EsCustomFieldValueDTO();
  81. valueDTO.setFieldId(countVO.getFieldId());
  82. valueDTO.setFieldValue(Arrays.asList(value));
  83. customFields.add(valueDTO);
  84. this.getReturnData(searchCondition, "",customFields, taskId, projectId,
  85. esCountVOS, detailDTOS, esCountDTO, value, map);
  86. }
  87. }
  88. } else {
  89. for (String value : values) {
  90. String condition = "";
  91. if (dateList.contains(countVO.getField())) {
  92. condition = this.getDateFormat(countVO.getField(), value);
  93. } else {
  94. condition = countVO.getField() + " = " + value;
  95. }
  96. this.getReturnData(searchCondition, condition, customFields, taskId, projectId,
  97. esCountVOS, detailDTOS, esCountDTO, value, map);
  98. }
  99. }
  100. } else {
  101. this.getReturnData(searchCondition, "", customFields, taskId, projectId, countVOS,
  102. detailDTOS, esCountDTO, "", map);
  103. }
  104. esCountDTO.setDetailDTOS(detailDTOS);
  105. return esCountDTO;
  106. }
  107. public String getDateFormat(String field,String value) {
  108. String condition = "";
  109. if (value.contains("Q") || value.contains("H") || value.length() == 9) {
  110. String year = value.substring(0, value.indexOf("-"));
  111. int nextYear = Integer.parseInt(year) + 1;
  112. String start = "";
  113. String end = "";
  114. if (value.contains("Q1")) {
  115. start = year + "-01";
  116. end = year + "-04";
  117. } else if (value.contains("Q2")) {
  118. start = year + "-04";
  119. end = year + "-07";
  120. } else if (value.contains("Q3")) {
  121. start = year + "-07";
  122. end = year + "-10";
  123. } else if (value.contains("Q4")) {
  124. start = year + "-10";
  125. end = nextYear + "-01";
  126. } else if (value.contains("H1")) {
  127. start = year + "-01";
  128. end = year + "-07";
  129. } else if (value.contains("H2")) {
  130. start = year + "-07";
  131. end = nextYear + "-01";
  132. } else if (value.length() == 9) {
  133. start = year;
  134. end = value.substring(value.indexOf("-") + 1);
  135. }
  136. condition = field + ">=" + start + " " + "AND" + " " + field + "<" + end;
  137. } else {
  138. condition = field + " = " + value;
  139. }
  140. return condition;
  141. }
  142. public void getReturnData(String searchCondition,String condition, List<EsCustomFieldValueDTO> customFields, Integer taskId,
  143. Integer projectId, List<EsCountVO> countVOS, List<EsCountDetailDTO> detailDTOS,
  144. EsCountDTO esCountDTO, String firstName, Map<String, List<EsCountDetailDTO>> map) throws Exception {
  145. if (StringUtils.isNotEmpty(condition)) {
  146. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  147. searchCondition = condition + " AND " + searchCondition;
  148. } else {
  149. searchCondition = condition;
  150. }
  151. }
  152. if (!CollectionUtils.isEmpty(customFields)) {
  153. searchCondition = esService.parseCustomField(customFields,projectId,taskId);
  154. }
  155. searchCondition = this.getCondition(searchCondition, taskId, projectId);
  156. SearchRequest.Builder builder = new SearchRequest.Builder();
  157. //设置查询索引
  158. builder.index("patent");
  159. //设置查询索引
  160. Query query = null;
  161. if (StringUtils.isNotEmpty(searchCondition)) {
  162. //1. 解析检索条件
  163. treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
  164. //格式化检索式
  165. //3. 从es中检索数据
  166. query = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
  167. }
  168. for (EsCountVO countVO : countVOS) {
  169. String field = countVO.getField();
  170. Integer topN = countVO.getTopN();
  171. Integer fieldType = null;
  172. String type = countVO.getFieldType();
  173. if (StringUtils.isNotEmpty(type) && type.equals("tree")) {
  174. fieldType = 6;
  175. }
  176. String format = countVO.getFormat();
  177. List<String> values = countVO.getValues();
  178. Aggregation aggregation = this.getAggregation(countVO, projectId, taskId);
  179. if (query != null) {
  180. Query finalQuery = query;
  181. Aggregation finalAggregation = aggregation;
  182. Aggregation filtersAgg = new Aggregation.Builder().filters(new FiltersAggregation.Builder()
  183. .filters(i -> i.array(Arrays.asList(finalQuery))).build())
  184. .aggregations(new HashMap() {{
  185. put("filters_agg", finalAggregation);
  186. }}).build();
  187. builder.aggregations("Agg", filtersAgg);
  188. } else {
  189. builder.aggregations("Agg", aggregation);
  190. }
  191. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  192. Aggregate agg = response.aggregations().get("Agg");
  193. if (query != null) {
  194. String finalSearchCondition = searchCondition;
  195. if (StringUtils.isNotEmpty(field)) {
  196. List<FiltersBucket> filtersBuckets = agg.filters().buckets().array();
  197. if (dateList.contains(field)) {
  198. if (CollectionUtils.isEmpty(values)) {
  199. filtersBuckets.forEach(filtersBucket -> {
  200. Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
  201. try {
  202. this.getDateCountDTOS(filtersAgg, field, topN, format, detailDTOS, esCountDTO);
  203. } catch (ParseException e) {
  204. e.printStackTrace();
  205. }
  206. });
  207. } else {
  208. filtersBuckets.forEach(filtersBucket -> {
  209. Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
  210. this.getDateAnalysisDTOS(filtersAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
  211. });
  212. }
  213. } else if (nestedList.contains(field)) {
  214. filtersBuckets.forEach(filtersBucket -> {
  215. Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
  216. this.getNestedCountDTOS(filtersAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
  217. if (!CollectionUtils.isEmpty(values)) {
  218. esCountDTO.setAllNumber(filtersAgg.nested().docCount());
  219. }
  220. });
  221. } else if (nestDateList.contains(field)) {
  222. if (CollectionUtils.isEmpty(values)) {
  223. filtersBuckets.forEach(filtersBucket -> {
  224. Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
  225. Aggregate termsAgg = filtersAgg.nested().aggregations().get("terms_agg");
  226. try {
  227. this.getDateCountDTOS(termsAgg, field, topN, format, detailDTOS, esCountDTO);
  228. } catch (ParseException e) {
  229. e.printStackTrace();
  230. }
  231. });
  232. } else {
  233. filtersBuckets.forEach(filtersBucket -> {
  234. Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
  235. Aggregate termsAgg = filtersAgg.nested().aggregations().get("terms_agg");
  236. this.getDateAnalysisDTOS(termsAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
  237. });
  238. }
  239. } else if (nestChildList.contains(field)) {
  240. filtersBuckets.forEach(filtersBucket -> {
  241. Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
  242. Aggregate childAgg = filtersAgg.children().aggregations().get("childAgg");
  243. this.getNestedCountDTOS(childAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
  244. if (!CollectionUtils.isEmpty(values)) {
  245. esCountDTO.setAllNumber(childAgg.nested().docCount());
  246. }
  247. });
  248. } else if (childList.contains(field)) {
  249. if (CollectionUtils.isEmpty(values)) {
  250. Integer finalFieldType = fieldType;
  251. filtersBuckets.forEach(filtersBucket -> {
  252. Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
  253. try {
  254. this.getChildCountDTOS(filtersAgg, field, finalFieldType, topN, detailDTOS, esCountDTO,projectId,taskId);
  255. } catch (Exception e) {
  256. e.printStackTrace();
  257. }
  258. });
  259. } else {
  260. Integer finalFieldType1 = fieldType;
  261. filtersBuckets.forEach(filtersBucket -> {
  262. Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
  263. this.getChildAnalysisDTOS(filtersAgg, field, finalFieldType1, topN,
  264. detailDTOS, esCountDTO, firstName, map, values);
  265. });
  266. }
  267. } else if (numberList.contains(field)) {
  268. if (CollectionUtils.isEmpty(values)) {
  269. } else {
  270. filtersBuckets.forEach(filtersBucket -> {
  271. Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
  272. this.getNumberAnalysisDTOS(filtersAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
  273. });
  274. }
  275. } else {
  276. filtersBuckets.forEach(filtersBucket -> {
  277. Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
  278. this.getTermCountDTOS(filtersAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
  279. });
  280. }
  281. } else {
  282. this.getFilterCountDTO(agg, finalSearchCondition, detailDTOS);
  283. }
  284. } else {
  285. if (dateList.contains(field)) {
  286. if (CollectionUtils.isEmpty(values)) {
  287. this.getDateCountDTOS(agg, field, topN, format, detailDTOS, esCountDTO);
  288. } else {
  289. this.getDateAnalysisDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
  290. }
  291. } else if (nestedList.contains(field)) {
  292. this.getNestedCountDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
  293. if (!CollectionUtils.isEmpty(values)) {
  294. esCountDTO.setAllNumber(agg.nested().docCount());
  295. }
  296. } else if (nestDateList.contains(field)) {
  297. Aggregate termsAgg = agg.nested().aggregations().get("terms_agg");
  298. this.getDateCountDTOS(termsAgg, field, topN, format, detailDTOS, esCountDTO);
  299. } else if (nestChildList.contains(field)) {
  300. Aggregate childAgg = agg.children().aggregations().get("childAgg");
  301. this.getNestedCountDTOS(childAgg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
  302. if (!CollectionUtils.isEmpty(values)) {
  303. esCountDTO.setAllNumber(childAgg.nested().docCount());
  304. }
  305. } else if (childList.contains(field)) {
  306. if (CollectionUtils.isEmpty(values)) {
  307. this.getChildCountDTOS(agg, field, fieldType, topN, detailDTOS, esCountDTO, projectId, taskId);
  308. } else {
  309. this.getChildAnalysisDTOS(agg, field, fieldType, topN,
  310. detailDTOS, esCountDTO, firstName, map, values);
  311. }
  312. } else if (numberList.contains(field)) {
  313. this.getNumberAnalysisDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
  314. } else {
  315. this.getTermCountDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
  316. }
  317. }
  318. esCountDTO.setCondition(searchCondition);
  319. esCountDTO.setAnalyseMap(map);
  320. }
  321. }
  322. public EsDateRangeDTO getFieldRange(EsDateRangeVO rangeVO) throws Exception {
  323. String searchCondition = rangeVO.getCondition();
  324. String field = rangeVO.getField();
  325. List<EsCustomFieldValueDTO> customFields = rangeVO.getCustomFields();
  326. if (!CollectionUtils.isEmpty(customFields)) {
  327. searchCondition = esService.parseCustomField(customFields, rangeVO.getProjectId(), rangeVO.getTaskId());
  328. }
  329. Integer taskId = rangeVO.getTaskId();
  330. Integer projectId = rangeVO.getProjectId();
  331. searchCondition = this.getCondition(searchCondition, taskId, projectId);
  332. SearchRequest.Builder builder = new SearchRequest.Builder();
  333. //设置查询索引
  334. builder.index("patent");
  335. //设置查询索引
  336. Query query = null;
  337. if (StringUtils.isNotEmpty(searchCondition)) {
  338. //1. 解析检索条件
  339. treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
  340. //格式化检索式
  341. //3. 从es中检索数据
  342. query = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
  343. }
  344. String esField = "";
  345. if (StringUtils.isNotEmpty(field)) {
  346. switch (field) {
  347. case "AD":
  348. esField = "app_date";
  349. break;
  350. case "PD":
  351. esField = "public_date";
  352. break;
  353. case "GD":
  354. esField = "grant_date";
  355. break;
  356. }
  357. }
  358. EsDateRangeDTO rangeDTO = new EsDateRangeDTO();
  359. if (StringUtils.isNotEmpty(esField)) {
  360. String finalEsField = esField;
  361. Query finalQuery = query;
  362. Aggregation min = AggregationBuilders.min(i -> i.field(finalEsField).format("yyyy"));
  363. Aggregation filtersAgg = new Aggregation.Builder().filters(new FiltersAggregation.Builder()
  364. .filters(i -> i.array(Arrays.asList(finalQuery))).build())
  365. .aggregations(new HashMap() {{
  366. put("filters_agg", min);
  367. }}).build();
  368. Aggregation max = AggregationBuilders.max(i -> i.field(finalEsField).format("yyyy"));
  369. Aggregation maxFilters = new Aggregation.Builder().filters(new FiltersAggregation.Builder()
  370. .filters(i -> i.array(Arrays.asList(finalQuery))).build())
  371. .aggregations(new HashMap() {{
  372. put("max_agg", max);
  373. }}).build();
  374. builder.aggregations("minAgg", filtersAgg);
  375. builder.aggregations("maxAgg", maxFilters);
  376. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  377. Aggregate minAgg = response.aggregations().get("minAgg");
  378. if (minAgg != null) {
  379. List<FiltersBucket> list = minAgg.filters().buckets().array();
  380. for (FiltersBucket bucket : list) {
  381. Aggregate aggregate = bucket.aggregations().get("filters_agg");
  382. String minValue = aggregate.min().valueAsString();
  383. rangeDTO.setMinDate(minValue);
  384. }
  385. }
  386. Aggregate maxAgg = response.aggregations().get("maxAgg");
  387. if (maxAgg != null) {
  388. List<FiltersBucket> list = maxAgg.filters().buckets().array();
  389. for (FiltersBucket bucket : list) {
  390. Aggregate aggregate = bucket.aggregations().get("max_agg");
  391. String maxValue = aggregate.max().valueAsString();
  392. rangeDTO.setMaxDate(maxValue);
  393. }
  394. }
  395. }
  396. return rangeDTO;
  397. }
  398. /**
  399. * 查询共用Aggregation
  400. *
  401. * @param countVO
  402. * @return
  403. * @throws Exception
  404. */
  405. public Aggregation getAggregation(EsCountVO countVO, Integer projectId, Integer taskId) throws Exception {
  406. String field = countVO.getField();
  407. Integer topN = countVO.getTopN();
  408. Boolean ifHaveChild = countVO.getIfHaveChild();
  409. String fieldId = countVO.getFieldId();
  410. Integer fieldType = null;
  411. String type = countVO.getFieldType();
  412. if (StringUtils.isNotEmpty(type) && type.equals("tree")) {
  413. fieldType = 6;
  414. }
  415. String format = countVO.getFormat();
  416. String valueOne = countVO.getValueOne();
  417. String valueTwo = countVO.getValueTwo();
  418. List<String> values = countVO.getValues();
  419. IEsCountAnalysisBuilder iEsCountAnalysisBuilder = null;
  420. String json = CommonService.readJsonFile("esCountAnalysis.json");
  421. List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
  422. EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getField().equals(field))
  423. .findFirst().orElse(null);
  424. Aggregation aggregation = null;
  425. if (esConfigVO != null) {
  426. iEsCountAnalysisBuilder = esCountAnalysisBuilderFactory.getClass(esConfigVO.getEsClass());
  427. iEsCountAnalysisBuilder.setField(esConfigVO.getEsField());
  428. iEsCountAnalysisBuilder.setValueOne(valueOne);
  429. iEsCountAnalysisBuilder.setValueTwo(valueTwo);
  430. iEsCountAnalysisBuilder.setValues(values);
  431. iEsCountAnalysisBuilder.setFieldId(fieldId);
  432. iEsCountAnalysisBuilder.setFieldType(String.valueOf(fieldType));
  433. iEsCountAnalysisBuilder.setTopN(topN);
  434. iEsCountAnalysisBuilder.setFormat(format);
  435. iEsCountAnalysisBuilder.setProjectId(projectId);
  436. iEsCountAnalysisBuilder.setTaskId(taskId);
  437. iEsCountAnalysisBuilder.setIfHaveChild(ifHaveChild);
  438. if (iEsCountAnalysisBuilder.getField().contains(".")) {
  439. String path = iEsCountAnalysisBuilder.getField()
  440. .substring(0, iEsCountAnalysisBuilder.getField().indexOf("."));
  441. iEsCountAnalysisBuilder.setPath(path);
  442. }
  443. aggregation = iEsCountAnalysisBuilder.createCountAnalyseAgg();
  444. }
  445. return aggregation;
  446. }
  447. /**
  448. * 获取筛选条件
  449. *
  450. * @param taskId
  451. * @param projectId
  452. * @return
  453. */
  454. public String getCondition(String searchCondition, Integer taskId, Integer projectId) {
  455. if (taskId != null) {
  456. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  457. searchCondition = "taskId = " + taskId + " AND " + searchCondition;
  458. } else {
  459. searchCondition = "taskId = " + taskId;
  460. }
  461. } else {
  462. if (projectId != null) {
  463. if (searchCondition != null && !"".equals(searchCondition.trim())) {
  464. searchCondition = "projectId = " + projectId + " AND " + searchCondition;
  465. } else {
  466. searchCondition = "projectId = " + projectId;
  467. }
  468. }
  469. }
  470. return searchCondition;
  471. }
  472. /**
  473. * 获取Filter聚合返回数据
  474. *
  475. * @param agg
  476. * @param condition
  477. * @return
  478. */
  479. public void getFilterCountDTO(Aggregate agg, String condition, List<EsCountDetailDTO> detailDTOS) {
  480. EsCountDetailDTO filterDTO = new EsCountDetailDTO();
  481. filterDTO.setField("condition");
  482. filterDTO.setName(condition);
  483. filterDTO.setNumber(agg.filter().docCount());
  484. if (filterDTO.getNumber() > 0) {
  485. detailDTOS.add(filterDTO);
  486. }
  487. }
  488. /**
  489. * 获取Terms聚合后数据
  490. *
  491. * @param agg
  492. * @param field
  493. * @param detailDTOS
  494. */
  495. public void getTermCountDTOS(Aggregate agg, String field, Integer topN, List<EsCountDetailDTO> detailDTOS,
  496. EsCountDTO esCountDTO, String firstName,
  497. Map<String, List<EsCountDetailDTO>> map,List<String> values) {
  498. List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
  499. EsCountDetailDTO countDTO = new EsCountDetailDTO();
  500. countDTO.setField(field);
  501. countDTO.setName("其他");
  502. countDTO.setTopN(topN);
  503. Long count = agg.sterms().sumOtherDocCount();
  504. if (count > 0) {
  505. countDTO.setNumber(count);
  506. detailDTOS.add(countDTO);
  507. }
  508. List<StringTermsBucket> list = agg.sterms().buckets().array();
  509. list.forEach(bucket -> {
  510. EsCountDetailDTO dto = new EsCountDetailDTO();
  511. dto.setField(field);
  512. Aggregate aggregate = bucket.aggregations().get("filter_agg");
  513. dto.setName(bucket.key().stringValue());
  514. dto.setNumber(bucket.docCount());
  515. dto.setTopN(topN);
  516. dto.setFirstName(firstName);
  517. if (aggregate != null) {
  518. dto.setNumber(aggregate.filter().docCount());
  519. }
  520. if (dto.getNumber() > 0) {
  521. countDetailDTOS.add(dto);
  522. }
  523. });
  524. List<String> strs = new ArrayList<>();
  525. if (!CollectionUtils.isEmpty(values)) {
  526. strs.addAll(values);
  527. }
  528. if (StringUtils.isNotEmpty(firstName)) {
  529. countDetailDTOS.removeIf(dto -> dto.getName().equals("其他"));
  530. if (countDetailDTOS.size() != values.size()) {
  531. for (EsCountDetailDTO detailDTO : countDetailDTOS) {
  532. strs.removeIf(i -> i.equals(detailDTO.getName()));
  533. }
  534. for (String value : strs) {
  535. EsCountDetailDTO detailDTO = new EsCountDetailDTO();
  536. detailDTO.setName(value);
  537. detailDTO.setFirstName(firstName);
  538. detailDTO.setField(field);
  539. detailDTO.setNumber(0L);
  540. detailDTO.setTopN(topN);
  541. countDetailDTOS.add(detailDTO);
  542. }
  543. } else if (countDetailDTOS.isEmpty()) {
  544. for (String value : values) {
  545. EsCountDetailDTO detailDTO = new EsCountDetailDTO();
  546. detailDTO.setName(value);
  547. detailDTO.setFirstName(firstName);
  548. detailDTO.setField(field);
  549. detailDTO.setNumber(0L);
  550. detailDTO.setTopN(topN);
  551. countDetailDTOS.add(detailDTO);
  552. }
  553. }
  554. map.put(firstName, countDetailDTOS);
  555. } else {
  556. detailDTOS.addAll(countDetailDTOS);
  557. }
  558. }
  559. /**
  560. * 获取children聚合后数据
  561. *
  562. * @param agg
  563. * @param field
  564. * @param detailDTOS
  565. */
  566. public void getChildCountDTOS(Aggregate agg, String field, Integer fieldType, Integer topN,
  567. List<EsCountDetailDTO> detailDTOS, EsCountDTO esCountDTO,Integer projectId, Integer taskId) throws Exception {
  568. esCountDTO.setAllNumber(agg.children().docCount());
  569. Aggregate childAgg = agg.children().aggregations().get("childAgg");
  570. List<StringTermsBucket> list = childAgg.sterms().buckets().array();
  571. list.forEach(bucket -> {
  572. Aggregate aggregate = bucket.aggregations().get("termAgg");
  573. List<StringTermsBucket> termsBuckets = aggregate.sterms().buckets().array();
  574. termsBuckets.forEach(termsBucket -> {
  575. Aggregate termAgg = termsBucket.aggregations().get("filterAgg");
  576. long count = termAgg.filter().docCount();
  577. if (count > 0) {
  578. EsCountDetailDTO dto = new EsCountDetailDTO();
  579. dto.setField(field);
  580. dto.setName(termsBucket.key().stringValue());
  581. dto.setNumber(termsBucket.docCount());
  582. dto.setNumber(count);
  583. dto.setTopN(topN);
  584. if (dto.getNumber() > 0) {
  585. detailDTOS.add(dto);
  586. }
  587. }
  588. });
  589. });
  590. if (fieldType == null || fieldType != 6) {
  591. GetUnselectedDTO unselectedDTO = this.getUnselectedCustomNum(projectId, taskId);
  592. EsCountDetailDTO detail = new EsCountDetailDTO();
  593. detail.setField(field);
  594. detail.setName("未选择");
  595. detail.setNumber(unselectedDTO.getNumber().longValue());
  596. detail.setTopN(topN);
  597. if (!detailDTOS.contains(detail)) {
  598. detailDTOS.add(detail);
  599. }
  600. }
  601. }
  602. public GetUnselectedDTO getUnselectedCustomNum(Integer projectId, Integer taskId) throws Exception {
  603. GetUnselectedDTO dto = new GetUnselectedDTO();
  604. SearchRequest.Builder builder = new SearchRequest.Builder();
  605. //设置查询索引
  606. builder.index("patent");
  607. Query q = null;
  608. if (taskId != null) {
  609. Query query = QueryBuilders.term(i -> i.field("project_task.task_id").value(taskId));
  610. q = QueryBuilders.hasChild(i -> i.type("task").query(query));
  611. } else {
  612. Query query = QueryBuilders.term(i -> i.field("project_id").value(projectId));
  613. q = QueryBuilders.hasChild(i -> i.type("project").query(query));
  614. }
  615. builder.size(9999);
  616. builder.query(q);
  617. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  618. List<Hit<Patent>> hits = response.hits().hits();
  619. List<String> list = new ArrayList<>();
  620. for (Hit<Patent> hit : hits) {
  621. String id = hit.id();
  622. list.add(id);
  623. }
  624. if (!CollectionUtils.isEmpty(list)) {
  625. dto = this.ifExistChild(list);
  626. }
  627. return dto;
  628. }
  629. public GetUnselectedDTO ifExistChild(List<String> list) throws IOException {
  630. GetUnselectedDTO dto = new GetUnselectedDTO();
  631. List<String> existChildIds = new ArrayList<>();
  632. List<String> beinglessChildIds = new ArrayList<>();
  633. int count = 0;
  634. for (String id : list) {
  635. SearchRequest.Builder builder = new SearchRequest.Builder();
  636. //设置查询索引
  637. builder.index("patent");
  638. Query q1 = QueryBuilders.parentId(i -> i.type("project_customfield").id(id));
  639. Query q2 = QueryBuilders.exists(i -> i.field("custom_field"));
  640. Query bool = QueryBuilders.bool(i -> i.must(q1, q2));
  641. builder.query(bool);
  642. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  643. Long total = response.hits().total().value();
  644. if (total > 0) {
  645. existChildIds.add(id);
  646. } else {
  647. beinglessChildIds.add(id);
  648. count++;
  649. }
  650. }
  651. dto.setNumber(count);
  652. dto.setExistChildIds(existChildIds);
  653. dto.setBeinglessChildIds(beinglessChildIds);
  654. return dto;
  655. }
  656. /**
  657. * 获取children分析后数据
  658. *
  659. * @param agg
  660. * @param field
  661. * @param detailDTOS
  662. */
  663. public void getChildAnalysisDTOS(Aggregate agg, String field, Integer fieldType, Integer topN,
  664. List<EsCountDetailDTO> detailDTOS, EsCountDTO esCountDTO,
  665. String firstName, Map<String, List<EsCountDetailDTO>> map,List<String> values) {
  666. List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
  667. Aggregate childAgg = agg.children().aggregations().get("childAgg");
  668. List<StringTermsBucket> list = childAgg.sterms().buckets().array();
  669. list.forEach(bucket -> {
  670. esCountDTO.setAllNumber(bucket.docCount());
  671. Aggregate aggregate = bucket.aggregations().get("termAgg");
  672. List<StringTermsBucket> termsBuckets = aggregate.sterms().buckets().array();
  673. termsBuckets.forEach(termsBucket -> {
  674. EsCountDetailDTO dto = new EsCountDetailDTO();
  675. dto.setField(field);
  676. dto.setName(termsBucket.key().stringValue());
  677. dto.setNumber(termsBucket.docCount());
  678. dto.setFirstName(firstName);
  679. dto.setTopN(topN);
  680. countDetailDTOS.add(dto);
  681. if (fieldType == null || fieldType != 6) {
  682. Long docCount = aggregate.sterms().sumOtherDocCount();
  683. EsCountDetailDTO detail = new EsCountDetailDTO();
  684. detail.setField(field);
  685. detail.setName("未选择");
  686. detail.setNumber(docCount);
  687. detail.setTopN(topN);
  688. if (!countDetailDTOS.contains(detail)) {
  689. countDetailDTOS.add(detail);
  690. }
  691. }
  692. });
  693. });
  694. List<String> strs = new ArrayList<>();
  695. if (!CollectionUtils.isEmpty(values)) {
  696. strs.addAll(values);
  697. }
  698. if (StringUtils.isNotEmpty(firstName)) {
  699. countDetailDTOS.removeIf(dto -> dto.getName().equals("其他"));
  700. if (countDetailDTOS.size() != values.size()) {
  701. for (EsCountDetailDTO detailDTO : countDetailDTOS) {
  702. strs.removeIf(i -> i.equals(detailDTO.getName()));
  703. }
  704. for (String value : strs) {
  705. EsCountDetailDTO detailDTO = new EsCountDetailDTO();
  706. detailDTO.setName(value);
  707. detailDTO.setFirstName(firstName);
  708. detailDTO.setField(field);
  709. detailDTO.setNumber(0L);
  710. detailDTO.setTopN(topN);
  711. countDetailDTOS.add(detailDTO);
  712. }
  713. } else if (countDetailDTOS.isEmpty()) {
  714. for (String value : values) {
  715. EsCountDetailDTO detailDTO = new EsCountDetailDTO();
  716. detailDTO.setName(value);
  717. detailDTO.setFirstName(firstName);
  718. detailDTO.setField(field);
  719. detailDTO.setNumber(0L);
  720. detailDTO.setTopN(topN);
  721. countDetailDTOS.add(detailDTO);
  722. }
  723. }
  724. map.put(firstName, countDetailDTOS);
  725. } else {
  726. detailDTOS.addAll(countDetailDTOS);
  727. }
  728. }
  729. /**
  730. * 获取range分析后数据
  731. *
  732. * @param agg
  733. * @param field
  734. * @param detailDTOS
  735. */
  736. public void getNumberAnalysisDTOS(Aggregate agg, String field, Integer topN, List<EsCountDetailDTO> detailDTOS,
  737. EsCountDTO esCountDTO, String firstName,
  738. Map<String, List<EsCountDetailDTO>> map,List<String> values) {
  739. List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
  740. List<RangeBucket> list = agg.range().buckets().array();
  741. for (RangeBucket bucket : list) {
  742. EsCountDetailDTO dto = new EsCountDetailDTO();
  743. dto.setField(field);
  744. dto.setName(bucket.key());
  745. dto.setNumber(bucket.docCount());
  746. dto.setTopN(topN);
  747. dto.setFirstName(firstName);
  748. countDetailDTOS.add(dto);
  749. // if (dto.getNumber() > 0) {
  750. // detailDTOS.add(dto);
  751. // }
  752. }
  753. List<String> strs = new ArrayList<>();
  754. if (!CollectionUtils.isEmpty(values)) {
  755. strs.addAll(values);
  756. }
  757. if (StringUtils.isNotEmpty(firstName)) {
  758. countDetailDTOS.removeIf(dto -> dto.getName().equals("其他"));
  759. if (countDetailDTOS.size() != values.size()) {
  760. for (EsCountDetailDTO detailDTO : countDetailDTOS) {
  761. strs.removeIf(i -> i.equals(detailDTO.getName()));
  762. }
  763. for (String value : strs) {
  764. EsCountDetailDTO detailDTO = new EsCountDetailDTO();
  765. detailDTO.setName(value);
  766. detailDTO.setFirstName(firstName);
  767. detailDTO.setField(field);
  768. detailDTO.setNumber(0L);
  769. detailDTO.setTopN(topN);
  770. countDetailDTOS.add(detailDTO);
  771. }
  772. } else if (countDetailDTOS.isEmpty()) {
  773. for (String value : values) {
  774. EsCountDetailDTO detailDTO = new EsCountDetailDTO();
  775. detailDTO.setName(value);
  776. detailDTO.setFirstName(firstName);
  777. detailDTO.setField(field);
  778. detailDTO.setNumber(0L);
  779. detailDTO.setTopN(topN);
  780. countDetailDTOS.add(detailDTO);
  781. }
  782. }
  783. map.put(firstName, countDetailDTOS);
  784. } else {
  785. detailDTOS.addAll(countDetailDTOS);
  786. }
  787. }
  788. /**
  789. * 获取dateHistogram聚合后数据
  790. *
  791. * @param agg
  792. * @param field
  793. * @param detailDTOS
  794. */
  795. public void getDateCountDTOS(Aggregate agg, String field, Integer topN, String format, List<EsCountDetailDTO> detailDTOS, EsCountDTO esCountDTO) throws ParseException {
  796. List<DateHistogramBucket> list = agg.dateHistogram().buckets().array();
  797. esCountDTO.setAllNumber(Long.valueOf(String.valueOf(list.size())));
  798. List<EsCountDetailDTO> esCountDetailDTOS = new ArrayList<>();
  799. if (StringUtils.isNotEmpty(format) && format.equals("半年")) {
  800. List<EsCountDetailDTO> detailDTOList = new ArrayList<>();
  801. for (DateHistogramBucket bucket : list) {
  802. String year = bucket.keyAsString();
  803. SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
  804. String startMonth = year + "-01";
  805. Calendar calendar = Calendar.getInstance();
  806. Date date = monthFormat.parse(startMonth);
  807. calendar.setTime(date);
  808. calendar.add(Calendar.MONTH, 6);
  809. Date halfYearDate = calendar.getTime();
  810. Aggregate aggregate = bucket.aggregations().get("halfYearAgg");
  811. List<DateHistogramBucket> buckets = aggregate.dateHistogram().buckets().array();
  812. long h1Count = 0;
  813. long h2Count = 0;
  814. for (DateHistogramBucket histogramBucket : buckets) {
  815. String key = histogramBucket.keyAsString();
  816. Date month = monthFormat.parse(key);
  817. EsCountDetailDTO dto = new EsCountDetailDTO();
  818. dto.setField(field);
  819. dto.setName(year + "-H1");
  820. dto.setNumber(h1Count);
  821. dto.setTopN(topN);
  822. EsCountDetailDTO dto2 = new EsCountDetailDTO();
  823. dto2.setField(field);
  824. dto2.setName(year + "-H2");
  825. dto2.setNumber(h2Count);
  826. dto2.setTopN(topN);
  827. if (month.before(halfYearDate)) {
  828. h1Count += histogramBucket.docCount();
  829. dto.setNumber(h1Count);
  830. }
  831. if (month.after(halfYearDate)) {
  832. h2Count += histogramBucket.docCount();
  833. dto2.setNumber(h2Count);
  834. }
  835. detailDTOList.add(dto);
  836. detailDTOList.add(dto2);
  837. }
  838. }
  839. Map<String, List<EsCountDetailDTO>> map = detailDTOList.stream().collect(Collectors.groupingBy(EsCountDetailDTO::getName));
  840. for (String key : map.keySet()) {
  841. List<EsCountDetailDTO> dtoList = map.get(key);
  842. long sum = dtoList.stream().mapToLong(EsCountDetailDTO::getNumber).sum();
  843. EsCountDetailDTO detailDTO = dtoList.get(0);
  844. detailDTO.setNumber(sum);
  845. esCountDetailDTOS.add(detailDTO);
  846. }
  847. } else if (StringUtils.isNotEmpty(format) && format.equals("季")) {
  848. for (DateHistogramBucket bucket : list) {
  849. String yearMonth = bucket.keyAsString();
  850. String year = yearMonth.substring(0, yearMonth.indexOf("-"));
  851. String firstQuarter = year + "-01";
  852. String secondQuarter = year + "-04";
  853. String thirdQuarter = year + "-07";
  854. String forthQuarter = year + "-10";
  855. String firstQ = year + "-Q1";
  856. String secondQ = year + "-Q2";
  857. String thirdQ = year + "-Q3";
  858. String forthQ = year + "-Q4";
  859. EsCountDetailDTO dto = new EsCountDetailDTO();
  860. dto.setField(field);
  861. Aggregate aggregate = bucket.aggregations().get("filter_agg");
  862. if (yearMonth.equals(firstQuarter)) {
  863. dto.setName(firstQ);
  864. } else if (yearMonth.equals(secondQuarter)) {
  865. dto.setName(secondQ);
  866. } else if (yearMonth.equals(thirdQuarter)) {
  867. dto.setName(thirdQ);
  868. } else if (yearMonth.equals(forthQuarter)) {
  869. dto.setName(forthQ);
  870. }
  871. dto.setNumber(bucket.docCount());
  872. dto.setTopN(topN);
  873. if (aggregate != null) {
  874. dto.setNumber(aggregate.filter().docCount());
  875. }
  876. esCountDetailDTOS.add(dto);
  877. }
  878. } else {
  879. list.forEach(bucket -> {
  880. EsCountDetailDTO dto = new EsCountDetailDTO();
  881. dto.setField(field);
  882. Aggregate aggregate = bucket.aggregations().get("filter_agg");
  883. dto.setName(bucket.keyAsString());
  884. dto.setNumber(bucket.docCount());
  885. dto.setTopN(topN);
  886. if (aggregate != null) {
  887. dto.setNumber(aggregate.filter().docCount());
  888. }
  889. esCountDetailDTOS.add(dto);
  890. });
  891. }
  892. if (!CollectionUtils.isEmpty(esCountDetailDTOS)) {
  893. List<EsCountDetailDTO> collect = esCountDetailDTOS.stream()
  894. .sorted(Comparator.comparing(EsCountDetailDTO::getName).reversed()).limit(topN).collect(Collectors.toList());
  895. detailDTOS.addAll(collect);
  896. }
  897. // EsCountDetailDTO countDTO = new EsCountDetailDTO();
  898. // countDTO.setField(field);
  899. // countDTO.setName("其他");
  900. // if (list.size() > topN) {
  901. // int sum = list.size() - topN;
  902. // countDTO.setNumber(Long.valueOf(String.valueOf(sum)));
  903. //
  904. // } else {
  905. // countDTO.setNumber(0L);
  906. // }
  907. // countDTO.setTopN(topN);
  908. // detailDTOS.add(countDTO);
  909. }
  910. /**
  911. * 获取dateHistogram分析后数据
  912. *
  913. * @param agg
  914. * @param field
  915. * @param topN
  916. * @param detailDTOS
  917. */
  918. public void getDateAnalysisDTOS(Aggregate agg, String field, Integer topN, List<EsCountDetailDTO> detailDTOS,
  919. EsCountDTO esCountDTO, String firstName,
  920. Map<String, List<EsCountDetailDTO>> map,List<String> values) {
  921. List<RangeBucket> list1 = agg.dateRange().buckets().array();
  922. List<EsCountDetailDTO> esCountDetailDTOS = new ArrayList<>();
  923. for (RangeBucket bucket : list1) {
  924. EsCountDetailDTO dto = new EsCountDetailDTO();
  925. dto.setFirstName(firstName);
  926. dto.setField(field);
  927. dto.setName(bucket.key());
  928. dto.setNumber(bucket.docCount());
  929. dto.setTopN(topN);
  930. esCountDetailDTOS.add(dto);
  931. }
  932. if (!CollectionUtils.isEmpty(esCountDetailDTOS)) {
  933. List<EsCountDetailDTO> collect = esCountDetailDTOS.stream()
  934. .sorted(Comparator.comparing(EsCountDetailDTO::getName).reversed()).limit(topN).collect(Collectors.toList());
  935. List<String> strs = new ArrayList<>();
  936. if (!CollectionUtils.isEmpty(values)) {
  937. strs.addAll(values);
  938. }
  939. if (StringUtils.isNotEmpty(firstName)) {
  940. collect.removeIf(dto -> dto.getName().equals("其他"));
  941. if (collect.size() != values.size()) {
  942. for (EsCountDetailDTO detailDTO : collect) {
  943. strs.removeIf(i -> i.equals(detailDTO.getName()));
  944. }
  945. for (String value : strs) {
  946. EsCountDetailDTO detailDTO = new EsCountDetailDTO();
  947. detailDTO.setName(value);
  948. detailDTO.setFirstName(firstName);
  949. detailDTO.setField(field);
  950. detailDTO.setNumber(0L);
  951. detailDTO.setTopN(topN);
  952. collect.add(detailDTO);
  953. }
  954. } else if (collect.isEmpty()) {
  955. for (String value : values) {
  956. EsCountDetailDTO detailDTO = new EsCountDetailDTO();
  957. detailDTO.setName(value);
  958. detailDTO.setFirstName(firstName);
  959. detailDTO.setField(field);
  960. detailDTO.setNumber(0L);
  961. detailDTO.setTopN(topN);
  962. collect.add(detailDTO);
  963. }
  964. }
  965. map.put(firstName, collect);
  966. } else {
  967. detailDTOS.addAll(collect);
  968. }
  969. }
  970. }
  971. /**
  972. * 获取nested聚合后数据
  973. *
  974. * @param agg
  975. * @param field
  976. * @param detailDTOS
  977. */
  978. public void getNestedCountDTOS(Aggregate agg, String field, Integer topN, List<EsCountDetailDTO> detailDTOS,
  979. EsCountDTO esCountDTO, String firstName,
  980. Map<String, List<EsCountDetailDTO>> map, List<String> values) {
  981. Aggregate termsAgg = agg.nested().aggregations().get("terms_agg");
  982. List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
  983. EsCountDetailDTO countDTO = new EsCountDetailDTO();
  984. countDTO.setField(field);
  985. countDTO.setName("其他");
  986. countDTO.setTopN(topN);
  987. Long count = termsAgg.sterms().sumOtherDocCount();
  988. if (count > 0) {
  989. countDTO.setNumber(count);
  990. countDetailDTOS.add(countDTO);
  991. }
  992. List<StringTermsBucket> list = termsAgg.sterms().buckets().array();
  993. list.forEach(bucket -> {
  994. EsCountDetailDTO dto = new EsCountDetailDTO();
  995. dto.setField(field);
  996. Aggregate aggregate = bucket.aggregations().get("filter_agg");
  997. String value = bucket.key().stringValue();
  998. if (StringUtils.isNotEmpty(value)) {
  999. dto.setName(value);
  1000. dto.setNumber(bucket.docCount());
  1001. dto.setTopN(topN);
  1002. dto.setFirstName(firstName);
  1003. if (aggregate != null) {
  1004. dto.setNumber(aggregate.filter().docCount());
  1005. }
  1006. countDetailDTOS.add(dto);
  1007. }
  1008. // if (dto.getNumber() > 0) {
  1009. // detailDTOS.add(dto);
  1010. // }
  1011. });
  1012. List<String> strs = new ArrayList<>();
  1013. if (!CollectionUtils.isEmpty(values)) {
  1014. strs.addAll(values);
  1015. }
  1016. if (StringUtils.isNotEmpty(firstName)) {
  1017. countDetailDTOS.removeIf(dto -> dto.getName().equals("其他"));
  1018. if (countDetailDTOS.size() != values.size()) {
  1019. for (EsCountDetailDTO detailDTO : countDetailDTOS) {
  1020. strs.removeIf(i -> i.equals(detailDTO.getName()));
  1021. }
  1022. for (String value : strs) {
  1023. EsCountDetailDTO detailDTO = new EsCountDetailDTO();
  1024. detailDTO.setName(value);
  1025. detailDTO.setFirstName(firstName);
  1026. detailDTO.setField(field);
  1027. detailDTO.setNumber(0L);
  1028. detailDTO.setTopN(topN);
  1029. countDetailDTOS.add(detailDTO);
  1030. }
  1031. } else if (countDetailDTOS.isEmpty()) {
  1032. for (String value : values) {
  1033. EsCountDetailDTO detailDTO = new EsCountDetailDTO();
  1034. detailDTO.setName(value);
  1035. detailDTO.setFirstName(firstName);
  1036. detailDTO.setField(field);
  1037. detailDTO.setNumber(0L);
  1038. detailDTO.setTopN(topN);
  1039. countDetailDTOS.add(detailDTO);
  1040. }
  1041. }
  1042. map.put(firstName, countDetailDTOS);
  1043. } else {
  1044. detailDTOS.addAll(countDetailDTOS);
  1045. }
  1046. }
  1047. }