[Recipes] Global Aggregations in Elasticsearch

Problem: 

Demo the use of Global aggregations.

Solution Summary: 

Global aggregations defines a single bucket of all the documents within the search execution context. This context is defined by the indices and the document types you’re searching on, but is not influenced by the search query itself.

Global aggregators can only be placed as top level aggregators, not within a nested child aggregation. 

Prerequisites: 

Set up accounts index from accounts.json as explained in the link

Solution Steps: 

Case 0 - Without global Aggregation

GET accounts/_search
{
  "query" : {
    "exists": {
      "field": "opening_date"
    }
  },
  "aggs": {
    "query_count": {
      "value_count": {
        "field": "_id"
      }
    }
  },
  "size": 0
}

 

Response contains:

...

"hits": {
    "total": 10,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "query_count": {
      "value": 10
    }
  }

 

Case 1 - With Global Aggregation

GET accounts/_search
{
  "query" : {
    "exists": {
      "field": "opening_date"
    }
  },
  "aggs": {
    "all_count": {
      "global": {},
      "aggs": {
        "all_count_sub": {
          "value_count": {
            "field": "_id"
          }
        }
      }
    }
  },
  "size": 0
}

 

Response contains:

...

"hits": {
    "total": 10,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "all_count": {
      "doc_count": 1000,
      "all_count_sub": {
        "value": 1000
      }
    }
  }
}

Recipe Tags: 

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream