elastic search and query的 Spring 代码实现-CSDN博客
看代码
@Test
public void matchAllSearchTest() throws IOException {
// 分页数据前端传递
int pageNo = 1, pageSize = 20;
SearchRequest sr = new SearchRequest("goods");
sr.source().query(QueryBuilders.matchAllQuery());
sr.source()
.from((pageNo - 1) * pageSize)
.size(10);
sr.source()
.sort("salers", SortOrder.DESC)
.sort("price", SortOrder.ASC);
SearchResponse result = esClient.search(sr, RequestOptions.DEFAULT);
System.out.println(result);
// 解析结果
// 解析真实的 Data 集
SearchHits hits = result.getHits();
SearchHit[] dataSetHits = hits.getHits();
// 获取总条数
long total = hits.getTotalHits().value;
TotalHits.Relation relation = hits.getTotalHits().relation;
System.out.println(total);
// 得到 source 的「value」
for (SearchHit hit : dataSetHits) {
String resultSourceStr = hit.getSourceAsString();
System.out.println(resultSourceStr);
// 转化成 GoodsPO 对象 , 返回前端
GoodsPO goodsPO = JSONUtil.toBean(resultSourceStr, GoodsPO.class);
}
}