2013-11-24 06:13:08 -05:00
|
|
|
[[search-aggregations]]
|
|
|
|
== Aggregations
|
|
|
|
|
2013-12-02 09:14:25 -05:00
|
|
|
Aggregations grew out of the <<search-facets, facets>> module and the long experience of how users use it
|
2013-12-01 19:54:42 -05:00
|
|
|
(and would like to use it) for real-time data analytics purposes. As such, it serves as the next generation
|
|
|
|
replacement for the functionality we currently refer to as "faceting".
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2013-12-01 19:54:42 -05:00
|
|
|
<<search-facets, Facets>> provide a great way to aggregate data within a document set context.
|
|
|
|
This context is defined by the executed query in combination with the different levels of filters that can be defined
|
2014-01-23 07:53:49 -05:00
|
|
|
(filtered queries, top-level filters, and facet level filters). While powerful, their implementation is not designed
|
2014-03-12 21:28:40 -04:00
|
|
|
from the ground up to support complex aggregations and is thus limited.
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2013-12-01 19:54:42 -05:00
|
|
|
The aggregations module breaks the barriers the current facet implementation put in place. The new name ("Aggregations")
|
2014-01-23 07:53:49 -05:00
|
|
|
also indicates the intention here - a generic yet extremely powerful framework for building aggregations - any types of
|
2013-12-01 19:54:42 -05:00
|
|
|
aggregations.
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2013-12-01 19:54:42 -05:00
|
|
|
An aggregation can be seen as a _unit-of-work_ that builds analytic information over a set of documents. The context of
|
2014-01-23 07:53:49 -05:00
|
|
|
the execution defines what this document set is (e.g. a top-level aggregation executes within the context of the executed
|
2013-12-01 19:54:42 -05:00
|
|
|
query/filters of the search request).
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2013-12-01 19:54:42 -05:00
|
|
|
There are many different types of aggregations, each with its own purpose and output. To better understand these types,
|
|
|
|
it is often easier to break them into two main families:
|
2013-11-24 06:13:08 -05:00
|
|
|
|
|
|
|
_Bucketing_::
|
2013-12-01 19:54:42 -05:00
|
|
|
A family of aggregations that build buckets, where each bucket is associated with a _key_ and a document
|
2014-01-23 07:53:49 -05:00
|
|
|
criterion. When the aggregation is executed, all the buckets criteria are evaluated on every document in
|
|
|
|
the context and when a criterion matches, the document is considered to "fall in" the relevant bucket.
|
|
|
|
By the end of the aggregation process, we'll end up with a list of buckets - each one with a set of
|
|
|
|
documents that "belong" to it.
|
2013-11-24 06:13:08 -05:00
|
|
|
|
|
|
|
_Metric_::
|
2014-03-12 21:28:40 -04:00
|
|
|
Aggregations that keep track and compute metrics over a set of documents.
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2014-03-12 21:28:40 -04:00
|
|
|
The interesting part comes next. Since each bucket effectively defines a document set (all documents belonging to
|
|
|
|
the bucket), one can potentially associate aggregations on the bucket level, and those will execute within the context
|
2013-12-01 19:54:42 -05:00
|
|
|
of that bucket. This is where the real power of aggregations kicks in: *aggregations can be nested!*
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2014-03-12 21:28:40 -04:00
|
|
|
NOTE: Bucketing aggregations can have sub-aggregations (bucketing or metric). The sub-aggregations will be computed for
|
|
|
|
the buckets which their parent aggregation generates. There is no hard limit on the level/depth of nested
|
|
|
|
aggregations (one can nest an aggregation under a "parent" aggregation, which is itself a sub-aggregation of
|
|
|
|
another higher-level aggregation).
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2013-11-29 06:35:25 -05:00
|
|
|
[float]
|
2013-11-24 06:13:08 -05:00
|
|
|
=== Structuring Aggregations
|
|
|
|
|
|
|
|
The following snippet captures the basic structure of aggregations:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
"aggregations" : {
|
|
|
|
"<aggregation_name>" : {
|
|
|
|
"<aggregation_type>" : {
|
|
|
|
<aggregation_body>
|
|
|
|
}
|
|
|
|
[,"aggregations" : { [<sub_aggregation>]+ } ]?
|
|
|
|
}
|
|
|
|
[,"<aggregation_name_2>" : { ... } ]*
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2014-01-23 07:53:49 -05:00
|
|
|
The `aggregations` object (the key `aggs` can also be used) in the JSON holds the aggregations to be computed. Each aggregation
|
2014-03-12 21:28:40 -04:00
|
|
|
is associated with a logical name that the user defines (e.g. if the aggregation computes the average price, then it would
|
2013-12-01 19:54:42 -05:00
|
|
|
make sense to name it `avg_price`). These logical names will also be used to uniquely identify the aggregations in the
|
|
|
|
response. Each aggregation has a specific type (`<aggregation_type>` in the above snippet) and is typically the first
|
2014-03-12 21:28:40 -04:00
|
|
|
key within the named aggregation body. Each type of aggregation defines its own body, depending on the nature of the
|
2014-01-23 07:53:49 -05:00
|
|
|
aggregation (e.g. an `avg` aggregation on a specific field will define the field on which the average will be calculated).
|
2013-12-01 19:54:42 -05:00
|
|
|
At the same level of the aggregation type definition, one can optionally define a set of additional aggregations,
|
|
|
|
though this only makes sense if the aggregation you defined is of a bucketing nature. In this scenario, the
|
|
|
|
sub-aggregations you define on the bucketing aggregation level will be computed for all the buckets built by the
|
2014-03-12 21:28:40 -04:00
|
|
|
bucketing aggregation. For example, if you define a set of aggregations under the `range` aggregation, the
|
2014-01-23 07:53:49 -05:00
|
|
|
sub-aggregations will be computed for the range buckets that are defined.
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2013-11-29 06:35:25 -05:00
|
|
|
[float]
|
2013-11-24 06:13:08 -05:00
|
|
|
==== Values Source
|
|
|
|
|
2013-12-01 19:54:42 -05:00
|
|
|
Some aggregations work on values extracted from the aggregated documents. Typically, the values will be extracted from
|
2014-01-23 07:53:49 -05:00
|
|
|
a specific document field which is set using the `field` key for the aggregations. It is also possible to define a
|
2014-03-12 21:28:40 -04:00
|
|
|
<<modules-scripting,`script`>> which will generate the values (per document).
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2013-12-01 19:54:42 -05:00
|
|
|
When both `field` and `script` settings are configured for the aggregation, the script will be treated as a
|
|
|
|
`value script`. While normal scripts are evaluated on a document level (i.e. the script has access to all the data
|
|
|
|
associated with the document), value scripts are evaluated on the *value* level. In this mode, the values are extracted
|
2014-03-12 21:28:40 -04:00
|
|
|
from the configured `field` and the `script` is used to apply a "transformation" over these value/s.
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2013-12-01 19:54:42 -05:00
|
|
|
["NOTE",id="aggs-script-note"]
|
2013-11-24 06:13:08 -05:00
|
|
|
===============================
|
2013-12-01 19:54:42 -05:00
|
|
|
When working with scripts, the `lang` and `params` settings can also be defined. The former defines the scripting
|
2014-03-12 21:28:40 -04:00
|
|
|
language which is used (assuming the proper language is available in Elasticsearch, either by default or as a plugin). The latter
|
|
|
|
enables defining all the "dynamic" expressions in the script as parameters, which enables the script to keep itself static
|
2014-01-23 07:53:49 -05:00
|
|
|
between calls (this will ensure the use of the cached compiled scripts in Elasticsearch).
|
2013-11-24 06:13:08 -05:00
|
|
|
===============================
|
|
|
|
|
2014-03-12 21:28:40 -04:00
|
|
|
Scripts can generate a single value or multiple values per document. When generating multiple values, one can use the
|
2014-01-23 07:53:49 -05:00
|
|
|
`script_values_sorted` settings to indicate whether these values are sorted or not. Internally, Elasticsearch can
|
2013-12-01 19:54:42 -05:00
|
|
|
perform optimizations when dealing with sorted values (for example, with the `min` aggregations, knowing the values are
|
2014-01-23 07:53:49 -05:00
|
|
|
sorted, Elasticsearch will skip the iterations over all the values and rely on the first value in the list to be the
|
2013-12-01 19:54:42 -05:00
|
|
|
minimum value among all other values associated with the same document).
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2013-11-29 06:35:25 -05:00
|
|
|
[float]
|
|
|
|
=== Metrics Aggregations
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2013-11-29 06:35:25 -05:00
|
|
|
The aggregations in this family compute metrics based on values extracted in one way or another from the documents that
|
|
|
|
are being aggregated. The values are typically extracted from the fields of the document (using the field data), but
|
2014-05-09 20:04:06 -04:00
|
|
|
can also be generated using scripts.
|
2014-05-29 05:37:03 -04:00
|
|
|
|
|
|
|
Numeric metrics aggregations are a special type of metrics aggregation which output numeric values. Some aggregations output
|
2014-08-06 05:54:51 -04:00
|
|
|
a single numeric metric (e.g. `avg`) and are called `single-value numeric metrics aggregation`, others generate multiple
|
|
|
|
metrics (e.g. `stats`) and are called `multi-value numeric metrics aggregation`. The distinction between single-value and
|
|
|
|
multi-value numeric metrics aggregations plays a role when these aggregations serve as direct sub-aggregations of some
|
2014-05-09 20:04:06 -04:00
|
|
|
bucket aggregations (some bucket aggregations enable you to sort the returned buckets based on the numeric metrics in each bucket).
|
2013-11-24 06:13:08 -05:00
|
|
|
|
|
|
|
|
2013-11-29 06:35:25 -05:00
|
|
|
[float]
|
|
|
|
=== Bucket Aggregations
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2013-11-29 06:35:25 -05:00
|
|
|
Bucket aggregations don't calculate metrics over fields like the metrics aggregations do, but instead, they create
|
2014-05-09 20:04:06 -04:00
|
|
|
buckets of documents. Each bucket is associated with a criterion (depending on the aggregation type) which determines
|
2014-03-12 21:28:40 -04:00
|
|
|
whether or not a document in the current context "falls" into it. In other words, the buckets effectively define document
|
2013-11-29 06:35:25 -05:00
|
|
|
sets. In addition to the buckets themselves, the `bucket` aggregations also compute and return the number of documents
|
2014-03-12 21:28:40 -04:00
|
|
|
that "fell in" to each bucket.
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2014-03-12 21:28:40 -04:00
|
|
|
Bucket aggregations, as opposed to `metrics` aggregations, can hold sub-aggregations. These sub-aggregations will be
|
2014-01-23 07:53:49 -05:00
|
|
|
aggregated for the buckets created by their "parent" bucket aggregation.
|
2013-11-29 06:35:25 -05:00
|
|
|
|
|
|
|
There are different bucket aggregators, each with a different "bucketing" strategy. Some define a single bucket, some
|
|
|
|
define fixed number of multiple buckets, and others dynamically create the buckets during the aggregation process.
|
|
|
|
|
2014-08-06 05:54:51 -04:00
|
|
|
[float]
|
|
|
|
=== Caching heavy aggregations
|
|
|
|
|
|
|
|
coming[1.4.0]
|
|
|
|
|
|
|
|
Frequently used aggregations (e.g. for display on the home page of a website)
|
|
|
|
can be cached for faster responses. These cached results are the same results
|
|
|
|
that would be returned by an uncached aggregation -- you will never get stale
|
|
|
|
results.
|
|
|
|
|
|
|
|
See <<index-modules-shard-query-cache>> for more details.
|
|
|
|
|
2014-08-08 07:28:03 -04:00
|
|
|
[float]
|
|
|
|
=== Returning only aggregation results
|
|
|
|
|
|
|
|
There are many occasions when aggregations are required but search hits are not. For these cases the hits can be ignored by
|
|
|
|
adding `search_type=count` to the request URL parameters. For example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?search_type=count' -d '{
|
|
|
|
"aggregations": {
|
|
|
|
"my_agg": {
|
|
|
|
"terms": {
|
|
|
|
"field": "text"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
Setting `search_type` to `count` avoids executing the fetch phase of the search making the request more efficient. See
|
|
|
|
<<search-request-search-type>> for more information on the `search_type` parameter.
|
2013-11-29 06:35:25 -05:00
|
|
|
|
|
|
|
include::aggregations/metrics.asciidoc[]
|
|
|
|
|
|
|
|
include::aggregations/bucket.asciidoc[]
|