当前位置:   article > 正文

es: java->count统计、distinct去重_java操作es查询数据如何去重

java操作es查询数据如何去重

 1 统计count

  1. GET /indexname/_count
  2. {
  3. "query": {
  4. "range": {
  5. "dataTime": {
  6. "from": "2023-01-22",
  7. "to": null,
  8. "include_lower": true,
  9. "include_upper": true,
  10. "format": "yyyy-MM-dd HH:mm:ss||yyyy||yyyy-MM-dd",
  11. "boost": 1
  12. }
  13. }
  14. }
  15. }
  16. /**
  17. * 查询指定索引文档总数(可增加查询条件,如果为空,则查询所有)
  18. */
  19. @Test
  20. public void testCount() {
  21. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  22. CountRequest countRequest = new CountRequest();
  23. //构造查询条件
  24. RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("dataTime").gte("2023-01-22").format("yyyy-MM-dd HH:mm:ss||yyyy||yyyy-MM-dd");
  25. searchSourceBuilder.query(rangeQueryBuilder);
  26. countRequest.indices("indexname").source(searchSourceBuilder);
  27. CountResponse countResponse = null;
  28. try {
  29. countResponse = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
  30. return countResponse.getCount();
  31. } catch (IOException e) {
  32. log.error("[EsClientConfig.countDocumentSize][error][fail to count document size,param is {}]", countRequest);
  33. }
  34. return 0;
  35. log.info("[document size is {}, indexName is {}]", size, indexName);
  36. }
  37. \

2. 使用agge 统计

在过滤条件后,统计总数(count), dinsticn , count(distinct())

  1. GET /indexName/_search
  2. {
  3. "size": 0,
  4. "query": {
  5. "range": {
  6. "dataTime": {
  7. "from": "2023-01-22",
  8. "to": null,
  9. "include_lower": true,
  10. "include_upper": true,
  11. "format": "yyyy-MM-dd HH:mm:ss||yyyy||yyyy-MM-dd",
  12. "boost": 1
  13. }
  14. }
  15. },
  16. "aggregations": {
  17. "objtypename": {
  18. "terms": {
  19. "field": "externalObj",
  20. "size": 100,
  21. "min_doc_count": 1,
  22. "shard_min_doc_count": 0,
  23. "show_term_doc_count_error": false,
  24. "order": [
  25. {
  26. "_count": "desc"
  27. },
  28. {
  29. "_key": "asc"
  30. }
  31. ]
  32. }
  33. },
  34. "objtypecount": {
  35. "cardinality": {
  36. "field": "externalObj"
  37. }
  38. },
  39. "total_count":{
  40. "value_count": {
  41. "field": "id.keyword"
  42. }
  43. }
  44. }
  45. }
  1. RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHostsList.toArray(new HttpHost[httpHostsList.size()])));
  2. SearchRequest searchRequest = new SearchRequest();
  3. searchRequest.indices("indexName");
  4. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  5. RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("dataTime").gte("2023-01-22").format("yyyy-MM-dd HH:mm:ss||yyyy||yyyy-MM-dd");
  6. TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("objtypename").field("externalObj").size(100);
  7. CardinalityAggregationBuilder cardinalityAggregationBuilder = AggregationBuilders.cardinality("objtypecount").field("externalObj");
  8. ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count("total_count").field("id.keyword");
  9. searchSourceBuilder.query(rangeQueryBuilder);
  10. searchSourceBuilder.aggregation(termsAggregationBuilder);
  11. searchSourceBuilder.aggregation(cardinalityAggregationBuilder);
  12. searchSourceBuilder.aggregation(valueCountAggregationBuilder);
  13. searchSourceBuilder.size(0);
  14. System.out.println("----"+searchSourceBuilder.toString());
  15. searchRequest.source(searchSourceBuilder);
  16. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  17. // 打印: objtypename,objtypecount,total_count
  18. Aggregations aggregations = searchResponse.getAggregations();
  19. for(Aggregation aggregation : aggregations.asList()){
  20. System.out.println("---"+aggregation.getName());
  21. }
  22. // distict
  23. Terms objtypenameTerms = aggregations.get("objtypename");
  24. for(Terms.Bucket bucket : objtypenameTerms.getBuckets()){
  25. System.out.println("***"+ bucket.getKeyAsString()+" : "+bucket.getDocCount() );
  26. }
  27. // count(disinct)
  28. Cardinality cardinality = aggregations.get("objtypecount");
  29. System.out.println("---"+cardinality.getValue());
  30. //count
  31. ValueCount valueCount = aggregations.get("total_count");
  32. System.out.println("^^^"+ valueCount.getValue());

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/994881
推荐阅读
相关标签
  

闽ICP备14008679号