Ver código fonte

es统计、分析

zero 1 ano atrás
pai
commit
bb276d0a8c

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

@@ -91,7 +91,7 @@ public class PatentController {
 
     @Operation(summary = "esAnalysis")
     @PostMapping("/esAnalysis")
-    public Response esAnalysis(@RequestBody EsCountVO countVO) throws Exception {
+    public Response esAnalysis(@RequestBody EsAllCountVO countVO) throws Exception {
         EsCountDTO dto = esCountService.esAnalysis(countVO);
         return Response.success(dto);
     }

+ 4 - 2
src/main/java/cn/cslg/pas/factorys/EsCountBuilderFactory/ChildCountBuilder.java

@@ -74,8 +74,10 @@ public class ChildCountBuilder implements IEsCountBuilder{
      * @return
      */
     private Aggregation getAggregation(List<Query> queryList) {
-        Query query = QueryBuilders.term(i -> i.field("custom_field.field").value(fieldId));
-        queryList.add(query);
+        Query q1 = QueryBuilders.term(i -> i.field("custom_field.field").value(fieldId));
+        Query q2 = QueryBuilders.term(i -> i.field("custom_field.field_type").value(fieldType));
+        queryList.add(q1);
+        queryList.add(q2);
 
         Aggregation filtersAgg = AggregationBuilders.filter(n -> n.bool(k -> k.must(queryList)));
 

+ 147 - 48
src/main/java/cn/cslg/pas/service/business/es/EsCountService.java

@@ -89,12 +89,15 @@ public class EsCountService {
 
         SearchRequest.Builder builder = new SearchRequest.Builder();
         //设置查询索引
-        builder.index("patent");
-        //1. 解析检索条件
-        treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
-        //格式化检索式
-        //3. 从es中检索数据
-        Query query = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
+        Query query = null;
+        if (StringUtils.isNotEmpty(searchCondition)) {
+            //1. 解析检索条件
+            treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
+            //格式化检索式
+            //3. 从es中检索数据
+            query = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
+        }
+
 
         EsCountDTO esCountDTO = new EsCountDTO();
         List<EsCountDetailDTO> detailDTOS = new ArrayList<>();
@@ -106,8 +109,9 @@ public class EsCountService {
             //查询es返回数据
             Aggregation aggregation = this.selectAggregation(builder, vo);
             if (query != null) {
+                Query finalQuery = query;
                 Aggregation filtersAgg = new Aggregation.Builder().filters(new FiltersAggregation.Builder()
-                        .filters(i -> i.array(Arrays.asList(query))).build())
+                        .filters(i -> i.array(Arrays.asList(finalQuery))).build())
                         .aggregations(new HashMap() {{
                             put("filters_agg", aggregation);
                         }}).build();
@@ -173,6 +177,40 @@ public class EsCountService {
      * @throws Exception
      */
     public EsCountDTO esAnalysisSearch(EsAllCountVO countVO) throws Exception {
+        String searchCondition = countVO.getCondition();
+        List<EsCustomFieldValueDTO> customFields = countVO.getCustomFields();
+        if (!CollectionUtils.isEmpty(customFields)) {
+            searchCondition = esService.parseCustomField(customFields);
+        }
+        Integer taskId = countVO.getTaskId();
+        Integer projectId = countVO.getProjectId();
+        if( taskId != null){
+            if (searchCondition != null && !"".equals(searchCondition.trim())) {
+                searchCondition = "taskId = " + taskId + " AND " + searchCondition;
+            } else {
+                searchCondition = "taskId = " + taskId;
+            }
+        } else {
+            if (projectId != null) {
+                if (searchCondition != null && !"".equals(searchCondition.trim())) {
+                    searchCondition = "projectId = " + projectId + " AND " + searchCondition;
+                } else {
+                    searchCondition = "projectId = " + projectId;
+                }
+            }
+        }
+
+        SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
+        Query query = null;
+        if (StringUtils.isNotEmpty(searchCondition)) {
+            //1. 解析检索条件
+            treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
+            //格式化检索式
+            //3. 从es中检索数据
+            query = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
+        }
+
         EsCountDTO esCountDTO = new EsCountDTO();
         List<EsCountDetailDTO> detailDTOS = new ArrayList<>();
         List<EsCountVO> countVOS = countVO.getCountVOS();
@@ -184,7 +222,6 @@ public class EsCountService {
             List<String> values = vo.getValues();
 
             //查询es返回数据
-            SearchRequest.Builder builder = new SearchRequest.Builder();
             builder.index("patent");
             IEsAnalysisBuilder iEsAnalysisBuilder = null;
             String json = CommonService.readJsonFile("esAnalysis.json");
@@ -204,6 +241,19 @@ public class EsCountService {
                 iEsAnalysisBuilder.setValues(values);
 
                 Aggregation aggregation = iEsAnalysisBuilder.createAnalyseAgg();
+
+                if (query != null) {
+                    Query finalQuery = query;
+                    Aggregation filtersAgg = new Aggregation.Builder().filters(new FiltersAggregation.Builder()
+                            .filters(i -> i.array(Arrays.asList(finalQuery))).build())
+                            .aggregations(new HashMap() {{
+                                put("filters_agg", aggregation);
+                            }}).build();
+                    builder.aggregations("Agg", filtersAgg);
+                } else {
+                    builder.aggregations("Agg", aggregation);
+                }
+
                 builder.aggregations("Agg", aggregation);
                 SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
 
@@ -215,7 +265,7 @@ public class EsCountService {
                 } else if (childList.contains(field)) {
                     this.getChildAnalysisDTOS(agg, field, detailDTOS);
                 } else if (numberList.contains(field)) {
-                    this.getNumberAnalysisDTOS(agg, field,detailDTOS);
+                    this.getNumberAnalysisDTOS(agg, field, detailDTOS);
                 } else {
                     this.getTermCountDTOS(agg, field, detailDTOS);
                 }
@@ -226,50 +276,99 @@ public class EsCountService {
     }
 
     //测试用的
-    public EsCountDTO esAnalysis(EsCountVO vo) throws Exception {
-        EsCountDTO esCountDTO = new EsCountDTO();
-        List<EsCountDetailDTO> detailDTOS = new ArrayList<>();
-        String field = vo.getField();
-        Integer topN = vo.getTopN();
-        Boolean ifHaveChild = vo.getIfHaveChild();
-        String fieldId = vo.getFieldId();
-        List<String> values = vo.getValues();
+    public EsCountDTO esAnalysis(EsAllCountVO countVO) throws Exception {
+        String searchCondition = countVO.getCondition();
+        List<EsCustomFieldValueDTO> customFields = countVO.getCustomFields();
+        if (!CollectionUtils.isEmpty(customFields)) {
+            searchCondition = esService.parseCustomField(customFields);
+        }
+        Integer taskId = countVO.getTaskId();
+        Integer projectId = countVO.getProjectId();
+        if( taskId != null){
+            if (searchCondition != null && !"".equals(searchCondition.trim())) {
+                searchCondition = "taskId = " + taskId + " AND " + searchCondition;
+            } else {
+                searchCondition = "taskId = " + taskId;
+            }
+        } else {
+            if (projectId != null) {
+                if (searchCondition != null && !"".equals(searchCondition.trim())) {
+                    searchCondition = "projectId = " + projectId + " AND " + searchCondition;
+                } else {
+                    searchCondition = "projectId = " + projectId;
+                }
+            }
+        }
 
-        //查询es返回数据
         SearchRequest.Builder builder = new SearchRequest.Builder();
+        //设置查询索引
         builder.index("patent");
-        IEsAnalysisBuilder iEsAnalysisBuilder = null;
-        String json = CommonService.readJsonFile("esAnalysis.json");
-        List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
-        EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getField().equals(field))
-                .findFirst().orElse(null);
-        if (esConfigVO != null) {
-            iEsAnalysisBuilder = esAnalysisBuilderFactory.getClass(esConfigVO.getEsClass());
-            iEsAnalysisBuilder.setField(esConfigVO.getEsField());
-            iEsAnalysisBuilder.setTopN(topN);
-            iEsAnalysisBuilder.setIfHaveChild(ifHaveChild);
-            iEsAnalysisBuilder.setFieldValue(fieldId);
-            if (iEsAnalysisBuilder.getField().contains(".")) {
-                String path = iEsAnalysisBuilder.getField().substring(0, iEsAnalysisBuilder.getField().indexOf("."));
-                iEsAnalysisBuilder.setPath(path);
-            }
-            iEsAnalysisBuilder.setValues(values);
+        Query query = null;
+        if (StringUtils.isNotEmpty(searchCondition)) {
+            //1. 解析检索条件
+            treeNode tree = expressManager.getInstance().Parse(searchCondition, false);
+            //格式化检索式
+            //3. 从es中检索数据
+            query = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
+        }
 
-            Aggregation aggregation = iEsAnalysisBuilder.createAnalyseAgg();
-            builder.aggregations("Agg", aggregation);
-            SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
+        EsCountDTO esCountDTO = new EsCountDTO();
+        List<EsCountDetailDTO> detailDTOS = new ArrayList<>();
+        List<EsCountVO> countVOS = countVO.getCountVOS();
+        for (EsCountVO vo : countVOS) {
+            String field = vo.getField();
+            Integer topN = vo.getTopN();
+            Boolean ifHaveChild = vo.getIfHaveChild();
+            String fieldId = vo.getFieldId();
+            List<String> values = vo.getValues();
 
-            Aggregate agg = response.aggregations().get("Agg");
-            if (dateList.contains(field)) {
-                this.getDateAnalysisDTOS(agg, field, topN, detailDTOS);
-            } else if (nestedList.contains(field)) {
-                this.getNestedCountDTOS(agg, field, detailDTOS);
-            } else if (childList.contains(field)) {
-                this.getChildAnalysisDTOS(agg, field, detailDTOS);
-            } else if (numberList.contains(field)) {
-                this.getNumberAnalysisDTOS(agg, field,detailDTOS);
-            } else {
-                this.getTermCountDTOS(agg, field, detailDTOS);
+            //查询es返回数据
+            builder.index("patent");
+            IEsAnalysisBuilder iEsAnalysisBuilder = null;
+            String json = CommonService.readJsonFile("esAnalysis.json");
+            List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
+            EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getField().equals(field))
+                    .findFirst().orElse(null);
+            if (esConfigVO != null) {
+                iEsAnalysisBuilder = esAnalysisBuilderFactory.getClass(esConfigVO.getEsClass());
+                iEsAnalysisBuilder.setField(esConfigVO.getEsField());
+                iEsAnalysisBuilder.setTopN(topN);
+                iEsAnalysisBuilder.setIfHaveChild(ifHaveChild);
+                iEsAnalysisBuilder.setFieldValue(fieldId);
+                if (iEsAnalysisBuilder.getField().contains(".")) {
+                    String path = iEsAnalysisBuilder.getField().substring(0, iEsAnalysisBuilder.getField().indexOf("."));
+                    iEsAnalysisBuilder.setPath(path);
+                }
+                iEsAnalysisBuilder.setValues(values);
+
+                Aggregation aggregation = iEsAnalysisBuilder.createAnalyseAgg();
+
+                if (query != null) {
+                    Query finalQuery = query;
+                    Aggregation filtersAgg = new Aggregation.Builder().filters(new FiltersAggregation.Builder()
+                            .filters(i -> i.array(Arrays.asList(finalQuery))).build())
+                            .aggregations(new HashMap() {{
+                                put("filters_agg", aggregation);
+                            }}).build();
+                    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)) {
+                    this.getDateAnalysisDTOS(agg, field, topN, detailDTOS);
+                } else if (nestedList.contains(field)) {
+                    this.getNestedCountDTOS(agg, field, detailDTOS);
+                } else if (childList.contains(field)) {
+                    this.getChildAnalysisDTOS(agg, field, detailDTOS);
+                } else if (numberList.contains(field)) {
+                    this.getNumberAnalysisDTOS(agg, field,detailDTOS);
+                } else {
+                    this.getTermCountDTOS(agg, field, detailDTOS);
+                }
             }
         }
         esCountDTO.setDetailDTOS(detailDTOS);