Ver Fonte

es聚合统计

zero há 1 ano atrás
pai
commit
5ba572868a

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

@@ -5,7 +5,7 @@ import lombok.Data;
 @Data
 public class EsCountDetailDTO {
     //名称
-    private String key;
+    private String name;
     //数量
     private Integer number;
 

+ 1 - 1
src/main/java/cn/cslg/pas/domain/es/Patent.java

@@ -163,7 +163,7 @@ public class Patent {
     private List<PatentClassify> upc;
 
     /**
-     * UPC分类号(主)
+     * LOC分类号(主)
      */
     @JsonProperty("mloc")
     private PatentClassify mloc;

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

@@ -0,0 +1,69 @@
+package cn.cslg.pas.factorys.EsCountBuilderFactory;
+
+import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
+import co.elastic.clients.elasticsearch._types.aggregations.AggregationBuilders;
+import co.elastic.clients.elasticsearch._types.aggregations.CalendarInterval;
+import co.elastic.clients.elasticsearch._types.query_dsl.Query;
+import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Component;
+
+@Component
+public class DateHistogramBuilder implements IEsCountBuilder{
+    public String field = "";
+    public String valueOne = "";
+    public String valueTwo = "";
+    public Integer topN = 10;
+    public String path = "";
+
+    @Override
+    public Aggregation createAggregation() {
+        Aggregation aggregation = AggregationBuilders.dateHistogram(i -> i.field(field).format("yyyy").calendarInterval(CalendarInterval.Year));
+        if (StringUtils.isNotEmpty(valueTwo)) {
+            Query query = QueryBuilders.range(j -> j.from(valueOne).to(valueTwo));
+            Aggregation filter = AggregationBuilders.filter(n -> n.bool(k -> k.must(query)));
+            aggregation.aggregations().put("filter_agg",filter);
+        }
+        return aggregation;
+    }
+
+    public String getField() {
+        return field;
+    }
+
+    public void setField(String field) {
+        this.field = field;
+    }
+
+    public String getValueOne() {
+        return valueOne;
+    }
+
+    public void setValueOne(String valueOne) {
+        this.valueOne = valueOne;
+    }
+
+    public String getValueTwo() {
+        return valueTwo;
+    }
+
+    public void setValueTwo(String valueTwo) {
+        this.valueTwo = valueTwo;
+    }
+
+    public Integer getTopN() {
+        return topN;
+    }
+
+    public void setTopN(Integer topN) {
+        this.topN = topN;
+    }
+
+    public String getPath() {
+        return path;
+    }
+
+    public void setPath(String path) {
+        this.path = path;
+    }
+}

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

@@ -0,0 +1,19 @@
+package cn.cslg.pas.factorys.EsCountBuilderFactory;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+@Component
+public class EsCountBuilderFactory {
+
+    @Autowired
+    private Map<String, IEsCountBuilder> iEsCountBuilderMap;
+
+
+    public IEsCountBuilder getClass(String builderName) {
+        IEsCountBuilder bean1 = iEsCountBuilderMap.get(builderName);
+        return bean1;
+    }
+}

+ 33 - 0
src/main/java/cn/cslg/pas/factorys/EsCountBuilderFactory/IEsCountBuilder.java

@@ -0,0 +1,33 @@
+package cn.cslg.pas.factorys.EsCountBuilderFactory;
+
+import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
+
+public interface IEsCountBuilder {
+    public String field = "";
+    public String valueOne = "";
+    public String valueTwo = "";
+    public Integer topN = 10;
+    public String path = "";
+
+    public Aggregation createAggregation();
+
+    public String getField();
+
+    public void setField(String field);
+
+    public String getValueOne();
+
+    public void setValueOne(String valueOne);
+
+    public String getValueTwo();
+
+    public void setValueTwo(String valueTwo);
+
+    public Integer getTopN();
+
+    public void setTopN(Integer topN);
+
+    public String getPath();
+
+    public void setPath(String path);
+}

+ 78 - 0
src/main/java/cn/cslg/pas/factorys/EsCountBuilderFactory/NestedCountBuilder.java

@@ -0,0 +1,78 @@
+package cn.cslg.pas.factorys.EsCountBuilderFactory;
+
+import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
+import co.elastic.clients.elasticsearch._types.aggregations.AggregationBuilders;
+import co.elastic.clients.elasticsearch._types.query_dsl.Query;
+import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Component;
+
+@Component
+public class NestedCountBuilder implements IEsCountBuilder {
+    public String field = "";
+    public String valueOne = "";
+    public String valueTwo = "";
+    public Integer topN = 10;
+    public String path = "";
+
+    @Override
+    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));
+            Aggregation filter = AggregationBuilders.filter(n -> n.bool(k -> k.must(query)));
+            terms.aggregations().put("filter_agg",filter);
+        }
+        return AggregationBuilders.nested(i -> i.path(path)).aggregations().put("terms_agg", terms);
+    }
+
+    @Override
+    public String getField() {
+        return field;
+    }
+
+    @Override
+    public void setField(String field) {
+        this.field = field;
+    }
+
+    @Override
+    public String getValueOne() {
+        return valueOne;
+    }
+
+    @Override
+    public void setValueOne(String valueOne) {
+        this.valueOne = valueOne;
+    }
+
+    @Override
+    public String getValueTwo() {
+        return valueTwo;
+    }
+
+    @Override
+    public void setValueTwo(String valueTwo) {
+        this.valueTwo = valueTwo;
+    }
+
+    @Override
+    public Integer getTopN() {
+        return topN;
+    }
+
+    @Override
+    public void setTopN(Integer topN) {
+        this.topN = topN;
+    }
+
+    @Override
+    public String getPath() {
+        return path;
+    }
+
+    @Override
+    public void setPath(String path) {
+        this.path = path;
+    }
+}

+ 78 - 0
src/main/java/cn/cslg/pas/factorys/EsCountBuilderFactory/TermsCountBuilder.java

@@ -0,0 +1,78 @@
+package cn.cslg.pas.factorys.EsCountBuilderFactory;
+
+import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
+import co.elastic.clients.elasticsearch._types.aggregations.AggregationBuilders;
+import co.elastic.clients.elasticsearch._types.query_dsl.Query;
+import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Component;
+
+@Component
+public class TermsCountBuilder implements IEsCountBuilder{
+    public String field = "";
+    public String valueOne = "";
+    public String valueTwo = "";
+    public Integer topN = 10;
+    public String path = "";
+
+    @Override
+    public Aggregation createAggregation() {
+        Aggregation aggregation = AggregationBuilders.terms(i -> i.field(field).size(topN));
+        if (StringUtils.isNotEmpty(valueOne)) {
+            Query query = QueryBuilders.term(j -> j.field(field).value(valueOne));
+            Aggregation filter = AggregationBuilders.filter(n -> n.bool(k -> k.must(query)));
+            aggregation.aggregations().put("filter_agg",filter);
+        }
+        return aggregation;
+    }
+
+    @Override
+    public String getField() {
+        return field;
+    }
+
+    @Override
+    public void setField(String field) {
+        this.field = field;
+    }
+
+    @Override
+    public String getValueOne() {
+        return valueOne;
+    }
+
+    @Override
+    public void setValueOne(String valueOne) {
+        this.valueOne = valueOne;
+    }
+
+    @Override
+    public String getValueTwo() {
+        return valueTwo;
+    }
+
+    @Override
+    public void setValueTwo(String valueTwo) {
+        this.valueTwo = valueTwo;
+    }
+
+    @Override
+    public Integer getTopN() {
+        return topN;
+    }
+
+    @Override
+    public void setTopN(Integer topN) {
+        this.topN = topN;
+    }
+
+    @Override
+    public String getPath() {
+        return path;
+    }
+
+    @Override
+    public void setPath(String path) {
+        this.path = path;
+    }
+}

+ 31 - 16
src/main/java/cn/cslg/pas/service/business/es/EsCountService.java

@@ -3,26 +3,35 @@ package cn.cslg.pas.service.business.es;
 import cn.cslg.pas.common.dto.PatentDTO;
 import cn.cslg.pas.common.dto.business.EsCountDTO;
 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.domain.es.Patent;
+import cn.cslg.pas.factorys.EsBuilderFactory.IQueryBuilder;
+import cn.cslg.pas.factorys.EsCountBuilderFactory.EsCountBuilderFactory;
+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.AggregateBuilders;
-import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
-import co.elastic.clients.elasticsearch._types.aggregations.AggregationBuilders;
-import co.elastic.clients.elasticsearch._types.aggregations.NestedAggregate;
+import co.elastic.clients.elasticsearch._types.aggregations.*;
 import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
 import co.elastic.clients.elasticsearch.core.SearchRequest;
 import co.elastic.clients.elasticsearch.core.SearchResponse;
+import com.alibaba.fastjson.JSON;
 import lombok.RequiredArgsConstructor;
+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 java.util.List;
+
 @Service
 @RequiredArgsConstructor(onConstructor_ = {@Lazy})
 public class EsCountService {
 
     private final ElasticsearchClient client;
 
+    @Autowired
+    private EsCountBuilderFactory esCountBuilderFactory;
 
     /**
      * 查询专利库中的专利分组聚合统计
@@ -32,26 +41,32 @@ public class EsCountService {
      */
     public EsCountDTO esCountSearch(EsCountVO vo) throws Exception {
         String field = vo.getField();
-        String value = vo.getValue();
+        String valueOne = vo.getValueOne();
+        String valueTwo = vo.getValueTwo();
         Integer topN = vo.getTopN();
         EsCountDTO esCountDTO = new EsCountDTO();
 
         SearchRequest.Builder builder = new SearchRequest.Builder();
         builder.index("patent");
-        if (field.equals("applicant")) {
-
-        }
-        if (field.equals("rightHolder")) {
-
-        }
-        if (field.equals("mipc")) {
-
-        }
-        if (field.equals("appDate")) {
+        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();
 
         }
-//        AggregationBuilders.terms(i -> i.field()).field()
         SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
+        Aggregate aggregate = response.aggregations().get("");
         return esCountDTO;
     }
 }

+ 314 - 0
src/main/resources/jsons/esCount.json

@@ -0,0 +1,314 @@
+[
+  {
+    "name": "申请人",
+    "type": "String",
+    "value": "PA",
+    "field": "PA",
+    "esField": "applicant.name",
+    "esClass": "nestedCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "发明人",
+    "type": "String",
+    "value": "IN",
+    "field": "IN",
+    "esField": "inventor.name",
+    "esClass": "nestedCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "权利人",
+    "type": "String",
+    "value": "PE",
+    "field": "PE",
+    "esField": "right_holder.name",
+    "esClass": "nestedCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "IPC分类号一级",
+    "type": "String",
+    "value": "ipcLevel1",
+    "field": "ipcLevel1",
+    "esField": "mipc.level1",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "IPC分类号二级",
+    "type": "String",
+    "value": "ipcLevel2",
+    "field": "ipcLevel2",
+    "esField": "mipc.level2",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "IPC分类号三级",
+    "type": "String",
+    "value": "ipcLevel3",
+    "field": "ipcLevel3",
+    "esField": "mipc.level3",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "IPC分类号四级",
+    "type": "String",
+    "value": "ipcLevel4",
+    "field": "ipcLevel4",
+    "esField": "mipc.level4",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "IPC分类号五级",
+    "type": "String",
+    "value": "ipcLevel5",
+    "field": "ipcLevel5",
+    "esField": "mipc.level5",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "CPC分类号一级",
+    "type": "String",
+    "value": "cpcLevel1",
+    "field": "cpcLevel1",
+    "esField": "mcpc.level1",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "CPC分类号二级",
+    "type": "String",
+    "value": "cpcLevel2",
+    "field": "cpcLevel2",
+    "esField": "mcpc.level2",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "CPC分类号三级",
+    "type": "String",
+    "value": "cpcLevel3",
+    "field": "cpcLevel3",
+    "esField": "mcpc.level3",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "CPC分类号四级",
+    "type": "String",
+    "value": "cpcLevel4",
+    "field": "cpcLevel4",
+    "esField": "mcpc.level4",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "CPC分类号五级",
+    "type": "String",
+    "value": "cpcLevel5",
+    "field": "cpcLevel5",
+    "esField": "mcpc.level5",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "UPC分类号一级",
+    "type": "String",
+    "value": "upcLevel1",
+    "field": "upcLevel1",
+    "esField": "mupc.level1",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "UPC分类号二级",
+    "type": "String",
+    "value": "upcLevel2",
+    "field": "upcLevel2",
+    "esField": "mupc.level2",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "UPC分类号三级",
+    "type": "String",
+    "value": "upcLevel3",
+    "field": "upcLevel3",
+    "esField": "mupc.level3",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "LOC分类号一级",
+    "type": "String",
+    "value": "locLevel1",
+    "field": "locLevel1",
+    "esField": "mloc.level1",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "LOC分类号二级",
+    "type": "String",
+    "value": "locLevel2",
+    "field": "locLevel2",
+    "esField": "mloc.level2",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "LOC分类号三级",
+    "type": "String",
+    "value": "locLevel3",
+    "field": "locLevel3",
+    "esField": "mloc.level3",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "专利状态",
+    "type": "String",
+    "value": "SS",
+    "field": "SS",
+    "esField": "simple_status",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "专利类型",
+    "type": "String",
+    "value": "PT",
+    "field": "PT",
+    "esField": "patent_type",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "申请人国家",
+    "type": "String",
+    "value": "CO",
+    "field": "CO",
+    "esField": "applicant_addr.country",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "申请人省份",
+    "type": "String",
+    "value": "appProvince",
+    "field": "appProvince",
+    "esField": "applicant_addr.province",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "公开年",
+    "type": "String",
+    "value": "PD",
+    "field": "PD",
+    "esField": "public_date",
+    "esClass": "dateQueryBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "申请年",
+    "type": "String",
+    "value": "AD",
+    "field": "AD",
+    "esField": "app_date",
+    "esClass": "dateQueryBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  },
+  {
+    "name": "受理局",
+    "type": "String",
+    "value": "appCountry",
+    "field": "appCountry",
+    "esField": "app_country",
+    "esClass": "termsCountBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "true",
+    "ifAsCondition": "true"
+  }
+]