OpenSearch/docs/java-api/aggregations/bucket/terms-aggregation.asciidoc
qwerty4030 e7d352b489 Compound order for histogram aggregations. (#22343)
This commit adds support for histogram and date_histogram agg compound order by refactoring and reusing terms agg order code. The major change is that the Terms.Order and Histogram.Order classes have been replaced/refactored into a new class BucketOrder. This is a breaking change for the Java Transport API. For backward compatibility with previous ES versions the (date)histogram compound order will use the first order. Also the _term and _time aggregation order keys have been deprecated; replaced by _key.

Relates to #20003: now that all these aggregations use the same order code, it should be easier to move validation to parse time (as a follow up PR).

Relates to #14771: histogram and date_histogram aggregation order will now be validated at reduce time.

Closes #23613: if a single BucketOrder that is not a tie-breaker is added with the Java Transport API, it will be converted into a CompoundOrder with a tie-breaker.
2017-05-11 18:06:26 +01:00

98 lines
2.7 KiB
Plaintext

[[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
}
--------------------------------------------------
===== Order
Import bucket ordering strategy classes:
[source,java]
--------------------------------------------------
import org.elasticsearch.search.aggregations.BucketOrder;
--------------------------------------------------
Ordering the buckets by their `doc_count` in an ascending manner:
[source,java]
--------------------------------------------------
AggregationBuilders
.terms("genders")
.field("gender")
.order(BucketOrder.count(true))
--------------------------------------------------
Ordering the buckets alphabetically by their terms in an ascending manner:
[source,java]
--------------------------------------------------
AggregationBuilders
.terms("genders")
.field("gender")
.order(BucketOrder.key(true))
--------------------------------------------------
Ordering the buckets by single value metrics sub-aggregation (identified by the aggregation name):
[source,java]
--------------------------------------------------
AggregationBuilders
.terms("genders")
.field("gender")
.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
.subAggregation(
AggregationBuilders.avg("avg_height").field("height")
)
--------------------------------------------------