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 date histogram aggregation


The previous recipe works mainly on numeric fields; ElasticSearch provides a custom date histogram aggregation to operate on date/date-time values. This aggregation is required because date values need more customization to solve problems such as time zone conversion and special time intervals.

Getting ready

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

How to do it...

We need two different date/time aggregations that are:

  • An annual aggregation

  • A quarter aggregation, but with time zone +1:00

To execute date histogram aggregations, we will perform the steps given as follows:

  1. The query will be as follows:

    curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search? pretty=true' -d '
    {
      "query": {
        "match_all": {}
      },
      "aggs": {
        "date_year": {
          "date_histogram": {
            "field...