ES java 根据条件查询总数

ES java 根据条件查询总数,第1张

public long countSearch(String index,String fuzzySearchStr) throws IOException {
        CountRequest countRequest = new CountRequest(index);
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        //等于条件
        boolQueryBuilder.must(QueryBuilders.termsQuery("字段" +  ".keyword", "字段值"));

        //不等于条件
        boolQueryBuilder.mustNot(QueryBuilders.termsQuery("字段" +  ".keyword", "字段值"));

        //时间范围查询
        boolQueryBuilder.must(QueryBuilders.rangeQuery("字段"+ ".keyword").from("开始时间值"));
        boolQueryBuilder.must(QueryBuilders.rangeQuery("字段"+ ".keyword").to("结束时间值"));
       

        //全局模糊查询
        if (StringUtils.isNotBlank(fuzzySearchStr)) {
            boolQueryBuilder.must(QueryBuilders.queryStringQuery("*"+fuzzySearchStr+"*"));
        }

        builder.query(boolQueryBuilder);
        countRequest.source(builder);
        CountResponse countResponse = client.count(countRequest, RequestOptions.DEFAULT);
        long count = countResponse.getCount();
        return count;
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/924674.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存