SpringBoot整合ElasticSearch实现分页查询

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


环境准备

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

<!-- 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来实现


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来实现

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来实现分页查询的两种方式

相关推荐

  1. SpringBoot 整合ES实现查询和滚动查询

    2024-01-25 04:44:01       35 阅读
  2. springboot查询

    2024-01-25 04:44:01       38 阅读
  3. PageHelper实现查询

    2024-01-25 04:44:01       16 阅读
  4. springboot+Vue实现

    2024-01-25 04:44:01       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-25 04:44:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-25 04:44:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-25 04:44:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-25 04:44:01       20 阅读

热门阅读

  1. springboot切面怎么将参数修改后传给目标方法

    2024-01-25 04:44:01       35 阅读
  2. Golang 定时任务的几种实现方法

    2024-01-25 04:44:01       34 阅读
  3. C# 实现 Vigenere 密码

    2024-01-25 04:44:01       27 阅读
  4. C++拾遗(四)引用与指针

    2024-01-25 04:44:01       27 阅读
  5. ROS学习笔记10——自定义源文件调用

    2024-01-25 04:44:01       35 阅读
  6. springboot集成mybatis处理json类型

    2024-01-25 04:44:01       35 阅读
  7. 汽车数据解决方案:通过更好的数据提高速度

    2024-01-25 04:44:01       32 阅读
  8. c语言之goto语句

    2024-01-25 04:44:01       34 阅读