EsCountService.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package cn.cslg.pas.service.business.es;
  2. import cn.cslg.pas.common.dto.business.EsCountDTO;
  3. import cn.cslg.pas.common.dto.business.EsCountDetailDTO;
  4. import cn.cslg.pas.common.vo.EsConfigVO;
  5. import cn.cslg.pas.common.vo.business.EsCountVO;
  6. import cn.cslg.pas.domain.es.Patent;
  7. import cn.cslg.pas.factorys.EsCountBuilderFactory.EsCountBuilderFactory;
  8. import cn.cslg.pas.factorys.EsCountBuilderFactory.IEsCountBuilder;
  9. import cn.cslg.pas.service.business.CommonService;
  10. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  11. import co.elastic.clients.elasticsearch._types.aggregations.*;
  12. import co.elastic.clients.elasticsearch.core.SearchRequest;
  13. import co.elastic.clients.elasticsearch.core.SearchResponse;
  14. import com.alibaba.fastjson.JSON;
  15. import lombok.RequiredArgsConstructor;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.context.annotation.Lazy;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.util.CollectionUtils;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Comparator;
  24. import java.util.List;
  25. import java.util.stream.Collectors;
  26. @Service
  27. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  28. public class EsCountService {
  29. private final List<String> childList = Arrays.asList("childRaw");
  30. private final List<String> nestedList = Arrays.asList("PA", "IN", "PE");
  31. private final List<String> dateList = Arrays.asList("PD", "AD");
  32. private final ElasticsearchClient client;
  33. @Autowired
  34. private EsCountBuilderFactory esCountBuilderFactory;
  35. /**
  36. * 查询专利库中的专利分组聚合统计
  37. *
  38. * @param countVOS
  39. * @return
  40. * @throws Exception
  41. */
  42. public EsCountDTO esCountSearch(List<EsCountVO> countVOS) throws Exception {
  43. EsCountDTO esCountDTO = new EsCountDTO();
  44. List<EsCountDetailDTO> detailDTOS = new ArrayList<>();
  45. for (EsCountVO vo : countVOS) {
  46. String field = vo.getField();
  47. String valueOne = vo.getValueOne();
  48. String valueTwo = vo.getValueTwo();
  49. Integer topN = vo.getTopN();
  50. SearchRequest.Builder builder = new SearchRequest.Builder();
  51. builder.index("patent");
  52. IEsCountBuilder iEsCountBuilder = null;
  53. String json = CommonService.readJsonFile("esCount.json");
  54. List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
  55. EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getField().equals(field)).findFirst().orElse(null);
  56. if (esConfigVO != null) {
  57. iEsCountBuilder = esCountBuilderFactory.getClass(esConfigVO.getEsClass());
  58. iEsCountBuilder.setField(esConfigVO.getEsField());
  59. iEsCountBuilder.setValueOne(valueOne);
  60. iEsCountBuilder.setValueTwo(valueTwo);
  61. iEsCountBuilder.setTopN(topN);
  62. if (iEsCountBuilder.getField().contains(".")) {
  63. String path = iEsCountBuilder.getField().substring(0, iEsCountBuilder.getField().indexOf("."));
  64. iEsCountBuilder.setPath(path);
  65. }
  66. }
  67. Aggregation aggregation = iEsCountBuilder.createAggregation();
  68. builder.aggregations("Agg", aggregation);
  69. SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
  70. Aggregate agg = response.aggregations().get("Agg");
  71. if (dateList.contains(field)) {
  72. List<DateHistogramBucket> list = agg.dateHistogram().buckets().array();
  73. List<EsCountDetailDTO> esCountDetailDTOS = new ArrayList<>();
  74. list.forEach(bucket -> {
  75. EsCountDetailDTO dto = new EsCountDetailDTO();
  76. dto.setField(field);
  77. Aggregate aggregate = bucket.aggregations().get("filter_agg");
  78. dto.setName(bucket.keyAsString());
  79. dto.setNumber(bucket.docCount());
  80. if (aggregate != null) {
  81. dto.setNumber(aggregate.filter().docCount());
  82. }
  83. if (dto.getNumber() > 0) {
  84. esCountDetailDTOS.add(dto);
  85. }
  86. });
  87. if (!CollectionUtils.isEmpty(esCountDetailDTOS)) {
  88. List<EsCountDetailDTO> collect = esCountDetailDTOS.stream().sorted(Comparator.comparing(EsCountDetailDTO::getName).reversed()).limit(topN).collect(Collectors.toList());
  89. detailDTOS.addAll(collect);
  90. }
  91. } else if (nestedList.contains(field)) {
  92. Aggregate termsAgg = agg.nested().aggregations().get("terms_agg");
  93. List<StringTermsBucket> list = termsAgg.sterms().buckets().array();
  94. list.forEach(bucket -> {
  95. EsCountDetailDTO dto = new EsCountDetailDTO();
  96. dto.setField(field);
  97. Aggregate aggregate = bucket.aggregations().get("filter_agg");
  98. dto.setName(bucket.key().stringValue());
  99. dto.setNumber(bucket.docCount());
  100. if (aggregate != null) {
  101. dto.setNumber(aggregate.filter().docCount());
  102. }
  103. if (dto.getNumber() > 0) {
  104. detailDTOS.add(dto);
  105. }
  106. });
  107. } else if (childList.contains(field)) {
  108. Aggregate childAgg = agg.children().aggregations().get("child_agg");
  109. List<StringTermsBucket> list = childAgg.sterms().buckets().array();
  110. list.forEach(bucket -> {
  111. EsCountDetailDTO dto = new EsCountDetailDTO();
  112. dto.setField(field);
  113. Aggregate aggregate = bucket.aggregations().get("filter_agg");
  114. dto.setName(bucket.key().stringValue());
  115. dto.setNumber(bucket.docCount());
  116. if (aggregate != null) {
  117. dto.setNumber(aggregate.filter().docCount());
  118. }
  119. if (dto.getNumber() > 0) {
  120. detailDTOS.add(dto);
  121. }
  122. });
  123. } else {
  124. List<StringTermsBucket> list = agg.sterms().buckets().array();
  125. list.forEach(bucket -> {
  126. EsCountDetailDTO dto = new EsCountDetailDTO();
  127. dto.setField(field);
  128. Aggregate aggregate = bucket.aggregations().get("filter_agg");
  129. dto.setName(bucket.key().stringValue());
  130. dto.setNumber(bucket.docCount());
  131. if (aggregate != null) {
  132. dto.setNumber(aggregate.filter().docCount());
  133. }
  134. if (dto.getNumber() > 0) {
  135. detailDTOS.add(dto);
  136. }
  137. });
  138. }
  139. }
  140. esCountDTO.setDetailDTOS(detailDTOS);
  141. return esCountDTO;
  142. }
  143. }