SpringBoot + Elasticsearch:分页查询
@Test
public void searchStuDoc() {
// 第一个参数是页数page,第二个参数是每页数据数量pageSize
Pageable pageable = PageRequest.of(0, 10);
SearchQuery query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("description", "save man"))
.withPageable(pageable)
.build();
AggregatedPage<Stu> pagedStu = esTemplate.queryForPage(query, Stu.class);
System.out.println("检索后的总分页数目为:" + pagedStu.getTotalPages());
List<Stu> stuList = pagedStu.getContent();
for (Stu s : stuList) {
System.out.println(s);
}
}
因为只有4条测试数据,所以1页会全部展示,打印结果如下:
然后修改查询数量
Pageable pageable = PageRequest.of(0, 2);
会变成2页,每页2条数据
还没有评论,来说两句吧...