OpenSearch/docs/java-api/aggregations/bucket/histogram-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

49 lines
1.3 KiB
Plaintext

[[java-aggs-bucket-histogram]]
==== Histogram Aggregation
Here is how you can use
{ref}/search-aggregations-bucket-histogram-aggregation.html[Histogram Aggregation]
with Java API.
===== Prepare aggregation request
Here is an example on how to create the aggregation request:
[source,java]
--------------------------------------------------
AggregationBuilder aggregation =
AggregationBuilders
.histogram("agg")
.field("height")
.interval(1);
--------------------------------------------------
===== Use aggregation response
Import Aggregation definition classes:
[source,java]
--------------------------------------------------
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
--------------------------------------------------
[source,java]
--------------------------------------------------
// sr is here your SearchResponse object
Histogram agg = sr.getAggregations().get("agg");
// For each entry
for (Histogram.Bucket entry : agg.getBuckets()) {
Number key = (Number) entry.getKey(); // Key
long docCount = entry.getDocCount(); // Doc count
logger.info("key [{}], doc_count [{}]", key, docCount);
}
--------------------------------------------------
===== Order
Supports the same order functionality as the <<java-aggs-bucket-terms,`Terms Aggregation`>>.