Search in sources :

Example 1 with PageResult

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;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) PageResult(com.github.lybgeek.common.model.PageResult) IOException(java.io.IOException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with PageResult

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;
}
Also used : JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) PageResult(com.github.lybgeek.common.model.PageResult) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Aggregations

PageResult (com.github.lybgeek.common.model.PageResult)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 JoinPoint (org.aspectj.lang.JoinPoint)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1