SpringBoot整合ElasticSearch实现分页查询

本文使用SpringBoot整合ElasticSearch实现分页查询

文章目录


环境准备

还是继续使用spring-boot-starter-data-elasticsearch来实现分页查询操作

xml 复制代码
<!-- spring-boot-starter-data-elasticsearch-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <version>2.6.6</version>
</dependency>

数据准备


分页查询

方式一

使用ElasticsearchRestTemplate来实现

java 复制代码
import cn.wideth.po.Article;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Api(value = "es分页查询测试")
@RestController
@RequestMapping("/api/listEs")
public class ArticleListController {


    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    // 分页列表查询
    // 旧版本的 Repository 中的 search 方法被废弃了。
    // 这里采用 ElasticSearchRestTemplate
    @GetMapping("/pageList")
    @ApiOperation("ES分页查询-方法一")
    public Map pageList(Integer currentPage, Integer limit) {


        NativeSearchQuery query = new NativeSearchQuery(new BoolQueryBuilder());
        query.setPageable(PageRequest.of(currentPage, limit));

        // 方法1:
        SearchHits<Article> searchHits = elasticsearchRestTemplate.search(query, Article.class);

        List<Article> articles = searchHits.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());

        Map jsonResult = new LinkedHashMap<>();
        jsonResult.put("count", searchHits.getTotalHits());
        jsonResult.put("articles", articles);
        return jsonResult;
    }
}

程序结果


方式二

使用ElasticsearchOperations来实现

java 复制代码
import cn.wideth.po.Article;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Api(value = "es分页查询测试2")
@RestController
@RequestMapping("/api/listEs")
public class ArticleListTwoController {


    @Autowired
    private ElasticsearchOperations elasticsearchOperations;


    // 分页列表查询
    // 旧版本的 Repository 中的 search 方法被废弃了。
    // 这里采用 ElasticsearchOperations 来进行分页查询
    @GetMapping("/pageList2")
    @ApiOperation("ES分页查询-方法二")
    public Map pageList2(Integer currentPage, Integer limit) {


        NativeSearchQuery query = new NativeSearchQuery(new BoolQueryBuilder());
        query.setPageable(PageRequest.of(currentPage, limit));

        // 方法2:
        SearchHits<Article> searchHits = elasticsearchOperations.search(query, Article.class);

        List<Article> articles = searchHits.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());

        Map jsonResult = new LinkedHashMap<>();

        jsonResult.put("count", searchHits.getTotalHits());
        jsonResult.put("articles", articles);
        return jsonResult;
    }

}

程序结果


本文小结

本文记录了SpringBoot整合ElasticSearch来实现分页查询的两种方式

相关推荐
Hello.Reader5 小时前
Elasticsearch Ruby 客户端安装与版本兼容指南
elasticsearch·jenkins·ruby
Elasticsearch9 小时前
加速你的故障排查:使用 Elasticsearch 构建家电手册的 RAG 应用
elasticsearch
激昂网络11 小时前
android kernel代码 common-android13-5.15 下载 编译
android·大数据·elasticsearch
huisheng_qaq12 小时前
【ElasticSearch实用篇-03】QueryDsl高阶用法以及缓存机制
elasticsearch·缓存·nosql·querydsl·score打分机制
颜如玉1 天前
ElasticSearch关键参数备忘
后端·elasticsearch·搜索引擎
健康平安的活着1 天前
es7.x es的高亮与solr高亮查询的对比&对比说明
大数据·elasticsearch·solr
Elasticsearch1 天前
使用 FastAPI 的 WebSockets 和 Elasticsearch 来构建实时应用
elasticsearch
百思可瑞教育2 天前
Git 对象存储:理解底层原理,实现高效排错与存储优化
大数据·git·elasticsearch·搜索引擎
陆小叁2 天前
基于Flink CDC实现联系人与标签数据实时同步至ES的实践
java·elasticsearch·flink
2501_930104043 天前
GitCode 疑难问题诊疗:全方位指南
大数据·elasticsearch·gitcode