zero před 1 rokem
rodič
revize
876d70495a

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

@@ -10,6 +10,7 @@ public class EsCountDTO {
     //总数
     private Long allNumber;
     private String condition;
+    private Long remainingNum;
     private List<EsCountDetailDTO> detailDTOS;
     //多维分析返回数据
     private Map<String, List<EsCountDetailDTO>> analyseMap;

+ 1 - 0
src/main/java/cn/cslg/pas/factorys/EsCountAnalyseBuilderFactory/ChildNestedAnalyseBuilder.java

@@ -47,6 +47,7 @@ public class ChildNestedAnalyseBuilder implements IEsCountAnalysisBuilder{
                         put("filter_agg", filter);
                     }}).build();
         } else {
+            topN = topN + 1;
             Aggregation filter = AggregationBuilders.filter(n -> n.bool(k -> k.must(q1)));
             terms = new Aggregation.Builder().terms(new TermsAggregation.Builder()
                     .field(field).size(topN).build())

+ 1 - 0
src/main/java/cn/cslg/pas/factorys/EsCountAnalyseBuilderFactory/NestedCountAnalysisBuilder.java

@@ -45,6 +45,7 @@ public class NestedCountAnalysisBuilder implements IEsCountAnalysisBuilder {
         } else if (!CollectionUtils.isEmpty(values)) {
             terms = AggregationBuilders.terms(i -> i.field(field).include(j -> j.terms(values)).size(topN));
         } else {
+            topN = topN + 1;
             terms = AggregationBuilders.terms(j -> j.field(field).size(topN));
         }
         Aggregation finalTerms = terms;

+ 1 - 0
src/main/java/cn/cslg/pas/factorys/EsCountAnalyseBuilderFactory/TermsCountAnalysisBuilder.java

@@ -44,6 +44,7 @@ public class TermsCountAnalysisBuilder implements IEsCountAnalysisBuilder {
         } else if (!CollectionUtils.isEmpty(values)) {
             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));
         }
         return aggregation;

+ 50 - 34
src/main/java/cn/cslg/pas/service/business/es/EsCountService.java

@@ -19,7 +19,6 @@ import cn.cslg.pas.service.business.CommonService;
 import cn.cslg.pas.service.query.FormatQueryService;
 import co.elastic.clients.elasticsearch.ElasticsearchClient;
 import co.elastic.clients.elasticsearch._types.aggregations.*;
-import co.elastic.clients.elasticsearch._types.query_dsl.IdsQuery;
 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;
@@ -37,6 +36,7 @@ import java.io.IOException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicLong;
 import java.util.stream.Collectors;
 
 @Service
@@ -527,32 +527,43 @@ public class EsCountService {
                                  EsCountDTO esCountDTO, String firstName,
                                  Map<String, List<EsCountDetailDTO>> map,List<String> values) {
         List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
+        long otherNum = 0l;
+        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;
+            }
+        }
+
+        if (countDetailDTOS.size() > topN) {
+            countDetailDTOS.remove(countDetailDTOS.size() - 1);
+        }
+
         EsCountDetailDTO countDTO = new EsCountDetailDTO();
         countDTO.setField(field);
         countDTO.setName("其他");
         countDTO.setTopN(topN);
         Long count = agg.sterms().sumOtherDocCount();
         if (count > 0) {
-            countDTO.setNumber(count);
+            countDTO.setNumber(count + otherNum);
             detailDTOS.add(countDTO);
         }
-
-        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());
-            dto.setTopN(topN);
-            dto.setFirstName(firstName);
-            if (aggregate != null) {
-                dto.setNumber(aggregate.filter().docCount());
-            }
-            if (dto.getNumber() > 0) {
-                countDetailDTOS.add(dto);
-            }
-        });
+        esCountDTO.setRemainingNum(count);
 
         List<String> strs = new ArrayList<>();
         if (!CollectionUtils.isEmpty(values)) {
@@ -1035,18 +1046,9 @@ public class EsCountService {
                                    Map<String, List<EsCountDetailDTO>> map, List<String> values) {
         Aggregate termsAgg = agg.nested().aggregations().get("terms_agg");
         List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
-        EsCountDetailDTO countDTO = new EsCountDetailDTO();
-        countDTO.setField(field);
-        countDTO.setName("其他");
-        countDTO.setTopN(topN);
-        Long count = termsAgg.sterms().sumOtherDocCount();
-        if (count > 0) {
-            countDTO.setNumber(count);
-            countDetailDTOS.add(countDTO);
-        }
-
+        long otherNum = 0l;
         List<StringTermsBucket> list = termsAgg.sterms().buckets().array();
-        list.forEach(bucket -> {
+        for (StringTermsBucket bucket : list) {
             EsCountDetailDTO dto = new EsCountDetailDTO();
             dto.setField(field);
             Aggregate aggregate = bucket.aggregations().get("filter_agg");
@@ -1062,11 +1064,25 @@ public class EsCountService {
                 if (dto.getNumber() > 0) {
                     countDetailDTOS.add(dto);
                 }
+            } else {
+                otherNum += 1;
             }
-//            if (dto.getNumber() > 0) {
-//                detailDTOS.add(dto);
-//            }
-        });
+        }
+
+        if (countDetailDTOS.size() > topN) {
+            countDetailDTOS.remove(countDetailDTOS.size() - 1);
+        }
+
+        EsCountDetailDTO countDTO = new EsCountDetailDTO();
+        countDTO.setField(field);
+        countDTO.setName("其他");
+        countDTO.setTopN(topN);
+        Long count = termsAgg.sterms().sumOtherDocCount();
+        if (count > 0) {
+            countDTO.setNumber(count + otherNum);
+            countDetailDTOS.add(countDTO);
+        }
+        esCountDTO.setRemainingNum(count);
 
         List<String> strs = new ArrayList<>();
         if (!CollectionUtils.isEmpty(values)) {