Executing the stats aggregation

The most commonly used metric aggregations are stats aggregations. They are generally used as terminal aggregation steps to compute a value to be used directly or for sorting.

Getting ready

You need a working ElasticSearch cluster and an index populated with the script (chapter_06/executing_stat_aggregations.sh) available at https://github.com/aparo/elasticsearch-cookbook-second-edition.

How to do it...

To execute a stats aggregation, we will perform the steps given as follows:

  1. We want to calculate all statistical values of a matched query in the age field. The REST call should be as follows:
    curl -XPOST "http://127.0.0.1:9200/test-index/_search?size=0" -d '
    {
      "query": {
        "match_all": {}
      },
      "aggs": {
     "age_stats": ...

Get ElasticSearch Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.