ソースを参照

Merge remote-tracking branch 'origin/master'

xiexiang 1 年間 前
コミット
3c55223176

+ 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;
 }

+ 10 - 0
src/main/java/cn/cslg/pas/common/vo/business/PatentNoVO.java

@@ -0,0 +1,10 @@
+package cn.cslg.pas.common.vo.business;
+
+import lombok.Data;
+
+@Data
+public class PatentNoVO {
+
+    //专利号
+    private String patentNo;
+}

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

@@ -1,12 +1,14 @@
 package cn.cslg.pas.controller;
 
 import cn.cslg.pas.common.core.base.Constants;
+import cn.cslg.pas.common.dto.PatentColumnDTO;
 import cn.cslg.pas.common.dto.business.EsCountDTO;
 import cn.cslg.pas.common.model.cronModel.Records;
 import cn.cslg.pas.common.model.request.MapRequest;
 import cn.cslg.pas.common.model.request.StringRequest;
 import cn.cslg.pas.common.utils.Response;
 import cn.cslg.pas.common.vo.business.EsCountVO;
+import cn.cslg.pas.common.vo.business.PatentNoVO;
 import cn.cslg.pas.factorys.businessFactory.Business;
 import cn.cslg.pas.factorys.businessFactory.BusinessFactory;
 import cn.cslg.pas.service.business.es.EsCountService;
@@ -17,6 +19,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 +41,15 @@ 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);
+    }
+
+    @Operation(summary = "查询专利详情")
+    @PostMapping("/selectPatentDetail")
+    public Response selectPatentDetail(@RequestBody PatentNoVO vo) throws Exception {
+        PatentColumnDTO dto = esCountService.selectPatentDetail(vo);
         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;

+ 4 - 1
src/main/java/cn/cslg/pas/factorys/EsCountBuilderFactory/NestedCountBuilder.java

@@ -21,7 +21,10 @@ public class NestedCountBuilder implements IEsCountBuilder {
     public Aggregation createAggregation() {
         Aggregation terms = AggregationBuilders.terms(j -> j.field(field).size(topN));
         if (StringUtils.isNotEmpty(valueOne)) {
-            Query query = QueryBuilders.term(j -> j.field(field).value(valueOne));
+            String str = "*";
+            String s = str.concat(valueOne).concat("*");
+            Query query = QueryBuilders.wildcard(i -> i.field(field).value(s));
+//            Query query = QueryBuilders.term(j -> j.field(field).value(valueOne));
             Aggregation filter = AggregationBuilders.filter(n -> n.bool(k -> k.must(query)));
             terms = new Aggregation.Builder().terms(new TermsAggregation.Builder()
                     .field(field).size(topN).build())

+ 5 - 1
src/main/java/cn/cslg/pas/factorys/EsCountBuilderFactory/TermsCountBuilder.java

@@ -23,7 +23,11 @@ public class TermsCountBuilder implements IEsCountBuilder{
     public Aggregation createAggregation() {
         Aggregation aggregation = AggregationBuilders.terms(i -> i.field(field).size(topN));
         if (StringUtils.isNotEmpty(valueOne)) {
-            Aggregation filter = AggregationBuilders.filter(n -> n.term(m -> m.field(field).value(valueOne)));
+            String str = "*";
+            String s = str.concat(valueOne).concat("*");
+            Query query = QueryBuilders.wildcard(i -> i.field(field).value(s));
+            Aggregation filter = AggregationBuilders.filter(n -> n.bool(k -> k.must(query)));
+//            Aggregation filter = AggregationBuilders.filter(n -> n.term(m -> m.field(field).value(valueOne)));
             aggregation = new Aggregation.Builder().terms(new TermsAggregation.Builder()
                     .field(field).size(topN).build())
                     .aggregations(new HashMap() {{

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

@@ -1,11 +1,13 @@
 package cn.cslg.pas.service.business.es;
 
+import cn.cslg.pas.common.dto.PatentColumnDTO;
 import cn.cslg.pas.common.dto.PatentDTO;
 import cn.cslg.pas.common.dto.business.EsCountDTO;
 import cn.cslg.pas.common.dto.business.EsCountDetailDTO;
 import cn.cslg.pas.common.model.request.QueryRequest;
 import cn.cslg.pas.common.vo.EsConfigVO;
 import cn.cslg.pas.common.vo.business.EsCountVO;
+import cn.cslg.pas.common.vo.business.PatentNoVO;
 import cn.cslg.pas.domain.es.Patent;
 import cn.cslg.pas.factorys.EsBuilderFactory.IQueryBuilder;
 import cn.cslg.pas.factorys.EsCountBuilderFactory.EsCountBuilderFactory;
@@ -13,27 +15,36 @@ import cn.cslg.pas.factorys.EsCountBuilderFactory.IEsCountBuilder;
 import cn.cslg.pas.service.business.CommonService;
 import co.elastic.clients.elasticsearch.ElasticsearchClient;
 import co.elastic.clients.elasticsearch._types.aggregations.*;
+import co.elastic.clients.elasticsearch._types.query_dsl.HasChildQuery;
+import co.elastic.clients.elasticsearch._types.query_dsl.Query;
 import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
 import co.elastic.clients.elasticsearch.core.SearchRequest;
 import co.elastic.clients.elasticsearch.core.SearchResponse;
+import co.elastic.clients.elasticsearch.core.search.Hit;
 import co.elastic.clients.json.JsonData;
 import com.alibaba.fastjson.JSON;
 import lombok.RequiredArgsConstructor;
+import org.springframework.beans.BeanUtils;
 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.io.IOException;
 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,58 +53,122 @@ 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);
-            }
+            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.getField().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");
-
-        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;
     }
+
+    /**
+     * 根据专利号查询专利详情
+     * @param vo
+     * @return
+     * @throws IOException
+     */
+    public PatentColumnDTO selectPatentDetail(PatentNoVO vo) throws IOException {
+        PatentColumnDTO dto = new PatentColumnDTO();
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+        Query q = QueryBuilders.term(i -> i.field("patent_no.keyword").value(vo.getPatentNo()));
+        Query query = QueryBuilders.bool(i -> i.must(q));
+        builder.query(query);
+        SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
+        List<PatentColumnDTO> list = new ArrayList<>();
+        List<Hit<Patent>> hits = response.hits().hits();
+        for (Hit<Patent> hit : hits) {
+            Patent esMess = hit.source();
+            BeanUtils.copyProperties(esMess, dto);
+        }
+        return dto;
+    }
 }

+ 1 - 1
src/main/java/cn/cslg/pas/service/query/FormatQueryService.java

@@ -456,7 +456,7 @@ public class FormatQueryService {
                 IQueryBuilder iQueryBuilder = null;
                 String json = CommonService.readJsonFile(configName + ".json");
                 List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
-                EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getValue().equals(field)).findFirst().orElse(null);
+                EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getField().equals(field)).findFirst().orElse(null);
                 if (esConfigVO != null) {
                     iQueryBuilder = esBuilderFactory.getClass(esConfigVO.getEsClass());
                     iQueryBuilder.setField(esConfigVO.getEsField());

+ 4 - 4
src/main/resources/jsons/patent.json

@@ -4,7 +4,7 @@
     "type": "String",
     "value": "patentNo",
     "field": "patentNo",
-    "esField": "patent_no",
+    "esField": "patent_no.keyword",
     "esClass": "wildcardQueryBuilder",
     "ifSearch": "false",
     "ifGroup": "false",
@@ -16,7 +16,7 @@
     "type": "String",
     "value": "appNo",
     "field": "AN",
-    "esField": "app_no",
+    "esField": "app_no.keyword",
     "esClass": "wildcardQueryBuilder",
     "ifSearch": "false",
     "ifGroup": "false",
@@ -28,7 +28,7 @@
     "type": "String",
     "value": "publicNo",
     "field": "PN",
-    "esField": "public_no",
+    "esField": "public_no.keyword",
     "esClass": "wildcardQueryBuilder",
     "ifSearch": "false",
     "ifGroup": "false",
@@ -40,7 +40,7 @@
     "type": "String",
     "value": "GN",
     "field": "GN",
-    "esField": "grant_no",
+    "esField": "grant_no.keyword",
     "esClass": "wildcardQueryBuilder",
     "ifSearch": "false",
     "ifGroup": "false",

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

@@ -1,5 +1,6 @@
 package cn.cslg.pas.service;
 
+import cn.cslg.pas.common.dto.PatentColumnDTO;
 import cn.cslg.pas.common.dto.PatentDTO;
 import cn.cslg.pas.common.dto.business.EsCountDTO;
 import cn.cslg.pas.common.dto.business.EsPatentFamilyDTO;
@@ -8,6 +9,7 @@ import cn.cslg.pas.common.model.cronModel.Records;
 import cn.cslg.pas.common.model.request.*;
 import cn.cslg.pas.common.utils.Response;
 import cn.cslg.pas.common.vo.business.EsCountVO;
+import cn.cslg.pas.common.vo.business.PatentNoVO;
 import cn.cslg.pas.controller.EventController;
 import cn.cslg.pas.controller.PatentController;
 import cn.cslg.pas.domain.es.FamilyPatent;
@@ -213,17 +215,35 @@ public class EventServiceTests {
         SelectClaimDTO dto = esService.selectClaim("CN102324864A");
         System.out.println(dto);
     }
+
     @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);
 
     }
+
+    @Test
+    void test9() throws IOException {
+        PatentNoVO vo = new PatentNoVO();
+        vo.setPatentNo("CN201910069334.7");
+        PatentColumnDTO columnDTO = esCountService.selectPatentDetail(vo);
+        System.out.println(columnDTO);
+    }
 }