I am in the process of migrating from the High Level REST API to the Java API. Although it is going ok despite not having many examples in the docs, there is one problem I can't figure out.
When you send a search request that contains a filter aggregation that contains one or more sub aggregations, I don't know how to access those sub aggregations from the search response.
The search response object gives access to the top level aggregations (Aggregate objects). I can also access the FilterAggregate objects with the filter() method, but those only allow me to get the docCount() from the response. I would expect an aggregations() method that returns Map<String,Aggregate>, in order to access the Aggregate objects under the filter. In the HLRC this used to be possible.
@RabBit_BR , thanks for your reply. For StringTerm aggregations, it also works for me. Just not for Filter aggregations. Your code snippet looks like it's using the High Level Rest API (e.g. with getAggregations() instead of aggregations()), but for me the problem only happens for the Java API. Here is a code snippet, using your example:
co.elastic.clients.elasticsearch.core.SearchResponse<HashMap> searchResponse = doSearchCall();
Aggregate aggregate = searchResponse.aggregations().get("root-aggs"); //This works
if(aggregate.isFilter()) {
FilterAggregate filterAggregate = aggregate.filter();
Aggregate subAggregate = filterAggregate.aggregations().get("key-sub-aggs"); //This does not work.
}
var mapResult = new HashMap<String, Long>();
for (var rootBucket : aggs.get("root-aggs").sterms().buckets().array()){
var subAggsBucket = rootBucket.aggregations().get("key-sub-aggs").sterms().buckets().array();
for (var subBucket : subAggsBucket) {
mapResult.put(subBucket.key(), subBucket.docCount());
}
}
So for String Term Aggregations I can indeed access the buckets and their sub aggregations via the Java API. It's different however for Filter Aggregations.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.