ElasticSearch查询满足条件的总数

1.采用的是RestHighLevelClient客户端连接,对应的pom文件如下:

<dependency>
	<groupId>org.elasticsearch</groupId>
	<artifactId>elasticsearch</artifactId>
	<version>7.5.0</version>
</dependency>
<dependency>
	<groupId>org.elasticsearch.client</groupId>
	<artifactId>elasticsearch-rest-client</artifactId>
	<version>7.5.0</version>
</dependency>

版本采用和你ES相同的版本
2.代码

public void count() throws IOException{ 
//创建连接
	RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("your ip", 9200, "http")));
//构建判断条件
	BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
    MatchQueryBuilder matchQueryBuilder1 = QueryBuilders.matchQuery("text", "123");
    RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("time");
    //起始时间
    rangeQueryBuilder.gte("2020-04-01T00:00:00+08:00");
    //结束时间
    rangeQueryBuilder.lte("2020-04-30T00:00:00+08:00");
    boolBuilder.must(matchQueryBuilder1).must(rangeQueryBuilder);
	
	CountRequest countRequest = new CountRequest();
    countRequest.indices("your index");
    countRequest.query(boolBuilder);
    CountResponse countResponse = client.count(countRequest,RequestOptions.DEFAULT);
    long count = countResponse.getCount();
}
    原文作者:CHSXS
    原文地址: https://blog.csdn.net/CHSXS/article/details/105974162
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞