Spring Boot Elasticsearch初级查询实现-ES索引名查询

Spring Boot Elasticsearch初级查询实现-ES索引名查询

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

引入依赖

	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-data-elasticsearchartifactId>
	dependency>

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

	@Override
    public Page<AlarmHistoryVO> alarmHistoryPage(PageParams<FindHistoryDTO> pageParams, Boolean flag) {
        //使用BoolQueryBuilder进行复合查询
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("companyId", SecurityUtils.getCompanyId()));
        if (StrUtil.isNotBlank(pageParams.getModel().getDeviceIp())){
            boolQueryBuilder.must(QueryBuilders.termQuery("deviceIp",pageParams.getModel().getDeviceIp()));
        }
        if (StrUtil.isNotBlank(pageParams.getModel().getAlarmName())){
            boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("alarmName",pageParams.getModel().getAlarmName()));
        }
        if (pageParams.getModel().getAlarmLevel() != null && pageParams.getModel().getAlarmLevel() != -1){
            boolQueryBuilder.must(QueryBuilders.termQuery("alarmLevel",pageParams.getModel().getAlarmLevel()));
        }
        if (flag) {
            boolQueryBuilder.must(QueryBuilders.termQuery("status", StaticConstant.ZERO));
        } else {
            if (pageParams.getModel().getStatus() != null && pageParams.getModel().getStatus() != -1) {
                boolQueryBuilder.must(QueryBuilders.termQuery("status", pageParams.getModel().getStatus()));
            }
        }
        //时间区间查询 时间段 一定要有头有尾 不然会出现慢查询
        if (StrUtil.isNotBlank(pageParams.getModel().getStartTime()) && StrUtil.isNotBlank(pageParams.getModel().getEndTime())){
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("createTime").from(pageParams.getModel().getStartTime()).to(pageParams.getModel().getEndTime()));
        }
        /*if (StrUtil.isNotBlank(pageParams.getModel().getStartTime())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("createTime").gte(pageParams.getModel().getStartTime()));
        }
        if (StrUtil.isNotBlank(pageParams.getModel().getEndTime())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("createTime").lte(pageParams.getModel().getEndTime()));
        }*/
        NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withTrackTotalHits(true)
                .withQuery(boolQueryBuilder)
                .withPageable(PageRequest.of((int) pageParams.getCurrent() - 1, (int) pageParams.getSize()));
        //时间
        searchQueryBuilder.withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC));

        NativeSearchQuery searchQuery = searchQueryBuilder.build();
        SearchHits<EsAlarmHistoryVO> search = elasticsearchRestTemplate.search(searchQuery, EsAlarmHistoryVO.class);
        List<EsAlarmHistoryVO> list = search
                .stream()
                .map(SearchHit::getContent)
                .collect(Collectors.toList());
        Page<AlarmHistoryVO> page = new Page();
        //封装最终返回前端数据
        List<AlarmHistoryVO> alarmHistoryVOS = BeanUtilPlus.toBeanList(list, AlarmHistoryVO.class);
        if (list.size()>0) {
            List<String> ids = list.stream().map(EsAlarmHistoryVO::getId).collect(Collectors.toList());
            List<AlarmHistoryVO> voList = alarmHistoryService.getPageListByIds(ids);
            for (AlarmHistoryVO historyVO : alarmHistoryVOS) {
                for (AlarmHistoryVO vo : voList) {
                    if (historyVO.getId().equals(vo.getId())) {
                        historyVO.setAlarmDescriptionId(vo.getAlarmDescriptionId());
                        historyVO.setAlarmDescription(vo.getAlarmDescription());
                        alarmHistoryService.addAlarmInfo(historyVO);
                        break;
                    }
                }
            }
        }
        page.setRecords(alarmHistoryVOS);
        page.setPages(pageParams.getSize());
        page.setCurrent(pageParams.getCurrent());
        page.setTotal(search.getTotalHits());
        return page;
    }

查询ES索引名indexName = “alarm_history”

@Data
@Document(indexName = "alarm_history")
public class EsAlarmHistoryVO implements Serializable {
}

下一篇: Spring Boot Elasticsearch中级查询实现-ES索引别名查询.

你可能感兴趣的:(SpringBoot,elasticsearch,spring,boot)