ES的分页查询测试
@Autowired
private RestHighLevelClient elasticsearchClient;
@Test
void test04() {
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
commonSearch(matchAllQueryBuilder);
}
public void commonSearch(QueryBuilder queryBuilder) {
SearchRequest searchRequest = new SearchRequest("book");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.fetchSource(new String[]{"title", "author"}, new String[]{"id"});
searchSourceBuilder.sort("author", SortOrder.DESC);
searchSourceBuilder.from(2).size(2);
searchRequest.source(searchSourceBuilder);
try {
SearchResponse search = elasticsearchClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = search.getHits().getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
} catch (IOException e) {
e.printStackTrace();
}
}