zero 1 rok pred
rodič
commit
b807218c5b

+ 62 - 0
src/main/java/cn/cslg/pas/factorys/EsBuilderFactory/ExistsQueryBuilder.java

@@ -0,0 +1,62 @@
+package cn.cslg.pas.factorys.EsBuilderFactory;
+
+
+import co.elastic.clients.elasticsearch._types.query_dsl.Query;
+import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
+import org.springframework.stereotype.Component;
+
+import java.text.ParseException;
+
+@Component
+public class ExistsQueryBuilder implements IQueryBuilder{
+    public String field = "";
+    public String value = "";
+    public String operator = "";
+    public String path = "";
+    public Integer projectId = null;
+
+    @Override
+    public Query creteQuery() throws ParseException {
+        return QueryBuilders.exists(i -> i.field(field));
+    }
+
+    public String getField() {
+        return field;
+    }
+
+    public void setField(String field) {
+        this.field = field;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+
+    public String getOperator() {
+        return operator;
+    }
+
+    public void setOperator(String operator) {
+        this.operator = operator;
+    }
+
+    public String getPath() {
+        return path;
+    }
+
+    public void setPath(String path) {
+        this.path = path;
+    }
+
+    public Integer getProjectId() {
+        return projectId;
+    }
+
+    public void setProjectId(Integer projectId) {
+        this.projectId = projectId;
+    }
+}

+ 51 - 39
src/main/java/cn/cslg/pas/service/business/es/EsCountService.java

@@ -28,6 +28,7 @@ import co.elastic.clients.elasticsearch.core.SearchRequest;
 import co.elastic.clients.elasticsearch.core.SearchResponse;
 import co.elastic.clients.elasticsearch.core.search.FieldCollapse;
 import co.elastic.clients.elasticsearch.core.search.Hit;
+import co.elastic.clients.elasticsearch.core.search.TotalHits;
 import com.alibaba.fastjson.JSON;
 import lombok.RequiredArgsConstructor;
 import org.apache.commons.lang3.StringUtils;
@@ -204,7 +205,7 @@ public class EsCountService {
                         }
                     } else if (childList.contains(field)) {
                         if (CollectionUtils.isEmpty(values)) {
-                            this.getChildCountDTOS(agg, field, fieldType, topN, detailDTOS, esCountDTO,projectId,fieldId,query,null);
+                            this.getChildCountDTOS(agg, field, fieldType, topN, detailDTOS, esCountDTO,projectId,fieldId,query);
                         } else {
                             this.getChildAnalysisDTOS(agg, field, fieldType, topN,
                                     detailDTOS, esCountDTO, firstName, map, values);
@@ -743,7 +744,7 @@ public class EsCountService {
      */
     public void getChildCountDTOS(Aggregate agg, String field, Integer fieldType, Integer topN,
                                   List<EsCountDetailDTO> detailDTOS, EsCountDTO esCountDTO,Integer projectId,
-                                  String fieldId,Query query,String esField) throws Exception {
+                                  String fieldId,Query query) throws Exception {
         esCountDTO.setAllNumber(agg.children().docCount());
         Aggregate childAgg = agg.children().aggregations().get("childAgg");
         List<StringTermsBucket> list = childAgg.sterms().buckets().array();
@@ -765,12 +766,12 @@ public class EsCountService {
             });
         });
         if (fieldType == null || fieldType != 6) {
-            Integer customNum1 = this.getUnselectedCustomNum1(projectId, fieldId, query);
+            Long customNum = this.getUnselectedCustomNum1(projectId, fieldId, query);
             EsCountDetailDTO detail = new EsCountDetailDTO();
             detail.setField(field);
             detail.setName("未选择");
-            if (customNum1 != null) {
-                detail.setNumber(customNum1.longValue());
+            if (customNum != null) {
+                detail.setNumber(customNum);
             } else {
                 detail.setNumber(0L);
             }
@@ -800,28 +801,42 @@ public class EsCountService {
     }
 
     //统计未选择数量
-    public Integer getUnselectedCustomNum1(Integer projectId, String fieldId, Query query) throws Exception {
+    public Long getUnselectedCustomNum1(Integer projectId, String fieldId, Query query) throws Exception {
         SearchRequest.Builder builder = new SearchRequest.Builder();
         //设置查询索引
         builder.index("patent");
         builder.query(query);
-        builder.from(0).size(10000);
-        builder.trackTotalHits(i -> i.enabled(true));
         SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
-        List<Hit<Patent>> hits = response.hits().hits();
-        List<String> list = new ArrayList<>();
-        for (Hit<Patent> hit : hits) {
-            String id = hit.id();
-            list.add(id);
-        }
-        if (!CollectionUtils.isEmpty(list)) {
-            return this.ifExistChild1(list, projectId, fieldId);
+        long total = response.hits().total().value();
+        final long childCount = this.ifExistChild1(projectId, fieldId,query);
+        return total - childCount;
+    }
+
+    public Long ifExistChild1(Integer projectId, String fieldId,Query query) throws IOException {
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        builder.index("patent");
+        Query q1 = QueryBuilders.exists(i -> i.field("custom_field"));
+        Query q2 = QueryBuilders.exists(i -> i.field("custom_field.field_value"));
+        Query q3 = QueryBuilders.term(i -> i.field("custom_field.if_new").value(1));
+        Query q4 = QueryBuilders.term(i -> i.field("custom_field.project_id").value(projectId));
+        Query q5 = QueryBuilders.term(i -> i.field("custom_field.field").value(fieldId));
+        Query bool = QueryBuilders.bool(i -> i.must(q1,q2, q3, q4, q5));
+        Query childQ = QueryBuilders.hasChild(i -> i.type("project_customfield").query(bool));
+        if (query != null) {
+            Query bool1 = QueryBuilders.bool(i -> i.must(query, childQ));
+            builder.query(bool1);
+        } else {
+            builder.query(childQ);
         }
-        return 0;
+        SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
+        return response.hits().total().value();
     }
 
     //根据未选择查询专利
     public GetUnselectedDTO getUnselectedCustomNum(Integer projectId, String fieldId) throws Exception {
+        Integer pageNum = 0;
+        Integer pageSize = 1000;
         GetUnselectedDTO dto = new GetUnselectedDTO();
         SearchRequest.Builder builder = new SearchRequest.Builder();
         //设置查询索引
@@ -829,7 +844,7 @@ public class EsCountService {
         Query query = QueryBuilders.term(i -> i.field("project_id").value(projectId));
         Query q = QueryBuilders.hasChild(i -> i.type("project").query(query));
         builder.query(q);
-        builder.from(0).size(10000);
+        builder.from(pageNum).size(pageSize);
         builder.trackTotalHits(i -> i.enabled(true));
         SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
         List<Hit<Patent>> hits = response.hits().hits();
@@ -838,35 +853,24 @@ public class EsCountService {
             String id = hit.id();
             list.add(id);
         }
+        long total = response.hits().total().value();
+        int sum = pageSize;
+        while (sum < total) {
+            pageNum = sum;
+            sum += 1000;
+            List<String> list1 = this.loadingBatchData(projectId, null, pageNum, pageSize);
+            list.addAll(list1);
+        }
         if (!CollectionUtils.isEmpty(list)) {
             dto = this.ifExistChild(list, projectId, fieldId);
         }
         return dto;
     }
 
-    public Integer ifExistChild1(List<String> list, Integer projectId, String fieldId) throws IOException {
-        int count = 0;
-        SearchRequest.Builder builder = new SearchRequest.Builder();
-        //设置查询索引
-        builder.index("patent");
-        Query ids = QueryBuilders.ids(i -> i.values(list));
-        Query q1 = QueryBuilders.hasParent(i -> i.parentType("patent").query(ids));
-        Query q2 = QueryBuilders.exists(i -> i.field("custom_field"));
-        Query q3 = QueryBuilders.term(i -> i.field("custom_field.if_new").value(1));
-        Query q4 = QueryBuilders.term(i -> i.field("custom_field.project_id").value(projectId));
-        Query q5 = QueryBuilders.term(i -> i.field("custom_field.field").value(fieldId));
-        Query bool = QueryBuilders.bool(i -> i.must(q1, q2, q3, q4, q5));
-        builder.query(bool);
-        SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
-        if (response.hits().total() != null) {
-            long total = response.hits().total().value();
-            count = list.size() - (int) total;
-        }
-        return count;
-    }
-
     //查询到对应的ids
     public GetUnselectedDTO ifExistChild(List<String> list, Integer projectId, String fieldId) throws IOException {
+        Integer pageNum = 0;
+        Integer pageSize = 1000;
         GetUnselectedDTO dto = new GetUnselectedDTO();
         SearchRequest.Builder builder = new SearchRequest.Builder();
         //设置查询索引
@@ -884,6 +888,14 @@ public class EsCountService {
         SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
         List<Hit<Patent>> hits = response.hits().hits();
         List<String> collect = hits.stream().map(Hit::routing).collect(Collectors.toList());
+        long total = response.hits().total().value();
+        int sum = pageSize;
+        while (sum < total) {
+            pageNum = sum;
+            sum += 1000;
+            List<String> list1 = this.loadingBatchData(projectId, null, pageNum, pageSize);
+            list.addAll(list1);
+        }
         list.removeAll(collect);
         dto.setBeinglessChildIds(list);
         return dto;

+ 2 - 14
src/main/java/cn/cslg/pas/service/business/es/EsService.java

@@ -465,7 +465,6 @@ public class EsService {
         int m = 1;
         int n = 0;
         StringBuilder builder = new StringBuilder();
-        long start = System.currentTimeMillis();
         if (customFields.size() > m) {
             for (int i = 0; i < customFields.size(); i++) {
                 EsCustomFieldValueDTO customField = customFields.get(i);
@@ -489,9 +488,6 @@ public class EsService {
                 }
             }
         }
-
-        long end = System.currentTimeMillis();
-        System.out.println("耗时" + (end - start));
         return builder.toString();
     }
 
@@ -500,10 +496,6 @@ public class EsService {
                           Integer projectId, Integer taskId) throws Exception {
         builder.append("field").append("=").append(customField.getFieldId());
         List<String> values = customField.getFieldValue();
-        if (values.contains("未选择")) {
-            values.removeIf(value -> value.equals("未选择"));
-            values.add("未选择");
-        }
         if (!CollectionUtils.isEmpty(values)) {
             builder.append(" ").append("and").append(" ");
             if (ifHaveChild) {
@@ -520,9 +512,7 @@ public class EsService {
                         builder.append(s).append(" ").append("or").append(" ");
                     } else {
                         if (s.equals("未选择")) {
-                            GetUnselectedDTO unselectedDTO = esCountService.getUnselectedCustomNum(projectId, customField.getFieldId());
-                            List<String> childIds = unselectedDTO.getBeinglessChildIds();
-                            String noCondition = this.appendIds(childIds);
+                            String noCondition = "(field = (NOT " + customField.getFieldId() + ")" + " OR " + "(field = " + customField.getFieldId() + " AND " + "fieldValue1 = 1))";
                             builder.append(s).append(")").append(" ").append("OR")
                                     .append(" ").append("(").append(noCondition).append(")").append(")");
                         } else {
@@ -533,9 +523,7 @@ public class EsService {
             } else {
                 for (String value : values) {
                     if (value.equals("未选择")) {
-                        GetUnselectedDTO unselectedDTO = esCountService.getUnselectedCustomNum(projectId, customField.getFieldId());
-                        List<String> childIds = unselectedDTO.getBeinglessChildIds();
-                        String noCondition = this.appendIds(childIds);
+                        String noCondition = "(field = (NOT " + customField.getFieldId() + ")" + " OR " + "(field = " + customField.getFieldId() + " AND " + "fieldValue1 = 1))";
                         builder.append(value).append(" ").append("OR").append(" ").append(noCondition).append(")");
                     } else {
                         builder.append(value).append(")");

+ 12 - 0
src/main/resources/jsons/patent.json

@@ -1363,6 +1363,18 @@
     "ifAsCondition": "true"
   },
   {
+    "name": "自定义栏位值1",
+    "type": "String",
+    "value": "fieldValue1",
+    "field": "fieldValue1",
+    "esField": "custom_field.field_value",
+    "esClass": "existsQueryBuilder",
+    "ifSearch": "false",
+    "ifGroup": "false",
+    "ifShow": "false",
+    "ifAsCondition": "true"
+  },
+  {
     "name": "自定义栏位统计值",
     "type": "String",
     "value": "statsValue",