2014-05-15 06:36:05 -04:00
|
|
|
[[java-aggs-bucket-terms]]
|
|
|
|
==== Terms Aggregation
|
|
|
|
|
|
|
|
Here is how you can use
|
|
|
|
{ref}/search-aggregations-bucket-terms-aggregation.html[Terms Aggregation]
|
|
|
|
with Java API.
|
|
|
|
|
|
|
|
|
|
|
|
===== Prepare aggregation request
|
|
|
|
|
|
|
|
Here is an example on how to create the aggregation request:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
AggregationBuilders
|
|
|
|
.terms("genders")
|
|
|
|
.field("gender");
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
===== Use aggregation response
|
|
|
|
|
|
|
|
Import Aggregation definition classes:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
// sr is here your SearchResponse object
|
|
|
|
Terms genders = sr.getAggregations().get("genders");
|
|
|
|
|
|
|
|
// For each entry
|
|
|
|
for (Terms.Bucket entry : genders.getBuckets()) {
|
|
|
|
entry.getKey(); // Term
|
|
|
|
entry.getDocCount(); // Doc count
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2017-05-11 13:06:26 -04:00
|
|
|
===== Order
|
|
|
|
|
|
|
|
Import bucket ordering strategy classes:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
import org.elasticsearch.search.aggregations.BucketOrder;
|
|
|
|
--------------------------------------------------
|
2014-05-15 06:36:05 -04:00
|
|
|
|
|
|
|
Ordering the buckets by their `doc_count` in an ascending manner:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
AggregationBuilders
|
|
|
|
.terms("genders")
|
|
|
|
.field("gender")
|
2017-05-11 13:06:26 -04:00
|
|
|
.order(BucketOrder.count(true))
|
2014-05-15 06:36:05 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
Ordering the buckets alphabetically by their terms in an ascending manner:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
AggregationBuilders
|
|
|
|
.terms("genders")
|
|
|
|
.field("gender")
|
2017-05-11 13:06:26 -04:00
|
|
|
.order(BucketOrder.key(true))
|
2014-05-15 06:36:05 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
Ordering the buckets by single value metrics sub-aggregation (identified by the aggregation name):
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
AggregationBuilders
|
|
|
|
.terms("genders")
|
|
|
|
.field("gender")
|
2017-05-11 13:06:26 -04:00
|
|
|
.order(BucketOrder.aggregation("avg_height", false))
|
|
|
|
.subAggregation(
|
|
|
|
AggregationBuilders.avg("avg_height").field("height")
|
|
|
|
)
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
Ordering the buckets by multiple criteria:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
AggregationBuilders
|
|
|
|
.terms("genders")
|
|
|
|
.field("gender")
|
|
|
|
.order(BucketOrder.compound( // in order of priority:
|
|
|
|
BucketOrder.aggregation("avg_height", false), // sort by sub-aggregation first
|
|
|
|
BucketOrder.count(true))) // then bucket count as a tie-breaker
|
2014-05-15 06:36:05 -04:00
|
|
|
.subAggregation(
|
|
|
|
AggregationBuilders.avg("avg_height").field("height")
|
|
|
|
)
|
|
|
|
--------------------------------------------------
|