赞
踩
- GET /indexname/_count
- {
- "query": {
- "range": {
- "dataTime": {
- "from": "2023-01-22",
- "to": null,
- "include_lower": true,
- "include_upper": true,
- "format": "yyyy-MM-dd HH:mm:ss||yyyy||yyyy-MM-dd",
- "boost": 1
- }
- }
- }
- }
-
-
- /**
- * 查询指定索引文档总数(可增加查询条件,如果为空,则查询所有)
- */
- @Test
- public void testCount() {
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- CountRequest countRequest = new CountRequest();
- //构造查询条件
- RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("dataTime").gte("2023-01-22").format("yyyy-MM-dd HH:mm:ss||yyyy||yyyy-MM-dd");
- searchSourceBuilder.query(rangeQueryBuilder);
-
- 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);
- }
- \
在过滤条件后,统计总数(count), dinsticn , count(distinct())
- GET /indexName/_search
- {
- "size": 0,
- "query": {
- "range": {
- "dataTime": {
- "from": "2023-01-22",
- "to": null,
- "include_lower": true,
- "include_upper": true,
- "format": "yyyy-MM-dd HH:mm:ss||yyyy||yyyy-MM-dd",
- "boost": 1
- }
- }
- },
- "aggregations": {
- "objtypename": {
- "terms": {
- "field": "externalObj",
- "size": 100,
- "min_doc_count": 1,
- "shard_min_doc_count": 0,
- "show_term_doc_count_error": false,
- "order": [
- {
- "_count": "desc"
- },
- {
- "_key": "asc"
- }
- ]
- }
- },
- "objtypecount": {
- "cardinality": {
- "field": "externalObj"
- }
- },
- "total_count":{
- "value_count": {
- "field": "id.keyword"
- }
- }
- }
- }
- RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHostsList.toArray(new HttpHost[httpHostsList.size()])));
- SearchRequest searchRequest = new SearchRequest();
- searchRequest.indices("indexName");
-
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("dataTime").gte("2023-01-22").format("yyyy-MM-dd HH:mm:ss||yyyy||yyyy-MM-dd");
- TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("objtypename").field("externalObj").size(100);
- CardinalityAggregationBuilder cardinalityAggregationBuilder = AggregationBuilders.cardinality("objtypecount").field("externalObj");
- ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count("total_count").field("id.keyword");
-
- searchSourceBuilder.query(rangeQueryBuilder);
- searchSourceBuilder.aggregation(termsAggregationBuilder);
- searchSourceBuilder.aggregation(cardinalityAggregationBuilder);
- searchSourceBuilder.aggregation(valueCountAggregationBuilder);
-
- searchSourceBuilder.size(0);
- System.out.println("----"+searchSourceBuilder.toString());
- searchRequest.source(searchSourceBuilder);
- SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
- // 打印: objtypename,objtypecount,total_count
- Aggregations aggregations = searchResponse.getAggregations();
- for(Aggregation aggregation : aggregations.asList()){
- System.out.println("---"+aggregation.getName());
- }
- // distict
- Terms objtypenameTerms = aggregations.get("objtypename");
- for(Terms.Bucket bucket : objtypenameTerms.getBuckets()){
- System.out.println("***"+ bucket.getKeyAsString()+" : "+bucket.getDocCount() );
- }
-
- // count(disinct)
- Cardinality cardinality = aggregations.get("objtypecount");
- System.out.println("---"+cardinality.getValue());
-
- //count
- ValueCount valueCount = aggregations.get("total_count");
- System.out.println("^^^"+ valueCount.getValue());
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。