|
@@ -3,8 +3,6 @@ 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.dto.es.EsCustomFieldValueDTO;
|
|
|
-import cn.cslg.pas.common.model.request.MapRequest;
|
|
|
-import cn.cslg.pas.common.model.request.StringRequest;
|
|
|
import cn.cslg.pas.common.utils.parseQueryToTree.expressManager;
|
|
|
import cn.cslg.pas.common.utils.parseQueryToTree.operateNode;
|
|
|
import cn.cslg.pas.common.utils.parseQueryToTree.treeNode;
|
|
@@ -12,19 +10,13 @@ import cn.cslg.pas.common.vo.EsConfigVO;
|
|
|
import cn.cslg.pas.common.vo.business.EsAllCountVO;
|
|
|
import cn.cslg.pas.common.vo.business.EsCountVO;
|
|
|
import cn.cslg.pas.domain.es.Patent;
|
|
|
-import cn.cslg.pas.factorys.EsAnalysisBuilderFactory.EsAnalysisBuilderFactory;
|
|
|
-import cn.cslg.pas.factorys.EsAnalysisBuilderFactory.IEsAnalysisBuilder;
|
|
|
import cn.cslg.pas.factorys.EsCountAnalyseBuilderFactory.EsCountAnalysisBuilderFactory;
|
|
|
import cn.cslg.pas.factorys.EsCountAnalyseBuilderFactory.IEsCountAnalysisBuilder;
|
|
|
-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 cn.hutool.json.JSONUtil;
|
|
|
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;
|
|
@@ -35,6 +27,8 @@ import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -50,11 +44,6 @@ public class EsCountService {
|
|
|
|
|
|
|
|
|
private final ElasticsearchClient client;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private EsCountBuilderFactory esCountBuilderFactory;
|
|
|
- @Autowired
|
|
|
- private EsAnalysisBuilderFactory esAnalysisBuilderFactory;
|
|
|
@Autowired
|
|
|
private EsCountAnalysisBuilderFactory esCountAnalysisBuilderFactory;
|
|
|
@Autowired
|
|
@@ -63,20 +52,19 @@ public class EsCountService {
|
|
|
private EsService esService;
|
|
|
|
|
|
/**
|
|
|
- * 查询专利库中的专利分组聚合统计
|
|
|
- *
|
|
|
- * @param countVO
|
|
|
+ * 聚合统计
|
|
|
+ * @param vo
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public EsCountDTO esCountSearch(EsAllCountVO countVO) throws Exception {
|
|
|
- String searchCondition = countVO.getCondition();
|
|
|
- List<EsCustomFieldValueDTO> customFields = countVO.getCustomFields();
|
|
|
+ public EsCountDTO esCountAnalysis(EsAllCountVO vo) throws Exception {
|
|
|
+ String searchCondition = vo.getCondition();
|
|
|
+ List<EsCustomFieldValueDTO> customFields = vo.getCustomFields();
|
|
|
if (!CollectionUtils.isEmpty(customFields)) {
|
|
|
searchCondition = esService.parseCustomField(customFields);
|
|
|
}
|
|
|
- Integer taskId = countVO.getTaskId();
|
|
|
- Integer projectId = countVO.getProjectId();
|
|
|
+ Integer taskId = vo.getTaskId();
|
|
|
+ Integer projectId = vo.getProjectId();
|
|
|
if (taskId != null) {
|
|
|
if (searchCondition != null && !"".equals(searchCondition.trim())) {
|
|
|
searchCondition = "taskId = " + taskId + " AND " + searchCondition;
|
|
@@ -95,6 +83,8 @@ public class EsCountService {
|
|
|
|
|
|
SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
//设置查询索引
|
|
|
+ builder.index("patent");
|
|
|
+ //设置查询索引
|
|
|
Query query = null;
|
|
|
if (StringUtils.isNotEmpty(searchCondition)) {
|
|
|
//1. 解析检索条件
|
|
@@ -104,272 +94,142 @@ public class EsCountService {
|
|
|
query = formatQueryService.EsQueryToQuery((operateNode) tree, "patent");
|
|
|
}
|
|
|
|
|
|
-
|
|
|
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();
|
|
|
- String condition = searchCondition;
|
|
|
- //查询es返回数据
|
|
|
- Aggregation aggregation = this.selectAggregation(builder, vo);
|
|
|
+ List<EsCountVO> countVOS = vo.getCountVOS();
|
|
|
+ for (EsCountVO countVO : countVOS) {
|
|
|
+ String field = countVO.getField();
|
|
|
+ Integer topN = countVO.getTopN();
|
|
|
+ Boolean ifHaveChild = countVO.getIfHaveChild();
|
|
|
+ String fieldId = countVO.getFieldId();
|
|
|
+ Integer fieldType = countVO.getFieldType();
|
|
|
+ String format = countVO.getFormat();
|
|
|
+ String valueOne = countVO.getValueOne();
|
|
|
+ String valueTwo = countVO.getValueTwo();
|
|
|
+ List<String> values = countVO.getValues();
|
|
|
+
|
|
|
+ IEsCountAnalysisBuilder iEsCountAnalysisBuilder = null;
|
|
|
+ String json = CommonService.readJsonFile("esCountAnalysis.json");
|
|
|
+ List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
|
|
|
+ EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getField().equals(field))
|
|
|
+ .findFirst().orElse(null);
|
|
|
+
|
|
|
+ Aggregation aggregation = null;
|
|
|
+ if (esConfigVO != null) {
|
|
|
+ iEsCountAnalysisBuilder = esCountAnalysisBuilderFactory.getClass(esConfigVO.getEsClass());
|
|
|
+ iEsCountAnalysisBuilder.setField(esConfigVO.getEsField());
|
|
|
+ iEsCountAnalysisBuilder.setValueOne(valueOne);
|
|
|
+ iEsCountAnalysisBuilder.setValueTwo(valueTwo);
|
|
|
+ iEsCountAnalysisBuilder.setValues(values);
|
|
|
+ iEsCountAnalysisBuilder.setFieldId(fieldId);
|
|
|
+ iEsCountAnalysisBuilder.setFieldType(String.valueOf(fieldType));
|
|
|
+ iEsCountAnalysisBuilder.setTopN(topN);
|
|
|
+ iEsCountAnalysisBuilder.setFormat(format);
|
|
|
+ iEsCountAnalysisBuilder.setIfHaveChild(ifHaveChild);
|
|
|
+ if (iEsCountAnalysisBuilder.getField().contains(".")) {
|
|
|
+ String path = iEsCountAnalysisBuilder.getField()
|
|
|
+ .substring(0, iEsCountAnalysisBuilder.getField().indexOf("."));
|
|
|
+ iEsCountAnalysisBuilder.setPath(path);
|
|
|
+ }
|
|
|
+ aggregation = iEsCountAnalysisBuilder.createCountAnalyseAgg();
|
|
|
+ }
|
|
|
if (query != null) {
|
|
|
Query finalQuery = query;
|
|
|
+ Aggregation finalAggregation = aggregation;
|
|
|
Aggregation filtersAgg = new Aggregation.Builder().filters(new FiltersAggregation.Builder()
|
|
|
.filters(i -> i.array(Arrays.asList(finalQuery))).build())
|
|
|
.aggregations(new HashMap() {{
|
|
|
- put("filters_agg", aggregation);
|
|
|
+ put("filters_agg", finalAggregation);
|
|
|
}}).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 (query != null) {
|
|
|
+ String finalSearchCondition = searchCondition;
|
|
|
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,esCountDTO);
|
|
|
- });
|
|
|
+ if (CollectionUtils.isEmpty(values)) {
|
|
|
+ filtersBuckets.forEach(filtersBucket -> {
|
|
|
+// this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
+ Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
+ try {
|
|
|
+ this.getDateCountDTOS(filtersAgg, field, topN,format, detailDTOS,esCountDTO);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ filtersBuckets.forEach(filtersBucket -> {
|
|
|
+// this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
+ Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
+ this.getDateAnalysisDTOS(filtersAgg, field, topN, detailDTOS,esCountDTO);
|
|
|
+ });
|
|
|
+ }
|
|
|
} else if (nestedList.contains(field)) {
|
|
|
filtersBuckets.forEach(filtersBucket -> {
|
|
|
- this.getFiltersCountDTO(filtersBucket, condition, detailDTOS);
|
|
|
+// this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
this.getNestedCountDTOS(filtersAgg, field, detailDTOS);
|
|
|
+ if (!CollectionUtils.isEmpty(values)) {
|
|
|
+ esCountDTO.setAllNumber(filtersAgg.nested().docCount());
|
|
|
+ }
|
|
|
});
|
|
|
} 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,esCountDTO);
|
|
|
- });
|
|
|
+ if (CollectionUtils.isEmpty(values)) {
|
|
|
+ filtersBuckets.forEach(filtersBucket -> {
|
|
|
+// this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
+ Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
+ this.getChildCountDTOS(filtersAgg, field, detailDTOS,esCountDTO);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ filtersBuckets.forEach(filtersBucket -> {
|
|
|
+// this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
+ Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
+ this.getChildAnalysisDTOS(filtersAgg, field, detailDTOS,esCountDTO);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } else if (numberList.contains(field)) {
|
|
|
+ if (CollectionUtils.isEmpty(values)) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ filtersBuckets.forEach(filtersBucket -> {
|
|
|
+// this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
+ Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
+ this.getNumberAnalysisDTOS(filtersAgg, field, detailDTOS);
|
|
|
+ });
|
|
|
+ }
|
|
|
} else {
|
|
|
filtersBuckets.forEach(filtersBucket -> {
|
|
|
- this.getFiltersCountDTO(filtersBucket, condition, detailDTOS);
|
|
|
+// this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
this.getTermCountDTOS(filtersAgg, field, detailDTOS);
|
|
|
});
|
|
|
}
|
|
|
} else {
|
|
|
- this.getFilterCountDTO(agg, condition, detailDTOS);
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (dateList.contains(field)) {
|
|
|
- this.getDateCountDTOS(agg, field, topN, detailDTOS,esCountDTO);
|
|
|
- } else if (nestedList.contains(field)) {
|
|
|
- this.getNestedCountDTOS(agg, field, detailDTOS);
|
|
|
- } else if (childList.contains(field)) {
|
|
|
- this.getChildCountDTOS(agg, field, detailDTOS,esCountDTO);
|
|
|
- } else {
|
|
|
- this.getTermCountDTOS(agg, field, detailDTOS);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- esCountDTO.setDetailDTOS(detailDTOS);
|
|
|
- return esCountDTO;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 专利的聚合分析
|
|
|
- *
|
|
|
- * @param countVO
|
|
|
- * @return
|
|
|
- * @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();
|
|
|
- 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();
|
|
|
-
|
|
|
- //查询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);
|
|
|
- }
|
|
|
-
|
|
|
- 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,esCountDTO);
|
|
|
- } else if (nestedList.contains(field)) {
|
|
|
- this.getNestedCountDTOS(agg, field, detailDTOS);
|
|
|
- } else if (childList.contains(field)) {
|
|
|
- this.getChildAnalysisDTOS(agg, field, detailDTOS, esCountDTO);
|
|
|
- } else if (numberList.contains(field)) {
|
|
|
- this.getNumberAnalysisDTOS(agg, field, detailDTOS);
|
|
|
- } else {
|
|
|
- this.getTermCountDTOS(agg, field, detailDTOS);
|
|
|
+ this.getFilterCountDTO(agg, finalSearchCondition, detailDTOS);
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- esCountDTO.setDetailDTOS(detailDTOS);
|
|
|
- return esCountDTO;
|
|
|
- }
|
|
|
-
|
|
|
- //测试用的
|
|
|
- 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;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
- //设置查询索引
|
|
|
- builder.index("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<>();
|
|
|
- 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();
|
|
|
-
|
|
|
- //查询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,esCountDTO);
|
|
|
+ if (CollectionUtils.isEmpty(values)) {
|
|
|
+ this.getDateCountDTOS(agg, field, topN,format, detailDTOS,esCountDTO);
|
|
|
+ } else {
|
|
|
+ this.getDateAnalysisDTOS(agg, field, topN, detailDTOS,esCountDTO);
|
|
|
+ }
|
|
|
} else if (nestedList.contains(field)) {
|
|
|
this.getNestedCountDTOS(agg, field, detailDTOS);
|
|
|
+ if (!CollectionUtils.isEmpty(values)) {
|
|
|
+ esCountDTO.setAllNumber(agg.nested().docCount());
|
|
|
+ }
|
|
|
} else if (childList.contains(field)) {
|
|
|
- this.getChildAnalysisDTOS(agg, field, detailDTOS, esCountDTO);
|
|
|
+ if (CollectionUtils.isEmpty(values)) {
|
|
|
+ this.getChildCountDTOS(agg, field, detailDTOS,esCountDTO);
|
|
|
+ } else {
|
|
|
+ this.getChildAnalysisDTOS(agg, field, detailDTOS, esCountDTO);
|
|
|
+ }
|
|
|
} else if (numberList.contains(field)) {
|
|
|
this.getNumberAnalysisDTOS(agg, field, detailDTOS);
|
|
|
} else {
|
|
@@ -378,51 +238,11 @@ public class EsCountService {
|
|
|
}
|
|
|
}
|
|
|
esCountDTO.setDetailDTOS(detailDTOS);
|
|
|
+ esCountDTO.setCondition(searchCondition);
|
|
|
return esCountDTO;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取聚合后的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();
|
|
|
- Integer fieldType = vo.getFieldType();
|
|
|
- String fieldId = vo.getFieldId();
|
|
|
- Aggregation aggregation = null;
|
|
|
-
|
|
|
- builder.index("patent");
|
|
|
- IEsCountBuilder iEsCountBuilder = null;
|
|
|
- String json = CommonService.readJsonFile("esCount.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) {
|
|
|
- iEsCountBuilder = esCountBuilderFactory.getClass(esConfigVO.getEsClass());
|
|
|
- iEsCountBuilder.setField(esConfigVO.getEsField());
|
|
|
- iEsCountBuilder.setFieldId(fieldId);
|
|
|
- iEsCountBuilder.setValueOne(valueOne);
|
|
|
- iEsCountBuilder.setValueTwo(valueTwo);
|
|
|
- iEsCountBuilder.setTopN(topN);
|
|
|
- iEsCountBuilder.setIfHaveChild(ifHaveChild);
|
|
|
- iEsCountBuilder.setFieldType(String.valueOf(fieldType));
|
|
|
- if (iEsCountBuilder.getField().contains(".")) {
|
|
|
- String path = iEsCountBuilder.getField().substring(0, iEsCountBuilder.getField().indexOf("."));
|
|
|
- iEsCountBuilder.setPath(path);
|
|
|
- }
|
|
|
- aggregation = iEsCountBuilder.createAggregation();
|
|
|
- }
|
|
|
- return aggregation;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* 获取Filter聚合返回数据
|
|
|
*
|
|
|
* @param agg
|
|
@@ -440,23 +260,6 @@ public class EsCountService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取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
|
|
@@ -580,23 +383,72 @@ public class EsCountService {
|
|
|
* @param field
|
|
|
* @param detailDTOS
|
|
|
*/
|
|
|
- public void getDateCountDTOS(Aggregate agg, String field, Integer topN, List<EsCountDetailDTO> detailDTOS,EsCountDTO esCountDTO) {
|
|
|
+ public void getDateCountDTOS(Aggregate agg, String field, Integer topN, String format, List<EsCountDetailDTO> detailDTOS, EsCountDTO esCountDTO) throws ParseException {
|
|
|
List<DateHistogramBucket> list = agg.dateHistogram().buckets().array();
|
|
|
esCountDTO.setAllNumber(Long.valueOf(String.valueOf(list.size())));
|
|
|
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 (format.equals("半年")) {
|
|
|
+ List<EsCountDetailDTO> detailDTOList = new ArrayList<>();
|
|
|
+ for (DateHistogramBucket bucket : list) {
|
|
|
+ String year = bucket.keyAsString();
|
|
|
+ SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
|
|
|
+ String startMonth = year + "-01";
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ Date date = monthFormat.parse(startMonth);
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.MONTH, 6);
|
|
|
+ Date halfYearDate = calendar.getTime();
|
|
|
+ Aggregate aggregate = bucket.aggregations().get("halfYearAgg");
|
|
|
+ List<DateHistogramBucket> buckets = aggregate.dateHistogram().buckets().array();
|
|
|
+ long h1Count = 0;
|
|
|
+ long h2Count = 0;
|
|
|
+ for (DateHistogramBucket histogramBucket : buckets) {
|
|
|
+ String key = histogramBucket.keyAsString();
|
|
|
+ Date month = monthFormat.parse(key);
|
|
|
+ EsCountDetailDTO dto = new EsCountDetailDTO();
|
|
|
+ dto.setField(field);
|
|
|
+ dto.setName(year + "-H1");
|
|
|
+ dto.setNumber(h1Count);
|
|
|
+ EsCountDetailDTO dto2 = new EsCountDetailDTO();
|
|
|
+ dto2.setField(field);
|
|
|
+ dto2.setName(year + "-H2");
|
|
|
+ dto2.setNumber(h2Count);
|
|
|
+ if (month.before(halfYearDate)) {
|
|
|
+ h1Count += histogramBucket.docCount();
|
|
|
+ dto.setNumber(h1Count);
|
|
|
+ }
|
|
|
+ if (month.after(halfYearDate)){
|
|
|
+ h2Count += histogramBucket.docCount();
|
|
|
+ dto2.setNumber(h2Count);
|
|
|
+ }
|
|
|
+ detailDTOList.add(dto);
|
|
|
+ detailDTOList.add(dto2);
|
|
|
+ }
|
|
|
}
|
|
|
- if (dto.getNumber() > 0) {
|
|
|
- esCountDetailDTOS.add(dto);
|
|
|
+ Map<String, List<EsCountDetailDTO>> map = detailDTOList.stream().collect(Collectors.groupingBy(EsCountDetailDTO::getName));
|
|
|
+ for (String key : map.keySet()) {
|
|
|
+ List<EsCountDetailDTO> dtoList = map.get(key);
|
|
|
+ long sum = dtoList.stream().mapToLong(EsCountDetailDTO::getNumber).sum();
|
|
|
+ EsCountDetailDTO detailDTO = dtoList.get(0);
|
|
|
+ detailDTO.setNumber(sum);
|
|
|
+ esCountDetailDTOS.add(detailDTO);
|
|
|
}
|
|
|
- });
|
|
|
+ } else {
|
|
|
+ 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());
|
|
@@ -657,191 +509,5 @@ public class EsCountService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 聚合统计
|
|
|
- * @param vo
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public EsCountDTO esCountAnalysis(EsAllCountVO vo) throws Exception {
|
|
|
- String searchCondition = vo.getCondition();
|
|
|
- List<EsCustomFieldValueDTO> customFields = vo.getCustomFields();
|
|
|
- if (!CollectionUtils.isEmpty(customFields)) {
|
|
|
- searchCondition = esService.parseCustomField(customFields);
|
|
|
- }
|
|
|
- Integer taskId = vo.getTaskId();
|
|
|
- Integer projectId = vo.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();
|
|
|
- //设置查询索引
|
|
|
- builder.index("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<>();
|
|
|
- List<EsCountVO> countVOS = vo.getCountVOS();
|
|
|
- for (EsCountVO countVO : countVOS) {
|
|
|
- String field = countVO.getField();
|
|
|
- Integer topN = countVO.getTopN();
|
|
|
- Boolean ifHaveChild = countVO.getIfHaveChild();
|
|
|
- String fieldId = countVO.getFieldId();
|
|
|
- Integer fieldType = countVO.getFieldType();
|
|
|
- String format = countVO.getFormat();
|
|
|
- String valueOne = countVO.getValueOne();
|
|
|
- String valueTwo = countVO.getValueTwo();
|
|
|
- List<String> values = countVO.getValues();
|
|
|
-
|
|
|
- IEsCountAnalysisBuilder iEsCountAnalysisBuilder = null;
|
|
|
- String json = CommonService.readJsonFile("esCountAnalysis.json");
|
|
|
- List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
|
|
|
- EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getField().equals(field))
|
|
|
- .findFirst().orElse(null);
|
|
|
-
|
|
|
- Aggregation aggregation = null;
|
|
|
- if (esConfigVO != null) {
|
|
|
- iEsCountAnalysisBuilder = esCountAnalysisBuilderFactory.getClass(esConfigVO.getEsClass());
|
|
|
- iEsCountAnalysisBuilder.setField(esConfigVO.getEsField());
|
|
|
- iEsCountAnalysisBuilder.setValueOne(valueOne);
|
|
|
- iEsCountAnalysisBuilder.setValueTwo(valueTwo);
|
|
|
- iEsCountAnalysisBuilder.setValues(values);
|
|
|
- iEsCountAnalysisBuilder.setFieldId(fieldId);
|
|
|
- iEsCountAnalysisBuilder.setFieldType(String.valueOf(fieldType));
|
|
|
- iEsCountAnalysisBuilder.setTopN(topN);
|
|
|
- iEsCountAnalysisBuilder.setFormat(format);
|
|
|
- iEsCountAnalysisBuilder.setIfHaveChild(ifHaveChild);
|
|
|
- if (iEsCountAnalysisBuilder.getField().contains(".")) {
|
|
|
- String path = iEsCountAnalysisBuilder.getField()
|
|
|
- .substring(0, iEsCountAnalysisBuilder.getField().indexOf("."));
|
|
|
- iEsCountAnalysisBuilder.setPath(path);
|
|
|
- }
|
|
|
- aggregation = iEsCountAnalysisBuilder.createCountAnalyseAgg();
|
|
|
- }
|
|
|
- if (query != null) {
|
|
|
- Query finalQuery = query;
|
|
|
- Aggregation finalAggregation = aggregation;
|
|
|
- Aggregation filtersAgg = new Aggregation.Builder().filters(new FiltersAggregation.Builder()
|
|
|
- .filters(i -> i.array(Arrays.asList(finalQuery))).build())
|
|
|
- .aggregations(new HashMap() {{
|
|
|
- put("filters_agg", finalAggregation);
|
|
|
- }}).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 (query != null) {
|
|
|
- String finalSearchCondition = searchCondition;
|
|
|
- if (StringUtils.isNotEmpty(field)) {
|
|
|
- List<FiltersBucket> filtersBuckets = agg.filters().buckets().array();
|
|
|
- if (dateList.contains(field)) {
|
|
|
- if (CollectionUtils.isEmpty(values)) {
|
|
|
- filtersBuckets.forEach(filtersBucket -> {
|
|
|
- this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
- this.getDateCountDTOS(filtersAgg, field, topN, detailDTOS,esCountDTO);
|
|
|
- });
|
|
|
- } else {
|
|
|
- filtersBuckets.forEach(filtersBucket -> {
|
|
|
- this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
- this.getDateAnalysisDTOS(filtersAgg, field, topN, detailDTOS,esCountDTO);
|
|
|
- });
|
|
|
- }
|
|
|
- } else if (nestedList.contains(field)) {
|
|
|
- filtersBuckets.forEach(filtersBucket -> {
|
|
|
- this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
- this.getNestedCountDTOS(filtersAgg, field, detailDTOS);
|
|
|
- if (!CollectionUtils.isEmpty(values)) {
|
|
|
- esCountDTO.setAllNumber(filtersAgg.nested().docCount());
|
|
|
- }
|
|
|
- });
|
|
|
- } else if (childList.contains(field)) {
|
|
|
- if (CollectionUtils.isEmpty(values)) {
|
|
|
- filtersBuckets.forEach(filtersBucket -> {
|
|
|
- this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
- this.getChildCountDTOS(filtersAgg, field, detailDTOS,esCountDTO);
|
|
|
- });
|
|
|
- } else {
|
|
|
- filtersBuckets.forEach(filtersBucket -> {
|
|
|
- this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
- this.getChildAnalysisDTOS(filtersAgg, field, detailDTOS,esCountDTO);
|
|
|
- });
|
|
|
- }
|
|
|
- } else if (numberList.contains(field)) {
|
|
|
- if (CollectionUtils.isEmpty(values)) {
|
|
|
-
|
|
|
- } else {
|
|
|
- filtersBuckets.forEach(filtersBucket -> {
|
|
|
- this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
- this.getNumberAnalysisDTOS(filtersAgg, field, detailDTOS);
|
|
|
- });
|
|
|
- }
|
|
|
- } else {
|
|
|
- filtersBuckets.forEach(filtersBucket -> {
|
|
|
- this.getFiltersCountDTO(filtersBucket, finalSearchCondition, detailDTOS);
|
|
|
- Aggregate filtersAgg = filtersBucket.aggregations().get("filters_agg");
|
|
|
- this.getTermCountDTOS(filtersAgg, field, detailDTOS);
|
|
|
- });
|
|
|
- }
|
|
|
- } else {
|
|
|
- this.getFilterCountDTO(agg, finalSearchCondition, detailDTOS);
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (dateList.contains(field)) {
|
|
|
- if (CollectionUtils.isEmpty(values)) {
|
|
|
- this.getDateCountDTOS(agg, field, topN, detailDTOS,esCountDTO);
|
|
|
- } else {
|
|
|
- this.getDateAnalysisDTOS(agg, field, topN, detailDTOS,esCountDTO);
|
|
|
- }
|
|
|
- } else if (nestedList.contains(field)) {
|
|
|
- this.getNestedCountDTOS(agg, field, detailDTOS);
|
|
|
- if (!CollectionUtils.isEmpty(values)) {
|
|
|
- esCountDTO.setAllNumber(agg.nested().docCount());
|
|
|
- }
|
|
|
- } else if (childList.contains(field)) {
|
|
|
- if (CollectionUtils.isEmpty(values)) {
|
|
|
- this.getChildCountDTOS(agg, field, detailDTOS,esCountDTO);
|
|
|
- } else {
|
|
|
- this.getChildAnalysisDTOS(agg, field, detailDTOS, esCountDTO);
|
|
|
- }
|
|
|
- } else if (numberList.contains(field)) {
|
|
|
- this.getNumberAnalysisDTOS(agg, field, detailDTOS);
|
|
|
- } else {
|
|
|
- this.getTermCountDTOS(agg, field, detailDTOS);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- esCountDTO.setDetailDTOS(detailDTOS);
|
|
|
- esCountDTO.setCondition(searchCondition);
|
|
|
- return esCountDTO;
|
|
|
- }
|
|
|
|
|
|
}
|