elasticsearch 7.6 获取查询结果集count

  • Post author:
  • Post category:其他


分页插件需要{count}这个参数,类似MYSQL中的select count(*) from user where userType=1,获取结果集的count数量。

JAVA有两种方式可以获得

1、将SearchSourceBuilder的“size”参数设置为0,获取结果集的“hits.total.value”得到count

2、查询语句不变,使用RestHighLevelClient.count()方法获取,创建CountRequest传入QueryBuilder(与SearchSourceBuilder使用相同)

代码:

public class FNewsImpl implements FNewsService {
    @Autowired
    ElasticService elasticService;
/**
     * 查找标题和内容
     * @param pageRequest
     * @return
     */
    @Override
    public ReturnESList getNewsES(PageRequest pageRequest)
    {
        // 创建BoolQueryBuilder
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        // 设置条件
        MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders
                .matchPhraseQuery("ftitle", pageRequest.getKeywords());
        MatchPhraseQueryBuilder matchPhraseQueryBuilder2 = QueryBuilders
                .matchPhraseQuery("fbastan", pageRequest.getKeywords());
        // 子查询“或”关系
        BoolQueryBuilder childBoolQueryBuilder = new BoolQueryBuilder()
                .should(matchPhraseQueryBuilder)
                .should(matchPhraseQueryBuilder2);
        boolQueryBuilder.must(childBoolQueryBuilder);
        // 创建SearchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 查询条件生成DSL语句
        searchSourceBuilder.query(boolQueryBuilder);
        // 从多少
        searchSourceBuilder.from(pageRequest.getPageNum());
        // 查多少条数据,如果设置“0”返回count数量
        searchSourceBuilder.size(pageRequest.getPageSize());
        // 排序规则
        searchSourceBuilder.sort("fcreatetime", SortOrder.DESC);
        // 设置超时
        TimeValue t=new TimeValue(3000);
        searchSourceBuilder.timeout(t);
        Class<?> clazz = ElasticUtil.getClazz(FquestionES.class.getName());
        //查询结果集
        List<?> data = elasticService.search(EsEnum.ES_INDEX,searchSourceBuilder,clazz);
        //查询count
        long count=elasticService.count(EsEnum.ES_INDEX,boolQueryBuilder);
        ReturnESList list=new ReturnESList();
        list.setSize(count);
        list.setData((List<FquestionES>) data);
        return list;
    }
}
@Service
public class ElasticService {
    @Autowired
    RestHighLevelClient restHighLevelClient;
    
/**
     * 根据条件查询
     * @param idxName index
     * @param builder   查询参数
     * @param c 结果类对象
     */
    public <T> List<T> search(String idxName, SearchSourceBuilder builder, Class<T> c) {
        SearchRequest request = new SearchRequest(idxName);
        request.source(builder);
        try {
            SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            SearchHit[] hits = response.getHits().getHits();
            List<T> res = new ArrayList<>(hits.length);
            for (SearchHit hit : hits) {
                res.add(JSON.parseObject(hit.getSourceAsString(), c));
            }
            return res;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询条件下共有多少条
     * @param idxName index
     * @param builder   查询参数
     */
    public long count(String idxName, QueryBuilder builder) {
        CountRequest countRequest=new CountRequest(idxName);
        countRequest.query(builder);
        try {
            CountResponse response=restHighLevelClient.count(countRequest,RequestOptions.DEFAULT);
            long length = response.getCount();
            return length;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }
}



版权声明:本文为gueizhia原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。