Переглянути джерело

Merge remote-tracking branch 'origin/master'

xiexiang 1 рік тому
батько
коміт
42fb0de78b

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

@@ -6,6 +6,7 @@ import java.util.List;
 
 @Data
 public class EsCountDTO {
+    private String condition;
 
     private List<EsCountDetailDTO> detailDTOS;
 }

+ 1 - 1
src/main/java/cn/cslg/pas/common/model/request/QueryRequest.java

@@ -17,6 +17,6 @@ public class QueryRequest {
     private String groupFieldValue;
     private List<OrderDTO> orderDTOList;
     private Integer projectId;
-    private Boolean ifHaveChild;
+    private Boolean ifHaveChild = false;
     private List<EsCustomFieldValueDTO> customFields;
 }

+ 9 - 3
src/main/java/cn/cslg/pas/common/vo/business/EsCountVO.java

@@ -2,16 +2,22 @@ package cn.cslg.pas.common.vo.business;
 
 import lombok.Data;
 
+import java.util.List;
+
 @Data
 public class EsCountVO {
+    //条件
+    private String condition;
     //栏位
     private String field;
-    //搜索的栏位值1
+    //统计搜索的栏位值1
     private String valueOne;
-    //搜索的栏位值2
+    //统计搜索的栏位值2
     private String valueTwo;
     //top
     private Integer topN = 10;
     //开关
-    private Boolean ifHaveChild;
+    private Boolean ifHaveChild = false;
+    //分析搜索栏位值
+    private List<String> values;
 }

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

@@ -55,8 +55,8 @@ public class PatentController {
 
     @Operation(summary = "统计专利库中专利")
     @PostMapping("/esCountSearch")
-    public Response esCountSearch(@RequestBody List<EsCountVO> countVOS,String condition) throws Exception {
-        EsCountDTO dto = esCountService.esCountSearch(countVOS, condition);
+    public Response esCountSearch(@RequestBody List<EsCountVO> countVOS) throws Exception {
+        EsCountDTO dto = esCountService.esCountSearch(countVOS);
         return Response.success(dto);
     }
 
@@ -81,6 +81,12 @@ public class PatentController {
         return Response.success(patentPageMessageVOS);
     }
 
+//    @Operation(summary = "esSearch")
+//    @PostMapping("/esSearch")
+//    public Response esSearch(@RequestBody List<EsCustomFieldValueDTO> customFields) throws Exception {
+//        PatentDTO patentDTO = esService.esSearch(customFields);
+//        return Response.success(patentDTO);
+//    }
 
     @Operation(summary = "esCount")
     @PostMapping("/esCount")

+ 3 - 1
src/main/java/cn/cslg/pas/factorys/EsCountBuilderFactory/IEsCountBuilder.java

@@ -2,6 +2,8 @@ package cn.cslg.pas.factorys.EsCountBuilderFactory;
 
 import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
 
+import java.text.ParseException;
+
 public interface IEsCountBuilder {
     public String field = "";
     public String valueOne = "";
@@ -10,7 +12,7 @@ public interface IEsCountBuilder {
     public String path = "";
     public Boolean ifHaveChild = false;
 
-    public Aggregation createAggregation();
+    public Aggregation createAggregation() throws Exception;
 
     public String getField();
 

+ 217 - 160
src/main/java/cn/cslg/pas/service/business/es/EsCountService.java

@@ -2,28 +2,33 @@ package cn.cslg.pas.service.business.es;
 
 import cn.cslg.pas.common.dto.business.EsCountDTO;
 import cn.cslg.pas.common.dto.business.EsCountDetailDTO;
+import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
+import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
+import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
 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.EsCountBuilderFactory.EsCountBuilderFactory;
 import cn.cslg.pas.factorys.EsCountBuilderFactory.IEsCountBuilder;
 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.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 com.alibaba.fastjson.JSON;
 import lombok.RequiredArgsConstructor;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 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.text.ParseException;
+import java.util.*;
 import java.util.stream.Collectors;
 
 @Service
@@ -37,6 +42,8 @@ public class EsCountService {
 
     @Autowired
     private EsCountBuilderFactory esCountBuilderFactory;
+    @Autowired
+    private FormatQueryService formatQueryService;
 
     /**
      * 查询专利库中的专利分组聚合统计
@@ -45,186 +52,100 @@ public class EsCountService {
      * @return
      * @throws Exception
      */
-    public EsCountDTO esCountSearch(List<EsCountVO> countVOS,String condition) throws Exception {
+    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();
             Integer topN = vo.getTopN();
-
+            String condition = vo.getCondition();
             //查询es返回数据
             SearchRequest.Builder builder = new SearchRequest.Builder();
-            Aggregation aggregation = this.selectAggregation(builder,vo);
-            builder.aggregations("Agg", aggregation);
+            Aggregation aggregation = this.selectAggregation(builder, vo);
+            if (StringUtils.isNotEmpty(condition)) {
+                //解析检索条件
+                treeNode tree = expressManager.getInstance().Parse(condition, false);
+                //从es中检索数据
+                Query query = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
+                Aggregation filtersAgg = null;
+                if (aggregation != null) {
+                    filtersAgg = new Aggregation.Builder().filters(new FiltersAggregation.Builder()
+                            .filters(i -> i.array(Arrays.asList(query))).build())
+                            .aggregations(new HashMap() {{
+                                put("filters_agg", aggregation);
+                            }}).build();
+                } else {
+                    filtersAgg = AggregationBuilders.filter(i -> i.bool(j -> j.must(query)));
+                }
+                builder.aggregations("Agg", filtersAgg);
+            } else {
+                builder.aggregations("Agg", aggregation);
+            }
             SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
 
             Aggregate agg = response.aggregations().get("Agg");
-            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 (StringUtils.isNotEmpty(condition)) {
+                if (StringUtils.isNotEmpty(field)) {
+                    List<FiltersBucket> filtersBuckets = agg.filters().buckets().array();
+                    if (dateList.contains(field)) {
+                        filtersBuckets.forEach(filtersBucket -> {
+                            this.getFiltersCountDTO(filtersBucket, condition,detailDTOS);
+                            Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
+                            this.getDateCountDTOS(filtersAgg, field,topN,detailDTOS);
+                        });
+                    } else if (nestedList.contains(field)) {
+                        filtersBuckets.forEach(filtersBucket -> {
+                            this.getFiltersCountDTO(filtersBucket, condition,detailDTOS);
+                            Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
+                            this.getNestedCountDTOS(filtersAgg, field,detailDTOS);
+                        });
+                    } else if (childList.contains(field)) {
+                        filtersBuckets.forEach(filtersBucket -> {
+                            this.getFiltersCountDTO(filtersBucket, condition,detailDTOS);
+                            Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
+                            this.getChildCountDTOS(filtersAgg, field,detailDTOS);
+                        });
+                    } else {
+                        filtersBuckets.forEach(filtersBucket -> {
+                            this.getFiltersCountDTO(filtersBucket, condition,detailDTOS);
+                            Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
+                            this.getTermCountDTOS(filtersAgg, field, detailDTOS);
+                        });
                     }
-                });
-                if (!CollectionUtils.isEmpty(esCountDetailDTOS)) {
-                    List<EsCountDetailDTO> collect = esCountDetailDTOS.stream().sorted(Comparator.comparing(EsCountDetailDTO::getName).reversed())
-                            .limit(topN).collect(Collectors.toList());
-                    detailDTOS.addAll(collect);
+                } else {
+                    this.getFilterCountDTO(agg, condition,detailDTOS);
                 }
-            } 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 if (childList.contains(field)) {
-                Aggregate childAgg = agg.children().aggregations().get("child_agg");
-                List<StringTermsBucket> list = childAgg.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);
-                    }
-                });
-            }
-        }
-        esCountDTO.setDetailDTOS(detailDTOS);
-        return esCountDTO;
-    }
-
-    public EsCountDTO esCount(EsCountVO countVO) throws Exception {
-        EsCountDTO esCountDTO = new EsCountDTO();
-        List<EsCountDetailDTO> detailDTOS = new ArrayList<>();
-        String valueOne = countVO.getValueOne();
-        String valueTwo = countVO.getValueTwo();
-        Integer topN = countVO.getTopN();
-        String field = countVO.getField();
-
-        //查询es返回数据
-        SearchRequest.Builder builder = new SearchRequest.Builder();
-        Aggregation aggregation = this.selectAggregation(builder,countVO);
-        builder.aggregations("Agg", aggregation);
-        SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
-        Aggregate agg = response.aggregations().get("Agg");
-        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 (dateList.contains(field)) {
+                    this.getDateCountDTOS(agg, field,topN, detailDTOS);
+                } else if (nestedList.contains(field)) {
+                    this.getNestedCountDTOS(agg, field, detailDTOS);
+                } else if (childList.contains(field)) {
+                    this.getChildCountDTOS(agg, field, detailDTOS);
+                } else {
+                    this.getTermCountDTOS(agg, field, detailDTOS);
                 }
-                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 if (childList.contains(field)) {
-            Aggregate childAgg = agg.children().aggregations().get("child_agg");
-            List<StringTermsBucket> list = childAgg.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);
-                }
-            });
         }
         esCountDTO.setDetailDTOS(detailDTOS);
         return esCountDTO;
     }
 
-    public Aggregation selectAggregation(SearchRequest.Builder builder, EsCountVO vo) {
+    /**
+     * 获取聚合后的aggregation
+     *
+     * @param builder
+     * @param vo
+     * @return
+     * @throws Exception
+     */
+    public Aggregation selectAggregation(SearchRequest.Builder builder, EsCountVO vo) throws Exception {
         String valueOne = vo.getValueOne();
         String valueTwo = vo.getValueTwo();
         Integer topN = vo.getTopN();
         Boolean ifHaveChild = vo.getIfHaveChild();
         String field = vo.getField();
+        Aggregation aggregation = null;
 
         builder.index("patent");
         IEsCountBuilder iEsCountBuilder = null;
@@ -242,8 +163,144 @@ public class EsCountService {
                 String path = iEsCountBuilder.getField().substring(0, iEsCountBuilder.getField().indexOf("."));
                 iEsCountBuilder.setPath(path);
             }
+            aggregation = iEsCountBuilder.createAggregation();
+        }
+        return aggregation;
+    }
+
+    /**
+     * 获取Filter聚合返回数据
+     * @param agg
+     * @param condition
+     * @return
+     */
+    public void getFilterCountDTO(Aggregate agg,String condition,List<EsCountDetailDTO> detailDTOS) {
+        EsCountDetailDTO filterDTO = new EsCountDetailDTO();
+        filterDTO.setField("condition");
+        filterDTO.setName(condition);
+        filterDTO.setNumber(agg.filter().docCount());
+        if (filterDTO.getNumber() > 0) {
+            detailDTOS.add(filterDTO);
+        }
+    }
+
+    /**
+     * 获取Filters聚合返回数据
+     * @param filtersBucket
+     * @param condition
+     * @return
+     */
+    public void getFiltersCountDTO(FiltersBucket filtersBucket,String condition,List<EsCountDetailDTO> detailDTOS) {
+        EsCountDetailDTO filtersDTO = new EsCountDetailDTO();
+        filtersDTO.setField("condition");
+        filtersDTO.setName(condition);
+        filtersDTO.setNumber(filtersBucket.docCount());
+        if (filtersDTO.getNumber() > 0) {
+            detailDTOS.add(filtersDTO);
+        }
+    }
+
+    /**
+     * 获取Terms聚合后数据
+     *
+     * @param agg
+     * @param field
+     * @param detailDTOS
+     */
+    public void getTermCountDTOS(Aggregate agg, String field, List<EsCountDetailDTO> detailDTOS) {
+        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);
+            }
+        });
+    }
+
+    /**
+     * 获取children聚合后数据
+     *
+     * @param agg
+     * @param field
+     * @param detailDTOS
+     */
+    public void getChildCountDTOS(Aggregate agg, String field,List<EsCountDetailDTO> detailDTOS) {
+        Aggregate childAgg = agg.children().aggregations().get("child_agg");
+        List<StringTermsBucket> list = childAgg.sterms().buckets().array();
+        list.forEach(bucket -> {
+            EsCountDetailDTO dto = new EsCountDetailDTO();
+            dto.setField(field);
+            Aggregate aggregate = bucket.aggregations().get("filter_agg");
+            List<StringTermsBucket> buckets = aggregate.sterms().buckets().array();
+            for (StringTermsBucket termsBucket : buckets) {
+                dto.setName(termsBucket.key().stringValue());
+                dto.setNumber(termsBucket.docCount());
+                if (dto.getNumber() > 0) {
+                    detailDTOS.add(dto);
+                }
+            }
+        });
+    }
+
+    /**
+     * 获取dateHistogram聚合后数据
+     *
+     * @param agg
+     * @param field
+     * @param detailDTOS
+     */
+    public void getDateCountDTOS(Aggregate agg, String field,Integer topN,List<EsCountDetailDTO> detailDTOS) {
+        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);
         }
-        return iEsCountBuilder.createAggregation();
     }
 
+    /**
+     * 获取nested聚合后数据
+     *
+     * @param agg
+     * @param field
+     * @param detailDTOS
+     */
+    public void getNestedCountDTOS(Aggregate agg, String field,List<EsCountDetailDTO> detailDTOS) {
+        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);
+            }
+        });
+    }
 }

+ 49 - 139
src/main/java/cn/cslg/pas/service/business/es/EsService.java

@@ -17,11 +17,9 @@ import cn.cslg.pas.common.utils.FormatUtil;
 import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
 import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
 import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
-import cn.cslg.pas.common.vo.ChinaLeagalStatus;
-import cn.cslg.pas.common.vo.ContentVO;
-import cn.cslg.pas.common.vo.PatentWithIdVO;
-import cn.cslg.pas.common.vo.StarPatentVO;
+import cn.cslg.pas.common.vo.*;
 import cn.cslg.pas.domain.es.*;
+import cn.cslg.pas.service.business.CommonService;
 import cn.cslg.pas.service.common.PatentStarApiService;
 import cn.cslg.pas.service.query.FormatQueryService;
 import co.elastic.clients.elasticsearch.ElasticsearchClient;
@@ -132,7 +130,7 @@ public class EsService {
         String searchCondition = "";
         List<EsCustomFieldValueDTO> customFields = queryRequest.getCustomFields();
         if (!CollectionUtils.isEmpty(customFields)) {
-            searchCondition = this.parseCustomField(customFields);
+            searchCondition = this.parseCustomField(customFields,queryRequest.getIfHaveChild());
         }
         Integer projectId = queryRequest.getProjectId();
         Long current = queryRequest.getCurrent();
@@ -171,116 +169,22 @@ public class EsService {
         //4. 返回数据
         builder.query(q);
         //排序
-        List<OrderDTO> dtoList = queryRequest.getOrderDTOList();
-        if (!CollectionUtils.isEmpty(dtoList)) {
-//            for (OrderDTO orderDTO : dtoList) {
-//                if (orderDTO.getOrderType().equals(IfConstant.NO)) {
-//                    builder.sort(sortOptionsBuilder -> sortOptionsBuilder
-//                            .field(fieldSortBuilder -> fieldSortBuilder
-//                                    .field(orderDTO.getOrderBy()).order(SortOrder.Asc)));
-//                } else {
-//                    builder.sort(sortOptionsBuilder -> sortOptionsBuilder
-//                            .field(fieldSortBuilder -> fieldSortBuilder
-//                                    .field(orderDTO.getOrderBy()).order(SortOrder.Desc)));
-//                }
-//            }
-        } else {
-            builder.sort(sortOptionsBuilder -> sortOptionsBuilder
-                    .field(fieldSortBuilder -> fieldSortBuilder
-                            .field("patent_no.keyword").order(SortOrder.Desc)));
-        }
-
-        //分页
-        if (current > 0 && size > 0) {
-            builder.from((current.intValue() - 1) * size.intValue()).size(size.intValue());
-        }
-
-        SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
-        List<PatentColumnDTO> list = new ArrayList<>();
-        List<Hit<Patent>> hits = response.hits().hits();
-        long total = response.hits().total().value();
-        for (Hit<Patent> hit : hits) {
-            Patent esMess = hit.source();
-            PatentColumnDTO columnDTO = new PatentColumnDTO();
-//            columnDTO.setPatentNo(esMess.getPatentNo());
-            BeanUtils.copyProperties(esMess, columnDTO);
-            ContentVO titleVO = new ContentVO();
-            String title = StringUtils.strip(JSON.toJSONString(esMess.getTitle()), "[]");
-            ContentDetailDTO titleContent = JSONObject.parseObject(title, ContentDetailDTO.class);
-            titleVO.setContent(titleContent.getTextContent());
-            columnDTO.setTitle(titleVO);
-            ContentVO abstractVO = new ContentVO();
-            String abstractStr = StringUtils.strip(JSON.toJSONString(esMess.getAbstractStr()), "[]");
-            ContentDetailDTO abstractContent = JSONObject.parseObject(abstractStr, ContentDetailDTO.class);
-            abstractVO.setContent(abstractContent.getTextContent());
-            columnDTO.setAbstractStr(abstractVO);
-            list.add(columnDTO);
-
-        }
-        this.loadCoulumnDTO(list);
-        dto.setTotal(total);
-        dto.setPatents(list);
-        dto.setPageNum(current);
-        dto.setPageSize(size);
-        return dto;
-    }
-
-    public PatentDTO esSearch(QueryRequest queryRequest, List<EsCustomFieldValueDTO> customFields) throws Exception {
-        PatentDTO dto = new PatentDTO();
-
-        String searchCondition = "";
-//        List<EsCustomFieldValueDTO> customFields = queryRequest.getCustomFields();
-        if (!CollectionUtils.isEmpty(customFields)) {
-            searchCondition = this.parseCustomField(customFields);
-        }
-        Integer projectId = queryRequest.getProjectId();
-        Long current = queryRequest.getCurrent();
-        Long size = queryRequest.getSize();
-        //判断表达式
-        if (queryRequest instanceof StringRequest) {
-            searchCondition = ((StringRequest) queryRequest).getSearchQuery();
-        } else if (queryRequest instanceof MapRequest) {
-            Map<String, Object> map = ((MapRequest) queryRequest).getSearchQuery();
-            StringBuilder stringBuilder = new StringBuilder();
-            for (String key : map.keySet()) {
-                Object value = map.get(key);
-                if (!"".contentEquals(stringBuilder)) {
-                    stringBuilder = stringBuilder.append(" AND ").append(key).append("=").append(value);
-                } else {
-                    stringBuilder = stringBuilder.append(key).append("=").append(value);
-                }
-            }
-            searchCondition = stringBuilder.toString();
-        }
-        if (projectId != null) {
-            if (searchCondition != null && !"".equals(searchCondition.trim())) {
-                searchCondition = "projectId = " + projectId + " AND " + searchCondition;
-            } else {
-                searchCondition = "projectId = " + projectId;
-            }
-        }
-        SearchRequest.Builder builder = new SearchRequest.Builder();
-        //设置查询索引
-        builder.index("patent");
-        //1. 解析检索条件
-        treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
-        //格式化检索式
-        //3. 从es中检索数据
-        Query q = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
-        //4. 返回数据
-        builder.query(q);
-        //排序
-        List<OrderDTO> dtoList = queryRequest.getOrderDTOList();
+//        List<OrderDTO> dtoList = queryRequest.getOrderDTOList();
 //        if (!CollectionUtils.isEmpty(dtoList)) {
+//            String json = CommonService.readJsonFile("patent.json");
+//            List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
 //            for (OrderDTO orderDTO : dtoList) {
-//                if (orderDTO.getOrderType().equals(IfConstant.NO)) {
-//                    builder.sort(sortOptionsBuilder -> sortOptionsBuilder
-//                            .field(fieldSortBuilder -> fieldSortBuilder
-//                                    .field(orderDTO.getOrderBy()).order(SortOrder.Asc)));
-//                } else {
-//                    builder.sort(sortOptionsBuilder -> sortOptionsBuilder
-//                            .field(fieldSortBuilder -> fieldSortBuilder
-//                                    .field(orderDTO.getOrderBy()).order(SortOrder.Desc)));
+//                EsConfigVO configVO = esConfigVOS.stream().filter(item -> item.getField().equals(orderDTO.getOrderBy())).findFirst().orElse(null);
+//                if (configVO != null) {
+//                    if (orderDTO.getOrderType().equals(IfConstant.NO)) {
+//                        builder.sort(sortOptionsBuilder -> sortOptionsBuilder
+//                                .field(fieldSortBuilder -> fieldSortBuilder
+//                                        .field(configVO.getEsField()).order(SortOrder.Asc)));
+//                    } else {
+//                        builder.sort(sortOptionsBuilder -> sortOptionsBuilder
+//                                .field(fieldSortBuilder -> fieldSortBuilder
+//                                        .field(configVO.getEsField()).order(SortOrder.Desc)));
+//                    }
 //                }
 //            }
 //        } else {
@@ -330,31 +234,31 @@ public class EsService {
      * @param customFields
      * @return
      */
-    public String parseCustomField(List<EsCustomFieldValueDTO> customFields) {
+    public String parseCustomField(List<EsCustomFieldValueDTO> customFields,Boolean ifHaveChild) {
         int m = 1;
         int n = 0;
         StringBuilder builder = new StringBuilder();
         long start = System.currentTimeMillis();
         if (customFields.size() > m) {
-            builder.append("(");
             for (int i = 0; i < customFields.size(); i++) {
                 EsCustomFieldValueDTO customField = customFields.get(i);
                 if (i != n) {
                     builder.append(" ").append("and").append(" ").append("(");
-                    this.appendStr(customField, builder, m);
+                    this.appendStr(customField, builder, m,ifHaveChild);
                 } else {
-                    this.appendStr(customField, builder, m);
+                    builder.append("(");
+                    this.appendStr(customField, builder, m,ifHaveChild);
                 }
             }
         } else {
             for (int i = 0; i < customFields.size(); i++) {
                 EsCustomFieldValueDTO customField = customFields.get(i);
-                int num = customFields.size() - m;
                 if (i != n) {
                     builder.append(" ").append("and").append(" ");
-                    this.appendStr(customField, builder, m);
+                    this.appendStr(customField, builder, m,ifHaveChild);
                 } else {
-                    this.appendStr(customField, builder, m);
+                    builder.append("(");
+                    this.appendStr(customField, builder, m,ifHaveChild);
                 }
             }
         }
@@ -364,31 +268,37 @@ public class EsService {
         return builder.toString();
     }
 
-
-    public void appendStr(EsCustomFieldValueDTO customField, StringBuilder builder, int m) {
-        List<String> values = customField.getFieldValue();
+    public void appendStr(EsCustomFieldValueDTO customField, StringBuilder builder, int m,boolean ifHaveChild) {
         builder.append("field").append("=").append(customField.getFieldId());
-        builder.append(" ").append("and").append(" ");
-        builder.append("fieldValue").append("=");
-        if (values.size() > m) {
-            builder.append("(");
-            for (int j = 0; j < values.size(); j++) {
-                String s = values.get(j);
-                if (j != values.size() - m) {
-                    builder.append(s).append(" ").append("or").append(" ");
-                } else {
-                    builder.append(s).append(")").append(")");
-                }
+        List<String> values = customField.getFieldValue();
+        if (!CollectionUtils.isEmpty(values)) {
+            builder.append(" ").append("and").append(" ");
+            if (ifHaveChild) {
+                builder.append("statsValue").append("=");
+            } else {
+                builder.append("fieldValue").append("=");
             }
-        }
-        else {
-            for (String value : values) {
-                builder.append(value);
+
+            if (values.size() > m) {
+                builder.append("(");
+                for (int j = 0; j < values.size(); j++) {
+                    String s = values.get(j);
+                    if (j != values.size() - m) {
+                        builder.append(s).append(" ").append("or").append(" ");
+                    } else {
+                        builder.append(s).append(")").append(")");
+                    }
+                }
+            } else {
+                for (String value : values) {
+                    builder.append(value).append(")");
+                }
             }
+        } else {
+            builder.append(")");
         }
     }
 
-
     /**
      * @param key
      * @param page

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

@@ -244,7 +244,7 @@ public class EventServiceTests {
         vo2.setValueTwo("2023");
         countVOS.add(vo1);
 //        countVOS.add(vo2);
-        EsCountDTO esCountDTO = esCountService.esCountSearch(countVOS, "");
+        EsCountDTO esCountDTO = esCountService.esCountSearch(countVOS);
         System.out.println(esCountDTO);
 
     }
@@ -296,23 +296,23 @@ public class EventServiceTests {
         esCustomFieldService.addCustomField(dto);
     }
 
-//    @Test
-//    void test16() {
-//        List<EsCustomFieldValueDTO> customFields = new ArrayList<>();
-//        EsCustomFieldValueDTO dto1 = new EsCustomFieldValueDTO();
-//        dto1.setFieldId("1");
-//        dto1.setFieldValue(Arrays.asList("a", "b"));
-//        EsCustomFieldValueDTO dto2 = new EsCustomFieldValueDTO();
-//        dto2.setFieldId("2");
-//        dto2.setFieldValue(Arrays.asList("a", "b"));
-//        customFields.add(dto1);
-//        customFields.add(dto2);
-//        String s = esService.parseCustomField(customFields,Boolean.FALSE);
-//        System.out.println("结果为:" + s);
-//        System.out.println("-----------------------------");
-//        List<EsCustomFieldValueDTO> fields = new ArrayList<>();
-//
-//    }
+    @Test
+    void test16() {
+        List<EsCustomFieldValueDTO> customFields = new ArrayList<>();
+        EsCustomFieldValueDTO dto1 = new EsCustomFieldValueDTO();
+        dto1.setFieldId("1");
+        dto1.setFieldValue(Arrays.asList("a"));
+        EsCustomFieldValueDTO dto2 = new EsCustomFieldValueDTO();
+        dto2.setFieldId("2");
+        dto2.setFieldValue(Arrays.asList("a"));
+        customFields.add(dto1);
+        customFields.add(dto2);
+        String s = esService.parseCustomField(customFields,Boolean.FALSE);
+        System.out.println("结果为:" + s);
+        System.out.println("-----------------------------");
+        List<EsCustomFieldValueDTO> fields = new ArrayList<>();
+
+    }
 
     @Test
     void test17() throws IOException {