[Recipes] Single Value Metric Aggregations - Sum, Avg, Min, Max, Count, Cardinality

Problem: 

Give code to demo Sum, Avg, Min, Max, Count and Cardinality metric aggregations. 

Solution Summary: 

We can use the metric aggregations sum, avg, min, max, value_count and cardinality.

Prerequisites: 

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

Solution Steps: 

Case 1 - Sum Aggregation

GET accounts/_search
{
  "aggregations" : {
    "balance_total" : {
      "sum" : {
        "field":"balance"
      }
    }
  },
  "size": 0
}

Note:

  1. Added size = 0 to see only aggregation data. Otherwise documents equal to the size (10 by default) will be returned along with aggregation result.

  2. "balance_total" is just an arbitraty name and ES uses the same in the response as well.

  3. Average aggregation (avg) is similar; just replace "sum" with "avg".

  4. We may also use "aggs" instead of "aggregations".

 

Response:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "balance_total": {
      "value": 25714837
    }

  }
}

 

Case 2 - Sum, Avg, Min, Max in single request

GET accounts/_search
{
  "aggregations" : {
    "balance_total" : {
      "sum" : {
        "field":"balance"
      }
    },
    "balance_average" : {
      "avg" : {
        "field":"balance"
      }
    },
    "balance_min" : {
      "min" : {
        "field":"balance"
      }
    },
    "balance_max" : {
      "max" : {
        "field":"balance"
      }
    }
  },
  "size": 0
}

 

Response will have:

...

"aggregations": {
    "balance_min": {
      "value": 1011
    },
    "balance_max": {
      "value": 49989
    },
    "balance_average": {
      "value": 25714.837
    },
    "balance_total": {
      "value": 25714837
    }
  }

 

Case 3 - Total number of accounts using value_count

GET accounts/_search
{
  "aggregations" : {
    "total_count" : {
      "value_count" : {
        "field":"_id"
      }
    }
  },
  "size": 0
}

 

Case 4 - Total number of unique states

GET accounts/_search
{
  "aggregations" : {
    "state_cardinality" : {
      "cardinality" : {
        "field":"state.keyword"
      }
    }
  },
  "size": 0
}

Note: Cardinality aggregation may return approximate results. However, you can increase the accuracy by sacrificing some performance. 

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