Ver código fonte

es聚合统计

zero 1 ano atrás
pai
commit
64724e0c65

+ 8 - 0
src/main/java/cn/cslg/pas/common/dto/business/EsCountDetailDTO.java

@@ -1,9 +1,17 @@
 package cn.cslg.pas.common.dto.business;
 
+import lombok.AllArgsConstructor;
 import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.NoArgsConstructor;
 
 @Data
+@AllArgsConstructor
+@NoArgsConstructor
+@EqualsAndHashCode
 public class EsCountDetailDTO {
+    //栏位
+    private String field;
     //名称
     private String name;
     //数量

+ 1 - 1
src/main/java/cn/cslg/pas/common/vo/business/EsCountVO.java

@@ -11,5 +11,5 @@ public class EsCountVO {
     //搜索的栏位值2
     private String valueTwo;
     //top
-    private Integer topN;
+    private Integer topN = 10;
 }

+ 4 - 2
src/main/java/cn/cslg/pas/controller/PatentController.java

@@ -17,6 +17,8 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.List;
+
 @RequestMapping(Constants.API_XiaoSHI + "/patent")
 @RestController
 public class PatentController {
@@ -37,8 +39,8 @@ public class PatentController {
 
     @Operation(summary = "统计专利库中专利")
     @PostMapping("/esCountSearch")
-    public Response esCountSearch(@RequestBody EsCountVO vo) throws Exception {
-        EsCountDTO dto = esCountService.esCountSearch(vo);
+    public Response esCountSearch(@RequestBody List<EsCountVO> countVOS) throws Exception {
+        EsCountDTO dto = esCountService.esCountSearch(countVOS);
         return Response.success(dto);
     }
 

+ 1 - 0
src/main/java/cn/cslg/pas/factorys/EsCountBuilderFactory/DateHistogramBuilder.java

@@ -4,6 +4,7 @@ import co.elastic.clients.elasticsearch._types.SortOrder;
 import co.elastic.clients.elasticsearch._types.aggregations.*;
 import co.elastic.clients.elasticsearch._types.query_dsl.Query;
 import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
+import co.elastic.clients.json.JsonData;
 import co.elastic.clients.util.NamedValue;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Component;

+ 89 - 45
src/main/java/cn/cslg/pas/service/business/es/EsCountService.java

@@ -23,17 +23,21 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.data.elasticsearch.client.elc.ElasticsearchAggregations;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Comparator;
 import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 @Service
 @RequiredArgsConstructor(onConstructor_ = {@Lazy})
 public class EsCountService {
     private final List<String> termList = Arrays.asList();
-    private final List<String> nestedList = Arrays.asList();
-    private final List<String> dateList = Arrays.asList();
+    private final List<String> nestedList = Arrays.asList("PA","IN","PE");
+    private final List<String> dateList = Arrays.asList("PD","AD");
 
     private final ElasticsearchClient client;
 
@@ -42,57 +46,97 @@ public class EsCountService {
 
     /**
      * 查询专利库中的专利分组聚合统计
-     * @param vo
+     *
+     * @param countVOS
      * @return
      * @throws Exception
      */
-    public EsCountDTO esCountSearch(EsCountVO vo) throws Exception {
-        String field = vo.getField();
-        String valueOne = vo.getValueOne();
-        String valueTwo = vo.getValueTwo();
-        Integer topN = vo.getTopN();
+    public EsCountDTO esCountSearch(List<EsCountVO> countVOS) throws Exception {
         EsCountDTO esCountDTO = new EsCountDTO();
+        List<EsCountDetailDTO> detailDTOS = new ArrayList<>();
+        for (EsCountVO vo : countVOS) {
+            String field = vo.getField();
+            String valueOne = vo.getValueOne();
+            String valueTwo = vo.getValueTwo();
+            Integer topN = vo.getTopN();
 
-        SearchRequest.Builder builder = new SearchRequest.Builder();
-        builder.index("patent_v1");
-        IEsCountBuilder iEsCountBuilder = null;
-        String json = CommonService.readJsonFile("esCount.json");
-        List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
-        EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getValue().equals(field)).findFirst().orElse(null);
-        if (esConfigVO != null) {
-            iEsCountBuilder = esCountBuilderFactory.getClass(esConfigVO.getEsClass());
-            iEsCountBuilder.setField(esConfigVO.getEsField());
-            iEsCountBuilder.setValueOne(valueOne);
-            iEsCountBuilder.setValueTwo(valueTwo);
-            iEsCountBuilder.setTopN(topN);
-            if (iEsCountBuilder.getField().contains(".")) {
-                String path = iEsCountBuilder.getField().substring(0, iEsCountBuilder.getField().indexOf("."));
-                iEsCountBuilder.setPath(path);
-            }
-
-        }
-        Aggregation aggregation = iEsCountBuilder.createAggregation();
-        builder.aggregations("Agg", aggregation);
-        SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
-        Aggregate agg = response.aggregations().get("Agg");
-//        List<DateHistogramBucket> array = agg.autoDateHistogram().buckets().array();
-//        agg.nested().aggregations().get("terms_agg");
+            SearchRequest.Builder builder = new SearchRequest.Builder();
+            builder.index("patent_v1");
+            IEsCountBuilder iEsCountBuilder = null;
+            String json = CommonService.readJsonFile("esCount.json");
+            List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
+            EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getValue().equals(field)).findFirst().orElse(null);
+            if (esConfigVO != null) {
+                iEsCountBuilder = esCountBuilderFactory.getClass(esConfigVO.getEsClass());
+                iEsCountBuilder.setField(esConfigVO.getEsField());
+                iEsCountBuilder.setValueOne(valueOne);
+                iEsCountBuilder.setValueTwo(valueTwo);
+                iEsCountBuilder.setTopN(topN);
+                if (iEsCountBuilder.getField().contains(".")) {
+                    String path = iEsCountBuilder.getField().substring(0, iEsCountBuilder.getField().indexOf("."));
+                    iEsCountBuilder.setPath(path);
+                }
 
-        List<EsCountDetailDTO> detailDTOS = new ArrayList<>();
-        List<StringTermsBucket> list = agg.sterms().buckets().array();
-        list.forEach(bucket -> {
-            EsCountDetailDTO dto = new EsCountDetailDTO();
-            Aggregate aggregate = bucket.aggregations().get("filter_agg");
-            dto.setName(bucket.key().stringValue());
-            dto.setNumber(bucket.docCount());
-            if (aggregate != null) {
-                dto.setNumber(aggregate.filter().docCount());
-                dto.setName(bucket.key().stringValue());
             }
-            if (dto.getNumber() > 0) {
-                detailDTOS.add(dto);
+            Aggregation aggregation = iEsCountBuilder.createAggregation();
+            builder.aggregations("Agg", aggregation);
+            SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
+            Aggregate agg = response.aggregations().get("Agg");
+            List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
+            if (dateList.contains(field)) {
+                List<DateHistogramBucket> list = agg.dateHistogram().buckets().array();
+                List<EsCountDetailDTO> esCountDetailDTOS = new ArrayList<>();
+                list.forEach(bucket -> {
+                    EsCountDetailDTO dto = new EsCountDetailDTO();
+                    dto.setField(field);
+                    Aggregate aggregate = bucket.aggregations().get("filter_agg");
+                    dto.setName(bucket.keyAsString());
+                    dto.setNumber(bucket.docCount());
+                    if (aggregate != null) {
+                        dto.setNumber(aggregate.filter().docCount());
+                    }
+                    if (dto.getNumber() > 0) {
+                        esCountDetailDTOS.add(dto);
+                    }
+                });
+                if (!CollectionUtils.isEmpty(esCountDetailDTOS)) {
+                    List<EsCountDetailDTO> collect = esCountDetailDTOS.stream().sorted(Comparator.comparing(EsCountDetailDTO::getName).reversed()).limit(topN).collect(Collectors.toList());
+                    detailDTOS.addAll(collect);
+                }
+            } else if (nestedList.contains(field)) {
+                Aggregate termsAgg = agg.nested().aggregations().get("terms_agg");
+                List<StringTermsBucket> list = termsAgg.sterms().buckets().array();
+                list.forEach(bucket -> {
+                    EsCountDetailDTO dto = new EsCountDetailDTO();
+                    dto.setField(field);
+                    Aggregate aggregate = bucket.aggregations().get("filter_agg");
+                    dto.setName(bucket.key().stringValue());
+                    dto.setNumber(bucket.docCount());
+                    if (aggregate != null) {
+                        dto.setNumber(aggregate.filter().docCount());
+                    }
+                    if (dto.getNumber() > 0) {
+                        detailDTOS.add(dto);
+                    }
+                });
+            } else {
+                List<StringTermsBucket> list = agg.sterms().buckets().array();
+                list.forEach(bucket -> {
+                    EsCountDetailDTO dto = new EsCountDetailDTO();
+                    dto.setField(field);
+                    Aggregate aggregate = bucket.aggregations().get("filter_agg");
+                    dto.setName(bucket.key().stringValue());
+                    dto.setNumber(bucket.docCount());
+                    if (aggregate != null) {
+                        dto.setNumber(aggregate.filter().docCount());
+                    }
+                    if (dto.getNumber() > 0) {
+                        detailDTOS.add(dto);
+                    }
+                });
             }
-        });
+//            detailDTOS.addAll(countDetailDTOS);
+        }
         esCountDTO.setDetailDTOS(detailDTOS);
         return esCountDTO;
     }

+ 15 - 6
src/test/java/cn/cslg/pas/service/EventServiceTests.java

@@ -215,14 +215,23 @@ public class EventServiceTests {
     }
     @Test
     void test8() throws Exception {
-        EsCountVO vo = new EsCountVO();
-//        vo.setField("PA");
+        List<EsCountVO> countVOS = new ArrayList<>();
+
+        EsCountVO vo1 = new EsCountVO();
+        vo1.setField("PA");
 //        vo.setField("CO");
-        vo.setField("PT");
-//        vo.setValueOne("1");
-//        vo.setValueTwo("1");
+//        vo.setField("PT");
+        vo1.setValueOne("国家电网公司");
+//        vo.setValueOne("2022");
+//        vo.setValueTwo("2024");
 //        vo.setField("AD");
-        EsCountDTO esCountDTO = esCountService.esCountSearch(vo);
+        EsCountVO vo2 = new EsCountVO();
+        vo2.setField("AD");
+        vo2.setValueOne("2022");
+        vo2.setValueTwo("2023");
+//        countVOS.add(vo1);
+        countVOS.add(vo2);
+        EsCountDTO esCountDTO = esCountService.esCountSearch(countVOS);
         System.out.println(esCountDTO);
 
     }