2014-05-15 06:36:05 -04:00
|
|
|
[[java-aggs-bucket-reverse-nested]]
|
|
|
|
==== Reverse Nested Aggregation
|
|
|
|
|
|
|
|
Here is how you can use
|
|
|
|
{ref}/search-aggregations-bucket-reverse-nested-aggregation.html[Reverse Nested Aggregation]
|
|
|
|
with Java API.
|
|
|
|
|
|
|
|
|
|
|
|
===== Prepare aggregation request
|
|
|
|
|
|
|
|
Here is an example on how to create the aggregation request:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
AggregationBuilder aggregation =
|
|
|
|
AggregationBuilders
|
2016-11-28 11:33:40 -05:00
|
|
|
.nested("agg", "resellers")
|
2014-05-15 06:36:05 -04:00
|
|
|
.subAggregation(
|
|
|
|
AggregationBuilders
|
|
|
|
.terms("name").field("resellers.name")
|
|
|
|
.subAggregation(
|
|
|
|
AggregationBuilders
|
|
|
|
.reverseNested("reseller_to_product")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
===== Use aggregation response
|
|
|
|
|
|
|
|
Import Aggregation definition classes:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
import org.elasticsearch.search.aggregations.bucket.nested.Nested;
|
|
|
|
import org.elasticsearch.search.aggregations.bucket.nested.ReverseNested;
|
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
// sr is here your SearchResponse object
|
|
|
|
Nested agg = sr.getAggregations().get("agg");
|
|
|
|
Terms name = agg.getAggregations().get("name");
|
|
|
|
for (Terms.Bucket bucket : name.getBuckets()) {
|
|
|
|
ReverseNested resellerToProduct = bucket.getAggregations().get("reseller_to_product");
|
|
|
|
resellerToProduct.getDocCount(); // Doc count
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|