use of com.github.lybgeek.common.model.PageResult in project springboot-learning by lyb-geek.
the class ElasticsearchHelper method pageSearch.
/**
* 分页搜索
*
* @param indexName 索引名称
* @param builder 查询参数
* @param searchTargetClz 结果类对象
*/
public <T> PageResult<T> pageSearch(String indexName, SearchSourceBuilder builder, Class<T> searchTargetClz) {
SearchRequest request = new SearchRequest(indexName);
request.source(builder);
PageResult<T> pageResult = new PageResult<>();
try {
SearchResponse response = highLevelClient.search(request, RequestOptions.DEFAULT);
SearchHit[] hits = response.getHits().getHits();
if (ArrayUtils.isNotEmpty(hits)) {
List<T> res = new ArrayList<>(hits.length);
for (SearchHit hit : hits) {
res.add(JSON.parseObject(hit.getSourceAsString(), searchTargetClz));
}
int pageSize = builder.size();
int pageNo = builder.from() / pageSize + 1;
long total = response.getHits().getTotalHits().value;
Long totalPage = (total + pageSize - 1) / pageSize;
pageResult.setTotal(total);
pageResult.setPageNo(pageNo);
pageResult.setPageSize(pageSize);
pageResult.setTotalPages(Integer.valueOf(totalPage.toString()));
pageResult.setList(res);
}
return pageResult;
} catch (Exception e) {
log.error("pageSearch error:" + e.getMessage(), e);
}
return null;
}
use of com.github.lybgeek.common.model.PageResult in project springboot-learning by lyb-geek.
the class ElasticsearchAspect method pageQuery.
private Object pageQuery(PageQuery pageQuery, String indexName) {
int pageNo = ObjectUtils.isNotEmpty(pageQuery.getPageNo()) && pageQuery.getPageNo() > 0 ? pageQuery.getPageNo() : 1;
int pageSize = ObjectUtils.isNotEmpty(pageQuery.getPageSize()) ? pageQuery.getPageSize() : 10;
int from = (pageNo - 1) * pageSize;
Object queryParams = pageQuery.getQueryParams();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
if (ObjectUtils.isNotEmpty(queryParams)) {
searchSourceBuilder = getSearchSouceBuilder(queryParams);
}
if (ObjectUtils.isNotEmpty(searchSourceBuilder)) {
searchSourceBuilder.from(from).size(pageSize);
PageResult pageResult = elasticsearchHelper.pageSearch(indexName, searchSourceBuilder, queryParams.getClass());
if (CollectionUtils.isEmpty(pageResult.getList())) {
return null;
}
}
return null;
}
Aggregations