51 lines
1.6 KiB
Plaintext
51 lines
1.6 KiB
Plaintext
[[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
|
|
.nested("agg").path("resellers")
|
|
.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
|
|
}
|
|
--------------------------------------------------
|
|
|