当前位置: 首页 > wzjs >正文

静海网站建设制作百度代发排名

静海网站建设制作,百度代发排名,网络舆情优化公司,产品推广软文500字本文使用SpringBoot整合ElasticSearch实现分页查询 文章目录 环境准备分页查询方式一方式二 本文小结 环境准备 还是继续使用spring-boot-starter-data-elasticsearch来实现分页查询操作 <!-- spring-boot-starter-data-elasticsearch--> <dependency><groupId&…

本文使用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 {@Autowiredprivate 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 {@Autowiredprivate 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来实现分页查询的两种方式

http://www.dtcms.com/wzjs/288070.html

相关文章:

  • 做海报哪个网站好站长工具查询系统
  • 公司注册信息查询鹤岗网站seo
  • 徐州网站制作需要多少钱上海小红书seo
  • 深喘旋磨做紧夹断妖精网站百度问答平台
  • 网站建设第三方百度知道提问首页
  • 网站建设报价方案对比网页制作的步骤
  • 网站自助平台软文宣传
  • 宣传片制作app优化大师的功能有哪些
  • 上海移动官网网站建设抖音代运营收费详细价格
  • 台州网站建设 网站制作 网站设计石家庄头条今日头条新闻
  • 昆明优化网站商品seo关键词优化
  • java如何对网站做压力测试《新闻联播》 今天
  • 龙岗汤坑社区网站建设微信加精准客源软件
  • 公益网站怎么做百度一下你知道
  • 我要注册邮箱独立站seo
  • wordpress在线报名插件seo伪原创工具
  • wordpress按照视频佛山市seo推广联系方式
  • 制作xml网站地图文件站长工具视频
  • 深圳企业信息查询网seo收费
  • 做美食网站视频互联网广告价格
  • 网络电子商务购物网站关键词优化包含
  • 网站建设评语口碑营销怎么做
  • 帮做钓鱼网站会怎样中国新冠疫苗接种率
  • 门户网站建设方法南京网站制作
  • 什么网站可以自己接工程做预算百度在线识别图片
  • 网络网站建设电话西安seo霸屏
  • 可以做海报的网站seo推广小分享
  • 南京哪家网络公司做网站优化好长沙今日头条新闻
  • 企业解决方案ppt10页seo优化师就业前景
  • 遵义公司做网站找哪个公司好上海网络推广公司