Add java documentation for aggregations
Buckets: -------- * terms * range * global * filter * filters * missing * nested * reverse nested * children * significant terms * date range * ip range * range * histogram * date histogram * geo distance * geo hash grid Metrics: -------- * min * max * sum * avg * stats * extended stats * value count * percentiles * percentile rank * cardinality * geo bounds * top hits * scripted metric
This commit is contained in:
parent
ef8802d878
commit
d7d937300a
|
@ -0,0 +1,33 @@
|
||||||
|
[[java-aggregations-bucket]]
|
||||||
|
|
||||||
|
include::bucket/global-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/filter-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/filters-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/missing-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/nested-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/reverse-nested-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/children-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/terms-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/significantterms-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/range-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/daterange-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/iprange-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/histogram-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/datehistogram-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/geodistance-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::bucket/geohashgrid-aggregation.asciidoc[]
|
|
@ -0,0 +1,37 @@
|
||||||
|
[[java-aggs-bucket-children]]
|
||||||
|
==== Children Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-children-aggregation.html[Children Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.children("agg")
|
||||||
|
.childType("reseller");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.children.Children;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Children agg = sr.getAggregations().get("agg");
|
||||||
|
agg.getDocCount(); // Doc count
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
[[java-aggs-bucket-datehistogram]]
|
||||||
|
==== Date Histogram Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-datehistogram-aggregation.html[Date Histogram Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.dateHistogram("agg")
|
||||||
|
.field("dateOfBirth")
|
||||||
|
.interval(DateHistogram.Interval.YEAR);
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
Or if you want to set an interval of 10 days:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.dateHistogram("agg")
|
||||||
|
.field("dateOfBirth")
|
||||||
|
.interval(DateHistogram.Interval.days(10));
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== 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
|
||||||
|
DateHistogram agg = sr.getAggregations().get("agg");
|
||||||
|
|
||||||
|
// For each entry
|
||||||
|
for (DateHistogram.Bucket entry : agg.getBuckets()) {
|
||||||
|
String key = entry.getKey(); // Key
|
||||||
|
DateTime keyAsDate = entry.getKeyAsDate(); // Key as date
|
||||||
|
long docCount = entry.getDocCount(); // Doc count
|
||||||
|
|
||||||
|
logger.info("key [{}], date [{}], doc_count [{}]", key, keyAsDate.getYear(), docCount);
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
This will basically produce for the first example:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
key [1942-01-01T00:00:00.000Z], date [1942], doc_count [1]
|
||||||
|
key [1945-01-01T00:00:00.000Z], date [1945], doc_count [1]
|
||||||
|
key [1946-01-01T00:00:00.000Z], date [1946], doc_count [1]
|
||||||
|
...
|
||||||
|
key [2005-01-01T00:00:00.000Z], date [2005], doc_count [1]
|
||||||
|
key [2007-01-01T00:00:00.000Z], date [2007], doc_count [2]
|
||||||
|
key [2008-01-01T00:00:00.000Z], date [2008], doc_count [3]
|
||||||
|
--------------------------------------------------
|
|
@ -0,0 +1,59 @@
|
||||||
|
[[java-aggs-bucket-daterange]]
|
||||||
|
==== Date Range Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-daterange-aggregation.html[Date Range Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.dateRange("agg")
|
||||||
|
.field("dateOfBirth")
|
||||||
|
.format("yyyy")
|
||||||
|
.addUnboundedTo("1950") // from -infinity to 1950 (excluded)
|
||||||
|
.addRange("1950", "1960") // from 1950 to 1960 (excluded)
|
||||||
|
.addUnboundedFrom("1960"); // from 1960 to +infinity
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.range.date.DateRange;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
DateRange agg = sr.getAggregations().get("agg");
|
||||||
|
|
||||||
|
// For each entry
|
||||||
|
for (DateRange.Bucket entry : agg.getBuckets()) {
|
||||||
|
String key = entry.getKey(); // Date range as key
|
||||||
|
DateTime fromAsDate = entry.getFromAsDate(); // Date bucket from as a Date
|
||||||
|
DateTime toAsDate = entry.getToAsDate(); // Date bucket to as a Date
|
||||||
|
long docCount = entry.getDocCount(); // Doc count
|
||||||
|
|
||||||
|
logger.info("key [{}], from [{}], to [{}], doc_count [{}]", key, fromAsDate, toAsDate, docCount);
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
This will basically produce:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
key [*-1950], from [null], to [1950-01-01T00:00:00.000Z], doc_count [8]
|
||||||
|
key [1950-1960], from [1950-01-01T00:00:00.000Z], to [1960-01-01T00:00:00.000Z], doc_count [5]
|
||||||
|
key [1960-*], from [1960-01-01T00:00:00.000Z], to [null], doc_count [37]
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
[[java-aggs-bucket-filter]]
|
||||||
|
==== Filter Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-filter-aggregation.html[Filter Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilders
|
||||||
|
.filter("agg")
|
||||||
|
.filter(FilterBuilders.termFilter("gender", "male"));
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Filter agg = sr.getAggregations().get("agg");
|
||||||
|
agg.getDocCount(); // Doc count
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
[[java-aggs-bucket-filters]]
|
||||||
|
==== Filters Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-filters-aggregation.html[Filters Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.filters("agg")
|
||||||
|
.filter("men", FilterBuilders.termFilter("gender", "male"))
|
||||||
|
.filter("women", FilterBuilders.termFilter("gender", "female"));
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.filters.Filters;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Filters agg = sr.getAggregations().get("agg");
|
||||||
|
|
||||||
|
// For each entry
|
||||||
|
for (Filters.Bucket entry : agg.getBuckets()) {
|
||||||
|
String key = entry.getKey(); // bucket key
|
||||||
|
long docCount = entry.getDocCount(); // Doc count
|
||||||
|
logger.info("key [{}], doc_count [{}]", key, docCount);
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
This will basically produce:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
key [men], doc_count [4982]
|
||||||
|
key [women], doc_count [5018]
|
||||||
|
--------------------------------------------------
|
|
@ -0,0 +1,60 @@
|
||||||
|
[[java-aggs-bucket-geodistance]]
|
||||||
|
==== Geo Distance Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-geodistance-aggregation.html[Geo Distance Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.geoDistance("agg")
|
||||||
|
.field("address.location")
|
||||||
|
.point(new GeoPoint(48.84237171118314,2.33320027692004))
|
||||||
|
.unit(DistanceUnit.KILOMETERS)
|
||||||
|
.addUnboundedTo(3.0)
|
||||||
|
.addRange(3.0, 10.0)
|
||||||
|
.addRange(10.0, 500.0);
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.range.geodistance.GeoDistance;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
GeoDistance agg = sr.getAggregations().get("agg");
|
||||||
|
|
||||||
|
// For each entry
|
||||||
|
for (GeoDistance.Bucket entry : agg.getBuckets()) {
|
||||||
|
String key = entry.getKey(); // key as String
|
||||||
|
Number from = entry.getFrom(); // bucket from value
|
||||||
|
Number to = entry.getTo(); // bucket to value
|
||||||
|
long docCount = entry.getDocCount(); // Doc count
|
||||||
|
|
||||||
|
logger.info("key [{}], from [{}], to [{}], doc_count [{}]", key, from, to, docCount);
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
This will basically produce:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
key [*-3.0], from [0.0], to [3.0], doc_count [161]
|
||||||
|
key [3.0-10.0], from [3.0], to [10.0], doc_count [460]
|
||||||
|
key [10.0-500.0], from [10.0], to [500.0], doc_count [4925]
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
[[java-aggs-bucket-geohashgrid]]
|
||||||
|
==== Geo Hash Grid Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-geohashgrid-aggregation.html[Geo Hash Grid Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.geohashGrid("agg")
|
||||||
|
.field("address.location")
|
||||||
|
.precision(4);
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
GeoHashGrid agg = sr.getAggregations().get("agg");
|
||||||
|
|
||||||
|
// For each entry
|
||||||
|
for (GeoHashGrid.Bucket entry : agg.getBuckets()) {
|
||||||
|
String key = entry.getKey(); // key as String
|
||||||
|
GeoPoint keyAsGeoPoint = entry.getKeyAsGeoPoint(); // key as geo point
|
||||||
|
long docCount = entry.getDocCount(); // Doc count
|
||||||
|
|
||||||
|
logger.info("key [{}], point {}, doc_count [{}]", key, keyAsGeoPoint, docCount);
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
This will basically produce:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
key [gbqu], point [47.197265625, -1.58203125], doc_count [1282]
|
||||||
|
key [gbvn], point [50.361328125, -4.04296875], doc_count [1248]
|
||||||
|
key [u1j0], point [50.712890625, 7.20703125], doc_count [1156]
|
||||||
|
key [u0j2], point [45.087890625, 7.55859375], doc_count [1138]
|
||||||
|
...
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
[[java-aggs-bucket-global]]
|
||||||
|
==== Global Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-global-aggregation.html[Global Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilders
|
||||||
|
.global("agg")
|
||||||
|
.subAggregation(AggregationBuilders.terms("genders").field("gender"));
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.global.Global;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Global agg = sr.getAggregations().get("agg");
|
||||||
|
agg.getDocCount(); // Doc count
|
||||||
|
--------------------------------------------------
|
|
@ -0,0 +1,42 @@
|
||||||
|
[[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()) {
|
||||||
|
String key = entry.getKey(); // Key
|
||||||
|
long docCount = entry.getDocCount(); // Doc count
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
|
@ -0,0 +1,79 @@
|
||||||
|
[[java-aggs-bucket-iprange]]
|
||||||
|
==== Ip Range Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-iprange-aggregation.html[Ip Range Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.ipRange("agg")
|
||||||
|
.field("ip")
|
||||||
|
.addUnboundedTo("192.168.1.0") // from -infinity to 192.168.1.0 (excluded)
|
||||||
|
.addRange("192.168.1.0", "192.168.2.0") // from 192.168.1.0 to 192.168.2.0 (excluded)
|
||||||
|
.addUnboundedFrom("192.168.2.0"); // from 192.168.2.0 to +infinity
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
Note that you could also use ip masks as ranges:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.ipRange("agg")
|
||||||
|
.field("ip")
|
||||||
|
.addMaskRange("192.168.0.0/32")
|
||||||
|
.addMaskRange("192.168.0.0/24")
|
||||||
|
.addMaskRange("192.168.0.0/16");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.range.ipv4.IPv4Range;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
IPv4Range agg = sr.getAggregations().get("agg");
|
||||||
|
|
||||||
|
// For each entry
|
||||||
|
for (IPv4Range.Bucket entry : agg.getBuckets()) {
|
||||||
|
String key = entry.getKey(); // Ip range as key
|
||||||
|
String fromAsString = entry.getFromAsString(); // Ip bucket from as a String
|
||||||
|
String toAsString = entry.getToAsString(); // Ip bucket to as a String
|
||||||
|
long docCount = entry.getDocCount(); // Doc count
|
||||||
|
|
||||||
|
logger.info("key [{}], from [{}], to [{}], doc_count [{}]", key, fromAsString, toAsString, docCount);
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
This will basically produce for the first example:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
key [*-192.168.1.0], from [null], to [192.168.1.0], doc_count [13]
|
||||||
|
key [192.168.1.0-192.168.2.0], from [192.168.1.0], to [192.168.2.0], doc_count [14]
|
||||||
|
key [192.168.2.0-*], from [192.168.2.0], to [null], doc_count [23]
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
And for the second one (using Ip masks):
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
key [192.168.0.0/32], from [192.168.0.0], to [192.168.0.1], doc_count [0]
|
||||||
|
key [192.168.0.0/24], from [192.168.0.0], to [192.168.1.0], doc_count [13]
|
||||||
|
key [192.168.0.0/16], from [192.168.0.0], to [192.169.0.0], doc_count [50]
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
[[java-aggs-bucket-missing]]
|
||||||
|
==== Missing Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-missing-aggregation.html[Missing Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilders.missing("agg").field("gender");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.missing.Missing;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Missing agg = sr.getAggregations().get("agg");
|
||||||
|
agg.getDocCount(); // Doc count
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
[[java-aggs-bucket-nested]]
|
||||||
|
==== Nested Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-nested-aggregation.html[Nested Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilders
|
||||||
|
.nested("agg")
|
||||||
|
.path("resellers");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.nested.Nested;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Nested agg = sr.getAggregations().get("agg");
|
||||||
|
agg.getDocCount(); // Doc count
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
[[java-aggs-bucket-range]]
|
||||||
|
==== Range Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-range-aggregation.html[Range Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.range("agg")
|
||||||
|
.field("height")
|
||||||
|
.addUnboundedTo(1.0f) // from -infinity to 1.0 (excluded)
|
||||||
|
.addRange(1.0f, 1.5f) // from 1.0 to 1.5 (excluded)
|
||||||
|
.addUnboundedFrom(1.5f); // from 1.5 to +infinity
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.range.Range;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Range agg = sr.getAggregations().get("agg");
|
||||||
|
|
||||||
|
// For each entry
|
||||||
|
for (Range.Bucket entry : agg.getBuckets()) {
|
||||||
|
String key = entry.getKey(); // Range as key
|
||||||
|
Number from = entry.getFrom(); // Bucket from
|
||||||
|
Number to = entry.getTo(); // Bucket to
|
||||||
|
long docCount = entry.getDocCount(); // Doc count
|
||||||
|
|
||||||
|
logger.info("key [{}], from [{}], to [{}], doc_count [{}]", key, from, to, docCount);
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
This will basically produce for the first example:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
key [*-1.0], from [-Infinity], to [1.0], doc_count [9]
|
||||||
|
key [1.0-1.5], from [1.0], to [1.5], doc_count [21]
|
||||||
|
key [1.5-*], from [1.5], to [Infinity], doc_count [20]
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
[[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
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
[[java-aggs-bucket-significantterms]]
|
||||||
|
==== Significant Terms Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-bucket-significantterms-aggregation.html[Significant Terms Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.significantTerms("significant_countries")
|
||||||
|
.field("address.country");
|
||||||
|
|
||||||
|
// Let say you search for men only
|
||||||
|
SearchResponse sr = client.prepareSearch()
|
||||||
|
.setQuery(QueryBuilders.termQuery("gender", "male"))
|
||||||
|
.addAggregation(aggregation)
|
||||||
|
.get();
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
SignificantTerms agg = sr.getAggregations().get("significant_countries");
|
||||||
|
|
||||||
|
// For each entry
|
||||||
|
for (SignificantTerms.Bucket entry : agg.getBuckets()) {
|
||||||
|
entry.getKey(); // Term
|
||||||
|
entry.getDocCount(); // Doc count
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
|
@ -0,0 +1,75 @@
|
||||||
|
[[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
|
||||||
|
|
||||||
|
Ordering the buckets by their `doc_count` in an ascending manner:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilders
|
||||||
|
.terms("genders")
|
||||||
|
.field("gender")
|
||||||
|
.order(Terms.Order.count(true))
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
Ordering the buckets alphabetically by their terms in an ascending manner:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilders
|
||||||
|
.terms("genders")
|
||||||
|
.field("gender")
|
||||||
|
.order(Terms.Order.term(true))
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
Ordering the buckets by single value metrics sub-aggregation (identified by the aggregation name):
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilders
|
||||||
|
.terms("genders")
|
||||||
|
.field("gender")
|
||||||
|
.order(Terms.Order.aggregation("avg_height", false))
|
||||||
|
.subAggregation(
|
||||||
|
AggregationBuilders.avg("avg_height").field("height")
|
||||||
|
)
|
||||||
|
--------------------------------------------------
|
|
@ -0,0 +1,27 @@
|
||||||
|
[[java-aggregations-metrics]]
|
||||||
|
|
||||||
|
include::metrics/min-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::metrics/max-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::metrics/sum-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::metrics/avg-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::metrics/stats-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::metrics/extendedstats-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::metrics/valuecount-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::metrics/percentile-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::metrics/percentile-rank-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::metrics/cardinality-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::metrics/geobounds-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::metrics/tophits-aggregation.asciidoc[]
|
||||||
|
|
||||||
|
include::metrics/scripted-metric-aggregation.asciidoc[]
|
|
@ -0,0 +1,37 @@
|
||||||
|
[[java-aggs-metrics-avg]]
|
||||||
|
==== Avg Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-avg-aggregation.html[Avg Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.avg("agg")
|
||||||
|
.field("height");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.avg.Avg;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Avg agg = sr.getAggregations().get("agg");
|
||||||
|
double value = agg.getValue();
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
[[java-aggs-metrics-cardinality]]
|
||||||
|
==== Cardinality Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-cardinality-aggregation.html[Cardinality Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.cardinality("agg")
|
||||||
|
.field("tags");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Cardinality agg = sr.getAggregations().get("agg");
|
||||||
|
long value = agg.getValue();
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
[[java-aggs-metrics-extendedstats]]
|
||||||
|
==== Extended Stats Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-extendedstats-aggregation.html[Extended Stats Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.extendedStats("agg")
|
||||||
|
.field("height");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Stats agg = sr.getAggregations().get("agg");
|
||||||
|
double min = agg.getMin();
|
||||||
|
double max = agg.getMax();
|
||||||
|
double avg = agg.getAvg();
|
||||||
|
double sum = agg.getSum();
|
||||||
|
long count = agg.getCount();
|
||||||
|
double stdDeviation = agg.getStdDeviation();
|
||||||
|
double sumOfSquares = agg.getSumOfSquares();
|
||||||
|
double variance = agg.getVariance();
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
[[java-aggs-metrics-geobounds]]
|
||||||
|
==== Cardinality Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-geobounds-aggregation.html[Geo Bounds Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
GeoBoundsBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.geoBounds("agg")
|
||||||
|
.field("address.location")
|
||||||
|
.wrapLongitude(true);
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBounds;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
GeoBounds agg = sr.getAggregations().get("agg");
|
||||||
|
GeoPoint bottomRight = agg.bottomRight();
|
||||||
|
GeoPoint topLeft = agg.topLeft();
|
||||||
|
logger.info("bottomRight {}, topLeft {}", bottomRight, topLeft);
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
This will basically produce:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
bottomRight [40.70500764381921, 13.952946866893775], topLeft [53.49603022435221, -4.190029308156676]
|
||||||
|
--------------------------------------------------
|
|
@ -0,0 +1,37 @@
|
||||||
|
[[java-aggs-metrics-max]]
|
||||||
|
==== Max Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-max-aggregation.html[Max Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.max("agg")
|
||||||
|
.field("height");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.max.Max;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Max agg = sr.getAggregations().get("agg");
|
||||||
|
double value = agg.getValue();
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
[[java-aggs-metrics-min]]
|
||||||
|
==== Min Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-min-aggregation.html[Min Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.min("agg")
|
||||||
|
.field("height");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.min.Min;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Min agg = sr.getAggregations().get("agg");
|
||||||
|
double value = agg.getValue();
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
[[java-aggs-metrics-percentile]]
|
||||||
|
==== Percentile Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-percentile-aggregation.html[Percentile Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.percentiles("agg")
|
||||||
|
.field("height");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
You can provide your own percentiles instead of using defaults:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.percentiles("agg")
|
||||||
|
.field("height")
|
||||||
|
.percentiles(1.0, 5.0, 10.0, 20.0, 30.0, 75.0, 95.0, 99.0);
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Percentiles agg = sr.getAggregations().get("agg");
|
||||||
|
// For each entry
|
||||||
|
for (Percentile entry : agg) {
|
||||||
|
double percent = entry.getPercent(); // Percent
|
||||||
|
double value = entry.getValue(); // Value
|
||||||
|
|
||||||
|
logger.info("percent [{}], value [{}]", percent, value);
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
This will basically produce for the first example:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
percent [1.0], value [0.814338896154595]
|
||||||
|
percent [5.0], value [0.8761912455821302]
|
||||||
|
percent [25.0], value [1.173346540141847]
|
||||||
|
percent [50.0], value [1.5432023318692198]
|
||||||
|
percent [75.0], value [1.923915462033674]
|
||||||
|
percent [95.0], value [2.2273644908535335]
|
||||||
|
percent [99.0], value [2.284989339108279]
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
[[java-aggs-metrics-percentile-rank]]
|
||||||
|
==== Percentile Ranks Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-percentile-rank-aggregation.html[Percentile Ranks Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.percentileRanks("agg")
|
||||||
|
.field("height")
|
||||||
|
.percentiles(1.24, 1.91, 2.22);
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
PercentileRanks agg = sr.getAggregations().get("agg");
|
||||||
|
// For each entry
|
||||||
|
for (Percentile entry : agg) {
|
||||||
|
double percent = entry.getPercent(); // Percent
|
||||||
|
double value = entry.getValue(); // Value
|
||||||
|
|
||||||
|
logger.info("percent [{}], value [{}]", percent, value);
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
This will basically produce:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
percent [29.664353095090945], value [1.24]
|
||||||
|
percent [73.9335313461868], value [1.91]
|
||||||
|
percent [94.40095147327283], value [2.22]
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,127 @@
|
||||||
|
[[java-aggs-metrics-scripted-metric]]
|
||||||
|
==== Scripted Metric Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-scripted-metric-aggregation.html[Scripted Metric Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
Don't forget to add Groovy in your classpath if you want to run Groovy scripts in an embedded data node
|
||||||
|
(for unit tests for example).
|
||||||
|
For example, with Maven, add this dependency to your `pom.xml` file:
|
||||||
|
|
||||||
|
[source,xml]
|
||||||
|
--------------------------------------------------
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy-all</artifactId>
|
||||||
|
<version>2.3.2</version>
|
||||||
|
<classifier>indy</classifier>
|
||||||
|
</dependency>
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.scriptedMetric("agg")
|
||||||
|
.initScript("_agg['heights'] = []")
|
||||||
|
.mapScript("if (doc['gender'].value == \"male\") " +
|
||||||
|
"{ _agg.heights.add(doc['height'].value) } " +
|
||||||
|
"else " +
|
||||||
|
"{ _agg.heights.add(-1 * doc['height'].value) }");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
You can also specify a `combine` script which will be executed on each shard:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.scriptedMetric("agg")
|
||||||
|
.initScript("_agg['heights'] = []")
|
||||||
|
.mapScript("if (doc['gender'].value == \"male\") " +
|
||||||
|
"{ _agg.heights.add(doc['height'].value) } " +
|
||||||
|
"else " +
|
||||||
|
"{ _agg.heights.add(-1 * doc['height'].value) }")
|
||||||
|
.combineScript("heights_sum = 0; for (t in _agg.heights) { heights_sum += t }; return heights_sum");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
You can also specify a `reduce` script which will be executed on the node which gets the request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.scriptedMetric("agg")
|
||||||
|
.initScript("_agg['heights'] = []")
|
||||||
|
.mapScript("if (doc['gender'].value == \"male\") " +
|
||||||
|
"{ _agg.heights.add(doc['height'].value) } " +
|
||||||
|
"else " +
|
||||||
|
"{ _agg.heights.add(-1 * doc['height'].value) }")
|
||||||
|
.combineScript("heights_sum = 0; for (t in _agg.heights) { heights_sum += t }; return heights_sum")
|
||||||
|
.reduceScript("heights_sum = 0; for (a in _aggs) { heights_sum += a }; return heights_sum");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
ScriptedMetric agg = sr.getAggregations().get("agg");
|
||||||
|
Object scriptedResult = agg.aggregation();
|
||||||
|
logger.info("scriptedResult [{}]", scriptedResult);
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
Note that the result depends on the script you built.
|
||||||
|
For the first example, this will basically produce:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
scriptedResult object [ArrayList]
|
||||||
|
scriptedResult [ {
|
||||||
|
"heights" : [ 1.122218480146643, -1.8148918111233887, -1.7626731575142909, ... ]
|
||||||
|
}, {
|
||||||
|
"heights" : [ -0.8046067304119863, -2.0785486707864553, -1.9183567430207953, ... ]
|
||||||
|
}, {
|
||||||
|
"heights" : [ 2.092635728868694, 1.5697545960886536, 1.8826954461968808, ... ]
|
||||||
|
}, {
|
||||||
|
"heights" : [ -2.1863201099468403, 1.6328549117346856, -1.7078288405893842, ... ]
|
||||||
|
}, {
|
||||||
|
"heights" : [ 1.6043904836424177, -2.0736538674414025, 0.9898266674373053, ... ]
|
||||||
|
} ]
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
The second example will produce:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
scriptedResult object [ArrayList]
|
||||||
|
scriptedResult [-41.279615707402876,
|
||||||
|
-60.88007362339038,
|
||||||
|
38.823270659734256,
|
||||||
|
14.840192739445632,
|
||||||
|
11.300902755741326]
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
The last example will produce:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
scriptedResult object [Double]
|
||||||
|
scriptedResult [2.171917696507009]
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
[[java-aggs-metrics-stats]]
|
||||||
|
==== Stats Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-stats-aggregation.html[Stats Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.stats("agg")
|
||||||
|
.field("height");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Stats agg = sr.getAggregations().get("agg");
|
||||||
|
double min = agg.getMin();
|
||||||
|
double max = agg.getMax();
|
||||||
|
double avg = agg.getAvg();
|
||||||
|
double sum = agg.getSum();
|
||||||
|
long count = agg.getCount();
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
[[java-aggs-metrics-sum]]
|
||||||
|
==== Sum Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-sum-aggregation.html[Sum Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.sum("agg")
|
||||||
|
.field("height");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Sum agg = sr.getAggregations().get("agg");
|
||||||
|
double value = agg.getValue();
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
[[java-aggs-metrics-tophits]]
|
||||||
|
==== Top Hits Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-tophits-aggregation.html[Top Hits Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.terms("agg").field("gender")
|
||||||
|
.subAggregation(
|
||||||
|
AggregationBuilders.topHits("top")
|
||||||
|
);
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
You can use most of the options available for standard search such as `from`, `size`, `sort`, `highlight`, `explain`...
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
AggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.terms("agg").field("gender")
|
||||||
|
.subAggregation(
|
||||||
|
AggregationBuilders.topHits("top")
|
||||||
|
.setExplain(true)
|
||||||
|
.setSize(1)
|
||||||
|
.setFrom(10)
|
||||||
|
);
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
Terms agg = sr.getAggregations().get("agg");
|
||||||
|
|
||||||
|
// For each entry
|
||||||
|
for (Terms.Bucket entry : agg.getBuckets()) {
|
||||||
|
String key = entry.getKey(); // bucket key
|
||||||
|
long docCount = entry.getDocCount(); // Doc count
|
||||||
|
logger.info("key [{}], doc_count [{}]", key, docCount);
|
||||||
|
|
||||||
|
// We ask for top_hits for each bucket
|
||||||
|
TopHits topHits = entry.getAggregations().get("top");
|
||||||
|
for (SearchHit hit : topHits.getHits().getHits()) {
|
||||||
|
logger.info(" -> id [{}], _source [{}]", hit.getId(), hit.getSourceAsString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
This will basically produce for the first example:
|
||||||
|
|
||||||
|
[source,text]
|
||||||
|
--------------------------------------------------
|
||||||
|
key [male], doc_count [5107]
|
||||||
|
-> id [AUnzSZze9k7PKXtq04x2], _source [{"gender":"male",...}]
|
||||||
|
-> id [AUnzSZzj9k7PKXtq04x4], _source [{"gender":"male",...}]
|
||||||
|
-> id [AUnzSZzl9k7PKXtq04x5], _source [{"gender":"male",...}]
|
||||||
|
key [female], doc_count [4893]
|
||||||
|
-> id [AUnzSZzM9k7PKXtq04xy], _source [{"gender":"female",...}]
|
||||||
|
-> id [AUnzSZzp9k7PKXtq04x8], _source [{"gender":"female",...}]
|
||||||
|
-> id [AUnzSZ0W9k7PKXtq04yS], _source [{"gender":"female",...}]
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
[[java-aggs-metrics-valuecount]]
|
||||||
|
==== Value Count Aggregation
|
||||||
|
|
||||||
|
Here is how you can use
|
||||||
|
{ref}/search-aggregations-metrics-valuecount-aggregation.html[Value Count Aggregation]
|
||||||
|
with Java API.
|
||||||
|
|
||||||
|
|
||||||
|
===== Prepare aggregation request
|
||||||
|
|
||||||
|
Here is an example on how to create the aggregation request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
MetricsAggregationBuilder aggregation =
|
||||||
|
AggregationBuilders
|
||||||
|
.count("agg")
|
||||||
|
.field("height");
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
===== Use aggregation response
|
||||||
|
|
||||||
|
Import Aggregation definition classes:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
// sr is here your SearchResponse object
|
||||||
|
ValueCount agg = sr.getAggregations().get("agg");
|
||||||
|
long value = agg.getValue();
|
||||||
|
--------------------------------------------------
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
[[java-aggs]]
|
||||||
|
== Aggregations
|
||||||
|
|
||||||
|
Elasticsearch provides a full Java API to play with aggregations. See the
|
||||||
|
{ref}/search-aggregations.html[Aggregations guide].
|
||||||
|
|
||||||
|
Use the factory for aggregation builders (`AggregationBuilders`) and add each aggregation
|
||||||
|
you want to compute when querying and add it to your search request:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
SearchResponse sr = node.client().prepareSearch()
|
||||||
|
.setQuery( /* your query */ )
|
||||||
|
.addAggregation( /* add an aggregation */ )
|
||||||
|
.execute().actionGet();
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
Note that you can add more than one aggregation. See
|
||||||
|
{ref}/search-search.html[Search Java API] for details.
|
||||||
|
|
||||||
|
To build aggregation requests, use `AggregationBuilders` helpers. Just import them
|
||||||
|
in your class:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
=== Structuring aggregations
|
||||||
|
|
||||||
|
As explained in the
|
||||||
|
{ref}/search-aggregations.html[Aggregations guide], you can define
|
||||||
|
sub aggregations inside an aggregation.
|
||||||
|
|
||||||
|
An aggregation could be a metrics aggregation or a bucket aggregation.
|
||||||
|
|
||||||
|
For example, here is a 3 levels aggregation composed of:
|
||||||
|
|
||||||
|
* Terms aggregation (bucket)
|
||||||
|
* Date Histogram aggregation (bucket)
|
||||||
|
* Average aggregation (metric)
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
--------------------------------------------------
|
||||||
|
SearchResponse sr = node.client().prepareSearch()
|
||||||
|
.addAggregation(
|
||||||
|
AggregationBuilders.terms("by_country").field("country")
|
||||||
|
.subAggregation(AggregationBuilders.dateHistogram("by_year")
|
||||||
|
.field("dateOfBirth")
|
||||||
|
.interval((DateHistogram.Interval.YEAR)
|
||||||
|
.subAggregation(AggregationBuilders.avg("avg_children").field("children"))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.execute().actionGet();
|
||||||
|
--------------------------------------------------
|
||||||
|
|
||||||
|
=== Metrics aggregations
|
||||||
|
|
||||||
|
include::aggregations/metrics.asciidoc[]
|
||||||
|
|
||||||
|
=== Bucket aggregations
|
||||||
|
|
||||||
|
include::aggregations/bucket.asciidoc[]
|
||||||
|
|
|
@ -53,6 +53,8 @@ include::delete-by-query.asciidoc[]
|
||||||
|
|
||||||
include::facets.asciidoc[]
|
include::facets.asciidoc[]
|
||||||
|
|
||||||
|
include::aggs.asciidoc[]
|
||||||
|
|
||||||
include::percolate.asciidoc[]
|
include::percolate.asciidoc[]
|
||||||
|
|
||||||
include::query-dsl-queries.asciidoc[]
|
include::query-dsl-queries.asciidoc[]
|
||||||
|
|
|
@ -119,23 +119,29 @@ for (MultiSearchResponse.Item item : sr.getResponses()) {
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
[[java-search-facets]]
|
[[java-search-aggs]]
|
||||||
=== Using Facets
|
=== Using Aggregations
|
||||||
|
|
||||||
The following code shows how to add two facets within your search:
|
The following code shows how to add two aggregations within your search:
|
||||||
|
|
||||||
[source,java]
|
[source,java]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
SearchResponse sr = node.client().prepareSearch()
|
SearchResponse sr = node.client().prepareSearch()
|
||||||
.setQuery(QueryBuilders.matchAllQuery())
|
.setQuery(QueryBuilders.matchAllQuery())
|
||||||
.addFacet(FacetBuilders.termsFacet("f1").field("field"))
|
.addAggregation(
|
||||||
.addFacet(FacetBuilders.dateHistogramFacet("f2").field("birth").interval("year"))
|
AggregationBuilders.terms("agg1").field("field")
|
||||||
|
)
|
||||||
|
.addAggregation(
|
||||||
|
AggregationBuilders.dateHistogram("agg2")
|
||||||
|
.field("birth")
|
||||||
|
.interval(DateHistogram.Interval.YEAR)
|
||||||
|
)
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
|
|
||||||
// Get your facet results
|
// Get your facet results
|
||||||
TermsFacet f1 = (TermsFacet) sr.getFacets().facetsAsMap().get("f1");
|
Terms agg1 = sr.getAggregations().get("agg1");
|
||||||
DateHistogramFacet f2 = (DateHistogramFacet) sr.getFacets().facetsAsMap().get("f2");
|
DateHistogram agg2 = sr.getAggregations().get("agg2");
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
|
||||||
See <<java-facets,Facets Java API>>
|
See <<java-aggs,Aggregations Java API>>
|
||||||
documentation for details.
|
documentation for details.
|
||||||
|
|
Loading…
Reference in New Issue