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 an aggregation


ElasticSearch provides several functionalities other than search; it allows executing statistics and real-time analytics on searches via aggregations.

Getting ready

You need a working ElasticSearch cluster and an index populated with the script, which is available at https://github.com/aparo/elasticsearch-cookbook-second-edition.

How to do it...

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

  1. From the command line, we can execute a query with aggregations:

    curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search?pretty=true&size=0' -d '{
      "query": {
        "match_all": {}
      },
      "aggregations": {
        "tag": {
          "terms": {
            "field": "tag",
            "size": 10
          }
        }
      }
    }'
    

    In this case, we have used a match_all query plus a terms aggregation that is used to count terms.

  2. The result returned by ElasticSearch, if everything is all right, should be:

    {
      "took" : 3,
      "timed_out" : false,
      "_shards" : {… truncated …},
      "hits"...