|
@@ -23,17 +23,21 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.data.elasticsearch.client.elc.ElasticsearchAggregations;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
@Service
|
|
|
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
|
|
public class EsCountService {
|
|
|
private final List<String> termList = Arrays.asList();
|
|
|
- private final List<String> nestedList = Arrays.asList();
|
|
|
- private final List<String> dateList = Arrays.asList();
|
|
|
+ private final List<String> nestedList = Arrays.asList("PA","IN","PE");
|
|
|
+ private final List<String> dateList = Arrays.asList("PD","AD");
|
|
|
|
|
|
private final ElasticsearchClient client;
|
|
|
|
|
@@ -42,57 +46,97 @@ public class EsCountService {
|
|
|
|
|
|
/**
|
|
|
* 查询专利库中的专利分组聚合统计
|
|
|
- * @param vo
|
|
|
+ *
|
|
|
+ * @param countVOS
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public EsCountDTO esCountSearch(EsCountVO vo) throws Exception {
|
|
|
- String field = vo.getField();
|
|
|
- String valueOne = vo.getValueOne();
|
|
|
- String valueTwo = vo.getValueTwo();
|
|
|
- Integer topN = vo.getTopN();
|
|
|
+ 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();
|
|
|
+ String valueOne = vo.getValueOne();
|
|
|
+ String valueTwo = vo.getValueTwo();
|
|
|
+ Integer topN = vo.getTopN();
|
|
|
|
|
|
- SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
- builder.index("patent_v1");
|
|
|
- IEsCountBuilder iEsCountBuilder = null;
|
|
|
- String json = CommonService.readJsonFile("esCount.json");
|
|
|
- List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
|
|
|
- EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getValue().equals(field)).findFirst().orElse(null);
|
|
|
- if (esConfigVO != null) {
|
|
|
- iEsCountBuilder = esCountBuilderFactory.getClass(esConfigVO.getEsClass());
|
|
|
- iEsCountBuilder.setField(esConfigVO.getEsField());
|
|
|
- iEsCountBuilder.setValueOne(valueOne);
|
|
|
- iEsCountBuilder.setValueTwo(valueTwo);
|
|
|
- iEsCountBuilder.setTopN(topN);
|
|
|
- if (iEsCountBuilder.getField().contains(".")) {
|
|
|
- String path = iEsCountBuilder.getField().substring(0, iEsCountBuilder.getField().indexOf("."));
|
|
|
- iEsCountBuilder.setPath(path);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- Aggregation aggregation = iEsCountBuilder.createAggregation();
|
|
|
- builder.aggregations("Agg", aggregation);
|
|
|
- SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
- Aggregate agg = response.aggregations().get("Agg");
|
|
|
-// List<DateHistogramBucket> array = agg.autoDateHistogram().buckets().array();
|
|
|
-// agg.nested().aggregations().get("terms_agg");
|
|
|
+ SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
+ builder.index("patent_v1");
|
|
|
+ IEsCountBuilder iEsCountBuilder = null;
|
|
|
+ String json = CommonService.readJsonFile("esCount.json");
|
|
|
+ List<EsConfigVO> esConfigVOS = JSON.parseArray(json, EsConfigVO.class);
|
|
|
+ EsConfigVO esConfigVO = esConfigVOS.stream().filter(item -> item.getValue().equals(field)).findFirst().orElse(null);
|
|
|
+ if (esConfigVO != null) {
|
|
|
+ iEsCountBuilder = esCountBuilderFactory.getClass(esConfigVO.getEsClass());
|
|
|
+ iEsCountBuilder.setField(esConfigVO.getEsField());
|
|
|
+ iEsCountBuilder.setValueOne(valueOne);
|
|
|
+ iEsCountBuilder.setValueTwo(valueTwo);
|
|
|
+ iEsCountBuilder.setTopN(topN);
|
|
|
+ if (iEsCountBuilder.getField().contains(".")) {
|
|
|
+ String path = iEsCountBuilder.getField().substring(0, iEsCountBuilder.getField().indexOf("."));
|
|
|
+ iEsCountBuilder.setPath(path);
|
|
|
+ }
|
|
|
|
|
|
- List<EsCountDetailDTO> detailDTOS = new ArrayList<>();
|
|
|
- List<StringTermsBucket> list = agg.sterms().buckets().array();
|
|
|
- list.forEach(bucket -> {
|
|
|
- EsCountDetailDTO dto = new EsCountDetailDTO();
|
|
|
- Aggregate aggregate = bucket.aggregations().get("filter_agg");
|
|
|
- dto.setName(bucket.key().stringValue());
|
|
|
- dto.setNumber(bucket.docCount());
|
|
|
- if (aggregate != null) {
|
|
|
- dto.setNumber(aggregate.filter().docCount());
|
|
|
- dto.setName(bucket.key().stringValue());
|
|
|
}
|
|
|
- if (dto.getNumber() > 0) {
|
|
|
- detailDTOS.add(dto);
|
|
|
+ Aggregation aggregation = iEsCountBuilder.createAggregation();
|
|
|
+ builder.aggregations("Agg", aggregation);
|
|
|
+ SearchResponse<Patent> response = client.search(builder.build(), Patent.class);
|
|
|
+ Aggregate agg = response.aggregations().get("Agg");
|
|
|
+ List<EsCountDetailDTO> countDetailDTOS = new ArrayList<>();
|
|
|
+ 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 (!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 {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
- });
|
|
|
+// detailDTOS.addAll(countDetailDTOS);
|
|
|
+ }
|
|
|
esCountDTO.setDetailDTOS(detailDTOS);
|
|
|
return esCountDTO;
|
|
|
}
|