我在使用Elasticsearch的聚合查询时遇到了一些问题。这是我使用的DSL语句

    {
      "size": 0,
      "aggs": {
        "terms_vlid_year":{
          "terms": {
            "field": "valid_year",
            "size": 5,
            "order": {
              "_key": "asc"
            }
          }
        },
        "filterss":{
          "filter": {
            "range": {
              "valid_year": {
                "gte": 6
              }
            }
          }
        }
      }
    }
这是合计结果
    "aggregations" : {
    "terms_vlid_year" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 13918850,
      "buckets" : [
        {
          "key" : 1,
          "doc_count" : 1632006
        },
        {
          "key" : 2,
          "doc_count" : 3588976
        },
        {
          "key" : 3,
          "doc_count" : 3710637
        },
        {
          "key" : 4,
          "doc_count" : 2948910
        },
        {
          "key" : 5,
          "doc_count" : 2672394
        }
      ]
    },
    "filters" : {
      "doc_count" : 13918850
    }
  }
现在我想使用Java获取过滤器的doc_count。下面是我的Java代码,但是无法通过filterResult.getDocCount()获得过滤器对象的doc_count值。如何解决这个问题呢?
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("terms_vlid_year");
    FilterAggregationBuilder filter = AggregationBuilders.filter("filters", QueryBuilders.rangeQuery("valid_year").gte(6));
    termsAggregationBuilder.field("valid_year");
    termsAggregationBuilder.size(5);
    termsAggregationBuilder.order(BucketOrder.key(true));
    sourceBuilder.aggregation(termsAggregationBuilder.subAggregation(filter));
    searchRequest.source(sourceBuilder);
    ReturnValue returnValue = new ReturnValue();
    LinkedList<Object> list = new LinkedList<>();
    try {
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        Terms terms = searchResponse.getAggregations().get("terms_vlid_year");
        List<? extends Terms.Bucket> buckets = terms.getBuckets();

        Filter filterResult = searchResponse.getAggregations().get("filters");
        long docCount1 = filterResult.getDocCount();
        System.out.println("docCount1 = " + docCount1);

        for (Terms.Bucket bucket : buckets) {
            String keyAsString = bucket.getKeyAsString();
            long docCount = bucket.getDocCount();
            returnValue.setFieldName(keyAsString);
            returnValue.setDocValue(docCount);
            System.out.println(returnValue.toString());
        }
    }

最佳答案

现在问题已解决,FilterAggregation不能使用嵌套在termAggregation中的subAggregation,它们属于同一级别,应该通过这种方式才能获得所需的结果:

sourceBuilder.aggregation(termsAggregationBuilder);
FilterAggregationBuilder filter = AggregationBuilders.filter("filters", QueryBuilders.rangeQuery("valid_year").gte(6));
sourceBuilder.aggregation(filter);

关于java - Elasticsearch如何在aggs下获取过滤器的doc_count值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63405903/

10-11 07:21