当前位置:   article > 正文

Spring Boot Elasticsearch高级查询实现-ES索引别名聚合查询_springboot es 查询别名

springboot es 查询别名

Spring Boot Elasticsearch7.6.2高级查询实现-ES索引别名聚合查询

注意:我的版本是elasticsearch7.6.2、spring-boot-starter-data-elasticsearch-2.5.6

引入依赖

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
	</dependency>
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述在这里插入图片描述

ES分页查询接口实现,直接通过索引别名聚合查询

	@SneakyThrows
    @Override
    public Page<AnalysisHistoryVO> alarmNamePage(PageParams<FindHistoryDTO> findPageParams) {
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("companyId", SecurityUtils.getCompanyId()));
        boolQueryBuilder.must(QueryBuilders.termQuery("status",StaticConstant.ZERO));
        boolQueryBuilder.must(QueryBuilders.termQuery("alarmStatus",StaticConstant.ZERO));
        if (findPageParams.getModel().getAlarmLevel() != null && findPageParams.getModel().getAlarmLevel() != -1){
            boolQueryBuilder.must(QueryBuilders.termQuery("alarmLevel",findPageParams.getModel().getAlarmLevel()));
        }
        TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("alarmName").field("alarmName").size(1000);
        TermsAggregationBuilder level = AggregationBuilders.terms("alarmLevel").field("alarmLevel");
//        TermsAggregationBuilder field = AggregationBuilders.terms("status").field("status");
//        level.subAggregation(field);
        aggregationBuilder.subAggregation(level);

        NativeSearchQuery build = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).addAggregation(aggregationBuilder).withTrackTotalHits(true).build();
        searchRequest.source(sourceBuilder);
        SearchHits<EsAlarmHistory> search = elasticsearchRestTemplate.search(build, EsAlarmHistory.class, IndexCoordinates.of("alarm_history_search"));
        Aggregations aggregations = (Aggregations) search.getAggregations();

        Map<AnalysisHistoryVO, Long> count = EsUtil.count(aggregations, AnalysisHistoryVO.class);
        count.forEach(AnalysisHistoryVO::setDocCount);
        List<AnalysisHistoryVO> historyVOList = new ArrayList<>(count.keySet());
        //list按指定长度分割,按500分割
        List<List<AnalysisHistoryVO>> splitList = ListUtil.split(historyVOList, (int) findPageParams.getSize());
        Page<AnalysisHistoryVO> page = new Page();
        if (splitList.size()>0) {
            page.setRecords(splitList.get((int) findPageParams.getCurrent() - 1));
        }
        page.setPages(findPageParams.getSize());
        page.setCurrent(findPageParams.getCurrent());
        page.setTotal(historyVOList.size());
        return page;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

实体中无需添加ES索引名信息

@Data
public class EsAlarmHistoryVO implements Serializable {
}
  • 1
  • 2
  • 3

上一篇: Spring Boot Elasticsearch高级查询实现-ES索引别名查询升级版.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/409868
推荐阅读
相关标签
  

闽ICP备14008679号