Book Image

ElasticSearch Cookbook

By : Alberto Paro
Book Image

ElasticSearch Cookbook

By: Alberto Paro

Overview of this book

Table of Contents (20 chapters)
ElasticSearch Cookbook Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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": {
          "extended_stats": {
            "field": "age"
          }
        }
      }
    }'
    
  2. The result, if everything is fine, should be:

    {
      "took" : 2,
      "timed_out" : false,
      "_shards" : { …truncated…},
      "hits" : {
        "total" : 1000,
        "max_score" : 0...