Blog excerpt "ES solution can only query 10,000 data solutions" April 11, 2023

1. Count calculation

In the project, count is also a relatively common method. It was needed in previous projects, all of which were based on querying all the methods, directly taking the value of total and returning it. It was recently discovered that Elasticsearch actually encapsulates a CountRequest class to obtain the quantity. code show as below:
 

/**
 * 查询指定索引文档总数(可增加查询条件,如果为空,则查询所有)
 */
@Test
public void testCount() {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    CountRequest countRequest = new CountRequest();
    //构造查询条件
//        searchSourceBuilder.query(QueryBuilders.termQuery("fieldName", 1));
    countRequest.indices("indexName").source(searchSourceBuilder);
    CountResponse countResponse = null;
    try {
        countResponse = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
        return countResponse.getCount();
    } catch (IOException e) {
        log.error("[EsClientConfig.countDocumentSize][error][fail to count document size,param is {}]", countRequest);
    }
    return 0;
    log.info("[document size is {}, indexName is {}]", size, indexName);
}

Guess you like

Origin blog.csdn.net/m0_46580493/article/details/130076576