Elasticsearch如何获取aggs下的filter的doc_count值?

huangapple go评论164阅读模式
英文:

How does Elasticsearch get the doc_count value of the filter under aggs?

问题

这是你提供的代码的翻译部分:

我在使用elasticsearch的聚合查询时遇到了一些问题以下是我使用的DSL语句

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("terms_valid_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_valid_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());
    }
}

希望这有助于你解决问题。如果你有其他问题或需要进一步的帮助,请随时提问。

英文:

I encountered some problems when using elasticsearch's aggregation query.This is the DSL statement I used

    {
      &quot;size&quot;: 0, 
      &quot;aggs&quot;: {
        &quot;terms_vlid_year&quot;:{
          &quot;terms&quot;: {
            &quot;field&quot;: &quot;valid_year&quot;,
            &quot;size&quot;: 5,
            &quot;order&quot;: {
              &quot;_key&quot;: &quot;asc&quot;
            }
          }
        },
        &quot;filterss&quot;:{
          &quot;filter&quot;: {
            &quot;range&quot;: {
              &quot;valid_year&quot;: {
                &quot;gte&quot;: 6
              }
            }
          }
        }
      }
    }

This is the aggregate result

    &quot;aggregations&quot; : {
    &quot;terms_vlid_year&quot; : {
      &quot;doc_count_error_upper_bound&quot; : 0,
      &quot;sum_other_doc_count&quot; : 13918850,
      &quot;buckets&quot; : [
        {
          &quot;key&quot; : 1,
          &quot;doc_count&quot; : 1632006
        },
        {
          &quot;key&quot; : 2,
          &quot;doc_count&quot; : 3588976
        },
        {
          &quot;key&quot; : 3,
          &quot;doc_count&quot; : 3710637
        },
        {
          &quot;key&quot; : 4,
          &quot;doc_count&quot; : 2948910
        },
        {
          &quot;key&quot; : 5,
          &quot;doc_count&quot; : 2672394
        }
      ]
    },
    &quot;filters&quot; : {
      &quot;doc_count&quot; : 13918850
    }
  }

Now I want to use java to get the doc_count of filters.Below is my java code, But the doc_count value of the filter object cannot be obtained through the filterResult.getDocCount(). How to solve this problem?

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms(&quot;terms_vlid_year&quot;);
    FilterAggregationBuilder filter = AggregationBuilders.filter(&quot;filters&quot;, QueryBuilders.rangeQuery(&quot;valid_year&quot;).gte(6));
    termsAggregationBuilder.field(&quot;valid_year&quot;);
    termsAggregationBuilder.size(5);
    termsAggregationBuilder.order(BucketOrder.key(true));
    sourceBuilder.aggregation(termsAggregationBuilder.subAggregation(filter));
    searchRequest.source(sourceBuilder);
    ReturnValue returnValue = new ReturnValue();
    LinkedList&lt;Object&gt; list = new LinkedList&lt;&gt;();
    try {
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        Terms terms = searchResponse.getAggregations().get(&quot;terms_vlid_year&quot;);
        List&lt;? extends Terms.Bucket&gt; buckets = terms.getBuckets();

        Filter filterResult = searchResponse.getAggregations().get(&quot;filters&quot;);
        long docCount1 = filterResult.getDocCount();
        System.out.println(&quot;docCount1 = &quot; + 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());
        }
    }

答案1

得分: 0

现在问题已解决。FilterAggregation 不能在 termsAggregation 内使用子聚合,它们属于同一级别。要获得所需的结果,应该这样做:

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

Now the problem has been solved.FilterAggregation cannot use subAggregation nested in termsAggregation, they belong to the same level.It should be this way to get the desired result:

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

huangapple
  • 本文由 发表于 2020年8月14日 11:00:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63405903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定