zero 1 year ago
parent
commit
f4e19def07

+ 15 - 2
src/main/java/cn/cslg/pas/factorys/EsCountAnalyseBuilderFactory/TermsCountAnalysisBuilder.java

@@ -34,7 +34,7 @@ public class TermsCountAnalysisBuilder implements IEsCountAnalysisBuilder {
         Aggregation aggregation = null;
         if (StringUtils.isNotEmpty(valueOne)) {
             Query query = null;
-            if (valueOne.contains("罒")) {
+            if (field.equals("patent_type") && valueOne.contains("罒")) {
                 String[] split = valueOne.split("罒");
                 List<String> list = Arrays.asList(split);
                 List<Query> queries = new ArrayList<>();
@@ -56,7 +56,20 @@ public class TermsCountAnalysisBuilder implements IEsCountAnalysisBuilder {
                 put("filter_agg", term);
             }}).build();
         } else if (!CollectionUtils.isEmpty(values)) {
-            aggregation = AggregationBuilders.terms(i -> i.field(field).include(j -> j.terms(values)).size(topN));
+            if (field.equals("patent_type")) {
+                List<String> list = new ArrayList<>();
+                for (String value : values) {
+                    if (value.contains("罒")) {
+                        String[] split = value.split("罒");
+                        list.addAll(Arrays.asList(split));
+                    } else {
+                        list.add(value);
+                    }
+                }
+                aggregation = AggregationBuilders.terms(i -> i.field(field).include(j -> j.terms(list)).size(topN));
+            } else {
+                aggregation = AggregationBuilders.terms(i -> i.field(field).include(j -> j.terms(values)).size(topN));
+            }
         } else {
             topN = topN + 1;
             aggregation = AggregationBuilders.terms(i -> i.field(field).size(topN));

+ 218 - 5
src/main/java/cn/cslg/pas/service/business/es/EsCountService.java

@@ -51,6 +51,7 @@ public class EsCountService {
     private final List<String> dateList = Arrays.asList("PD", "AD", "GD");
     private final List<String> numberList = Arrays.asList("QPN", "QDPN", "SFN", "IFN", "PFN");
     private final List<String> nestDateList = Arrays.asList("PRD");
+    private final List<String> patentTypeList = Arrays.asList("PT");
     private final List<String> nestChildList = Arrays.asList("MAT","MRH","MIN");
 
 
@@ -137,14 +138,19 @@ public class EsCountService {
             }
         }
         searchCondition = this.getCondition(searchCondition, taskId, projectId);
+        String queryCondition = "";
+        if (searchCondition.contains("罒")) {
+            queryCondition = searchCondition.replace("罒", " OR ");
+        } else {
+            queryCondition = searchCondition;
+        }
         SearchRequest.Builder builder = new SearchRequest.Builder();
         //设置查询索引
         builder.index("patent");
         Query query = null;
-//        String esGroupField = "";
-        if (StringUtils.isNotEmpty(searchCondition)) {
+        if (StringUtils.isNotEmpty(queryCondition)) {
             //1. 解析检索条件
-            treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
+            treeNode tree = expressManager.getInstance().Parse(queryCondition, false);
             //格式化检索式
             //3. 从es中检索数据
             query = formatQueryService.EsQueryToQuery((operateNode) tree, "patent",projectId);
@@ -210,8 +216,10 @@ public class EsCountService {
                         } else {
                             this.getNumberAnalysisDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values);
                         }
+                    } else if (patentTypeList.contains(field)) {
+                        this.getPTTermCountDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values, valueOne);
                     } else {
-                        this.getTermCountDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values,valueOne);
+                        this.getTermCountDTOS(agg, field, topN, detailDTOS, esCountDTO, firstName, map, values, valueOne);
                     }
                 } else {
                     this.getFilterCountDTO(agg, searchCondition, detailDTOS);
@@ -480,7 +488,7 @@ public class EsCountService {
             Long count = agg.sterms().sumOtherDocCount();
             if (count > 0) {
                 countDTO.setNumber(count + otherNum);
-                detailDTOS.add(countDTO);
+                countDetailDTOS.add(countDTO);
             }
             esCountDTO.setRemainingNum(count + otherNum);
         }
@@ -523,6 +531,211 @@ public class EsCountService {
     }
 
     /**
+     * 获取Terms聚合后数据
+     *
+     * @param agg
+     * @param field
+     * @param detailDTOS
+     */
+    public void getPTTermCountDTOS(Aggregate agg, String field, Integer topN, List<EsCountDetailDTO> detailDTOS,
+                                 EsCountDTO esCountDTO, String firstName,
+                                 Map<String, List<EsCountDetailDTO>> map,List<String> values,String valueOne) {
+        List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
+        List<EsCountDetailDTO> countList = new ArrayList<>();
+        long otherNum = 0l;
+        final List<String> inventorList = Arrays.asList("1", "8");
+        final List<String> functionalList = Arrays.asList("2", "9");
+        if (StringUtils.isNotEmpty(valueOne)) {
+            Aggregate filterAgg = agg.filter().aggregations().get("filter_agg");
+            List<StringTermsBucket> list = filterAgg.sterms().buckets().array();
+            for (StringTermsBucket bucket : list) {
+                EsCountDetailDTO dto = new EsCountDetailDTO();
+                dto.setField(field);
+                String value = bucket.key().stringValue();
+                if (StringUtils.isNotEmpty(value)) {
+                    dto.setName(value);
+                    dto.setNumber(bucket.docCount());
+                    dto.setTopN(topN);
+                    dto.setFirstName(firstName);
+                    if (dto.getNumber() > 0) {
+                        countDetailDTOS.add(dto);
+                    }
+                } else {
+                    otherNum += 1;
+                }
+            }
+
+            List<EsCountDetailDTO> inventors = new ArrayList<>();
+            List<EsCountDetailDTO> functions = new ArrayList<>();
+            Map<String, List<EsCountDetailDTO>> collect = countDetailDTOS.stream().collect(Collectors.groupingBy(EsCountDetailDTO::getName));
+            for (String key : collect.keySet()) {
+                List<EsCountDetailDTO> list1 = collect.get(key);
+                if (inventorList.contains(key)) {
+                    inventors.addAll(list1);
+                } else if (functionalList.contains(key)) {
+                    functions.addAll(list1);
+                } else {
+                    countList.addAll(list1);
+                }
+            }
+
+            if (!CollectionUtils.isEmpty(inventors)) {
+                final long inventorSum = inventors.stream().mapToLong(EsCountDetailDTO::getNumber).sum();
+                final EsCountDetailDTO detailDTO = inventors.get(0);
+                EsCountDetailDTO inventor = new EsCountDetailDTO();
+                inventor.setField(detailDTO.getField());
+                inventor.setName("1罒8");
+                inventor.setNumber(inventorSum);
+                inventor.setTopN(detailDTO.getTopN());
+                inventor.setFirstName(detailDTO.getFirstName());
+                inventor.setFieldId(detailDTO.getFieldId());
+                countList.add(inventor);
+            }
+            if (!CollectionUtils.isEmpty(functions)) {
+                final long functionalSum = functions.stream().mapToLong(EsCountDetailDTO::getNumber).sum();
+                final EsCountDetailDTO detailDTO = functions.get(0);
+                EsCountDetailDTO functional = new EsCountDetailDTO();
+                functional.setField(detailDTO.getField());
+                functional.setName("2罒9");
+                functional.setNumber(functionalSum);
+                functional.setTopN(detailDTO.getTopN());
+                functional.setFirstName(detailDTO.getFirstName());
+                functional.setFieldId(detailDTO.getFieldId());
+                countList.add(functional);
+            }
+
+            if (countList.size() > topN) {
+                countList.remove(countList.size() - 1);
+                otherNum += 1;
+            }
+
+            EsCountDetailDTO countDTO = new EsCountDetailDTO();
+            countDTO.setField(field);
+            countDTO.setName("其他");
+            countDTO.setTopN(topN);
+            Long count = filterAgg.sterms().sumOtherDocCount();
+            if (count > 0) {
+                countDTO.setNumber(count + otherNum);
+                countList.add(countDTO);
+            }
+            esCountDTO.setRemainingNum(count + otherNum);
+        } else {
+            List<StringTermsBucket> list = agg.sterms().buckets().array();
+            for (StringTermsBucket bucket : list) {
+                EsCountDetailDTO dto = new EsCountDetailDTO();
+                dto.setField(field);
+                Aggregate aggregate = bucket.aggregations().get("filter_agg");
+                String value = bucket.key().stringValue();
+                if (StringUtils.isNotEmpty(value)) {
+                    dto.setName(value);
+                    dto.setNumber(bucket.docCount());
+                    dto.setTopN(topN);
+                    dto.setFirstName(firstName);
+                    if (aggregate != null) {
+                        dto.setNumber(aggregate.filter().docCount());
+                    }
+                    if (dto.getNumber() > 0) {
+                        countDetailDTOS.add(dto);
+                    }
+                } else {
+                    otherNum += 1;
+                }
+            }
+
+            List<EsCountDetailDTO> inventors = new ArrayList<>();
+            List<EsCountDetailDTO> functions = new ArrayList<>();
+            Map<String, List<EsCountDetailDTO>> collect = countDetailDTOS.stream().collect(Collectors.groupingBy(EsCountDetailDTO::getName));
+            for (String key : collect.keySet()) {
+                List<EsCountDetailDTO> list1 = collect.get(key);
+                if (inventorList.contains(key)) {
+                    inventors.addAll(list1);
+                } else if (functionalList.contains(key)) {
+                    functions.addAll(list1);
+                } else {
+                    countList.addAll(list1);
+                }
+            }
+
+            if (!CollectionUtils.isEmpty(inventors)) {
+                final long inventorSum = inventors.stream().mapToLong(EsCountDetailDTO::getNumber).sum();
+                final EsCountDetailDTO detailDTO = inventors.get(0);
+                EsCountDetailDTO inventor = new EsCountDetailDTO();
+                inventor.setField(detailDTO.getField());
+                inventor.setName("1罒8");
+                inventor.setNumber(inventorSum);
+                inventor.setTopN(detailDTO.getTopN());
+                inventor.setFirstName(detailDTO.getFirstName());
+                inventor.setFieldId(detailDTO.getFieldId());
+                countList.add(inventor);
+            }
+            if (!CollectionUtils.isEmpty(functions)) {
+                final long functionalSum = functions.stream().mapToLong(EsCountDetailDTO::getNumber).sum();
+                final EsCountDetailDTO detailDTO = functions.get(0);
+                EsCountDetailDTO functional = new EsCountDetailDTO();
+                functional.setField(detailDTO.getField());
+                functional.setName("2罒9");
+                functional.setNumber(functionalSum);
+                functional.setTopN(detailDTO.getTopN());
+                functional.setFirstName(detailDTO.getFirstName());
+                functional.setFieldId(detailDTO.getFieldId());
+                countList.add(functional);
+            }
+
+            if (countList.size() > topN) {
+                countList.remove(countList.size() - 1);
+                otherNum += 1;
+            }
+
+            EsCountDetailDTO countDTO = new EsCountDetailDTO();
+            countDTO.setField(field);
+            countDTO.setName("其他");
+            countDTO.setTopN(topN);
+            Long count = agg.sterms().sumOtherDocCount();
+            if (count > 0) {
+                countDTO.setNumber(count + otherNum);
+                countList.add(countDTO);
+            }
+            esCountDTO.setRemainingNum(count + otherNum);
+        }
+
+
+        List<String> strs = new ArrayList<>();
+        if (!CollectionUtils.isEmpty(values)) {
+            strs.addAll(values);
+        }
+        if (StringUtils.isNotEmpty(firstName)) {
+            countList.removeIf(dto -> dto.getName().equals("其他"));
+            if (countList.size() != values.size()) {
+                for (EsCountDetailDTO detailDTO : countList) {
+                    strs.removeIf(i -> i.equals(detailDTO.getName()));
+                }
+                for (String value : strs) {
+                    EsCountDetailDTO detailDTO = new EsCountDetailDTO();
+                    detailDTO.setName(value);
+                    detailDTO.setFirstName(firstName);
+                    detailDTO.setField(field);
+                    detailDTO.setNumber(0L);
+                    detailDTO.setTopN(topN);
+                    countList.add(detailDTO);
+                }
+            } else if (countList.isEmpty()) {
+                for (String value : values) {
+                    EsCountDetailDTO detailDTO = new EsCountDetailDTO();
+                    detailDTO.setName(value);
+                    detailDTO.setFirstName(firstName);
+                    detailDTO.setField(field);
+                    detailDTO.setNumber(0L);
+                    detailDTO.setTopN(topN);
+                    countList.add(detailDTO);
+                }
+            }
+            map.put(firstName, countList);
+        } else {
+            detailDTOS.addAll(countList);
+        }
+    }
+
+    /**
      * 获取children聚合后数据
      *
      * @param agg

+ 6 - 1
src/main/java/cn/cslg/pas/service/business/es/EsService.java

@@ -170,7 +170,12 @@ public class EsService {
         commonVO.setTaskId(taskId);
         commonVO.setProjectId(projectId);
         commonVO.setProductId(productId);
-        commonVO.setSearchCondition(searchCondition);
+        if (searchCondition.contains("罒")) {
+            String replace = searchCondition.replace("罒", " OR ");
+            commonVO.setSearchCondition(replace);
+        } else {
+            commonVO.setSearchCondition(searchCondition);
+        }
         commonVO.setProductFrom(productFrom);
         commonVO.setCurrent(current);
         commonVO.setSize(size);