EsCountService.java 48 KB

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