|
@@ -29,7 +29,7 @@ import java.util.stream.Collectors;
|
|
|
@Service
|
|
|
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
|
|
public class EsCountService {
|
|
|
- private final List<String> childList = Arrays.asList("childRaw");
|
|
|
+ private final List<String> childList = Arrays.asList("field");
|
|
|
private final List<String> nestedList = Arrays.asList("PA", "IN", "PE");
|
|
|
private final List<String> dateList = Arrays.asList("PD", "AD");
|
|
|
|
|
@@ -45,36 +45,19 @@ public class EsCountService {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public EsCountDTO esCountSearch(List<EsCountVO> countVOS) throws Exception {
|
|
|
+ public EsCountDTO esCountSearch(List<EsCountVO> countVOS,String condition) 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();
|
|
|
|
|
|
+ //查询es返回数据
|
|
|
SearchRequest.Builder builder = new SearchRequest.Builder();
|
|
|
- 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.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();
|
|
|
+ Aggregation aggregation = this.selectAggregation(builder,vo);
|
|
|
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();
|
|
@@ -93,7 +76,8 @@ public class EsCountService {
|
|
|
}
|
|
|
});
|
|
|
if (!CollectionUtils.isEmpty(esCountDetailDTOS)) {
|
|
|
- List<EsCountDetailDTO> collect = esCountDetailDTOS.stream().sorted(Comparator.comparing(EsCountDetailDTO::getName).reversed()).limit(topN).collect(Collectors.toList());
|
|
|
+ List<EsCountDetailDTO> collect = esCountDetailDTOS.stream().sorted(Comparator.comparing(EsCountDetailDTO::getName).reversed())
|
|
|
+ .limit(topN).collect(Collectors.toList());
|
|
|
detailDTOS.addAll(collect);
|
|
|
}
|
|
|
} else if (nestedList.contains(field)) {
|
|
@@ -148,4 +132,118 @@ public class EsCountService {
|
|
|
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 (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) {
|
|
|
+ String valueOne = vo.getValueOne();
|
|
|
+ String valueTwo = vo.getValueTwo();
|
|
|
+ Integer topN = vo.getTopN();
|
|
|
+ Boolean ifHaveChild = vo.getIfHaveChild();
|
|
|
+ String field = vo.getField();
|
|
|
+
|
|
|
+ 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.setValueOne(valueOne);
|
|
|
+ iEsCountBuilder.setValueTwo(valueTwo);
|
|
|
+ iEsCountBuilder.setTopN(topN);
|
|
|
+ iEsCountBuilder.setIfHaveChild(ifHaveChild);
|
|
|
+ if (iEsCountBuilder.getField().contains(".")) {
|
|
|
+ String path = iEsCountBuilder.getField().substring(0, iEsCountBuilder.getField().indexOf("."));
|
|
|
+ iEsCountBuilder.setPath(path);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return iEsCountBuilder.createAggregation();
|
|
|
+ }
|
|
|
+
|
|
|
}
|