英文:
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
{
"size": 0,
"aggs": {
"terms_vlid_year":{
"terms": {
"field": "valid_year",
"size": 5,
"order": {
"_key": "asc"
}
}
},
"filterss":{
"filter": {
"range": {
"valid_year": {
"gte": 6
}
}
}
}
}
}
This is the aggregate result
"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
}
}
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("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());
}
}
答案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("filters", QueryBuilders.rangeQuery("valid_year").gte(6));
sourceBuilder.aggregation(filter);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论