赞
踩
目录
1.from + size
from表示从第几行开始,size表示查询多少条文档。from默认为0,size默认为10,最灵活的分页方式。
2.scroll
不适合用来做实时搜索,而更适用于后台批处理任务,如日志导出。暂存搜索结果,每次传入scroll_id。scroll_id会占用大量资源,用于非实时处理大量数据的情况。可以通过scroll 初始化查询后,指定scroll_id 结合from+size的方式来实现分页。
3. search_after
根据上一页的最后一条数据来确定下一页的位置。需要使用一个唯一值的字段作为排序字段。不能自由跳到一个随机页面。要想实现翻页,需要每次记录最后查询的sort。实现方式比较麻烦,需要去记录上一次查询的排序字段的值用于下一页分页查询。
此处采用第一种from + size方式来实现类似于mysql的like模糊查询。
- <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
- </dependency>
-
- <!-- elasticsearch-rest-high-level-client -->
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>elasticsearch-rest-high-level-client</artifactId>
- </dependency>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。