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();
}