The previous example told us the number of documents in each bucket, which is useful. But often, our applications require more-sophisticated metrics about the documents. For example, what is the average price of cars in each bucket?
To get this information, we need to tell Elasticsearch which metrics to calculate, and on which fields. This requires nesting metrics inside the buckets. Metrics will calculate mathematical statistics based on the values of documents within a bucket.
Let’s go ahead and add an
average metric to our car example:
We add a new
We then give the metric a name:
And finally, we define it as an
As you can see, we took the previous example and tacked on a new
This new aggregation level allows us to nest the
avg metric inside the
terms bucket. Effectively, this means we will generate an average for each
Just like the
colors example, we need to name our metric (
avg_price) so we
can retrieve the values later. Finally, we specify the metric itself (
and what field we want the average to be calculated on (
Although the response has changed minimally, the data we get out of it has grown substantially. Before, we knew there were four red cars. Now we know that the average price of red cars is $32,500. This is something that you can plug directly into reports or graphs.