English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Elasticsearch Aggregation

The aggregation framework collects all the data selected by the search query and is composed of many building blocks, which help to construct complex summaries of data. The basic structure of aggregation is as follows-

"aggregations": {
   "": {
      "": {
      }
 
      [["meta": { [] }]]?
      [["aggregations": { [] }]]?+ }
   }
   [,"": { ... } ]?*
}

Aggregations have different types, each with its own purpose. This chapter will discuss these issues in detail.

Metrics Aggregation

These aggregations help to calculate the matrix based on the field values of the aggregated documents, and sometimes some values can also be generated from scripts.

The numerical matrix can be either a single value (such as average aggregation) or a multi-value (such as statistics).

Average Aggregation

This aggregation is used to obtain the average value of any numeric field existing in the aggregated documents. For example,

POST /schools/_search
{
   "aggs":{
      "avg_fees":{"avg":{"field":"fees"}}
   }
}

When running the above code, we get the following results-

{
   "took": 41,
   "timed_out": false
   "_shards": {
      "total": 1,
      "successful": 1,
      "skipped": 0,
      "failed": 0
   ,
   "hits": {
      "total": {
         "value": 2,
         "relation": "eq"
      ,
      "max_score": 1.0,
      "hits": [
         {
            "_index": "schools",
            "_type": "school",
            "_id": ""5",
            "_score": 1.0,
            "_source": {
               "name": "Central School",
               "description": "CBSE Affiliation",
               "street": "Nagan",
               "city": "paprola",
               "state": "HP",
               "zip": ""176115",
               "location": [
                  31.8955385,
                  76.8380405
               ],
            "fees": 2200,
            "tags": [
               "Senior Secondary",
               "beautiful campus"
            ],
            "rating": ""3.3"
         }
      ,
      {
         "_index": "schools",
         "_type": "school",
         "_id": ""4",
         "_score": 1.0,
         "_source": {
            "name": "City Best School",
            "description": "ICSE",
            "street": "West End",
            "city": "Meerut",
            "state": "UP",
            "zip": ""250002",
            "location": [
               28.9926174,
               77.692485
            ],
            "fees": 3500,
            "tags": [
               "fully computerized"
            ],
            "rating": ""4.5"
         }
      }
   ]
 ,
   "aggregations": {
      "avg_fees": {
         "value": 2850.0
      }
   }
}

Cardinality Aggregation

This aggregation provides the count of different values for a specific field.

POST /schools/_search?size=0
{
   "aggs":{
      "distinct_name_count":{"cardinality":{"field":"fees"}}
   }
}

When running the above code, we get the following results-

{
   "took": 2,
   "timed_out": false
   "_shards": {
      "total": 1,
      "successful": 1,
      "skipped": 0,
      "failed": 0
   ,
   "hits": {
      "total": {
         "value": 2,
         "relation": "eq"
      ,
      "max_score": null,
      "hits": [
   ,
   "aggregations": {
      "distinct_name_count": {
         "value": 2
      }
   }
}

Note −The value of the base is2because there are two different values for the cost.

Extended Statistics Aggregation

This aggregation generates all statistical information about a specific numeric field in the aggregated documents.

POST /schools/_search?size=0
{
   "aggs": {
      "fees_stats": { "extended_stats": { "field": "fees" } }
   }
}

When running the above code, we get the following results-

{
   "took": 8,
   "timed_out": false
   "_shards": {
      "total": 1,
      "successful": 1,
      "skipped": 0,
      "failed": 0
   ,
   "hits": {
      "total": {
         "value": 2,
         "relation": "eq"
      ,
      "max_score": null,
      "hits": [
   ,
   "aggregations": {
      "fees_stats": {
         "count": 2,
         "min": 2200.0,
         "max": 3500.0,
         "avg": 2850.0,
         "sum": 5700.0,
         "sum_of_squares": 1.709E7,
         "variance": 422500.0,
         "std_deviation": 650.0,
         "std_deviation_bounds": {
            "upper": 4150.0,
            "lower": 1550.0
         }
      }
   }
}

Maximum Aggregation

This aggregation finds the maximum value of a specific numeric field in the aggregated documents.

POST /schools/_search?size=0
{
   "aggs": {
   "max_fees": { "max": { "field": "fees" } }
   }
}

When running the above code, we get the following results-

{
   "took": 16,
   "timed_out": false
   "_shards": {
      "total": 1,
      "successful": 1,
      "skipped": 0,
      "failed": 0
   ,
  "hits": {
      "total": {
         "value": 2,
         "relation": "eq"
      ,
      "max_score": null,
      "hits": [
   ,
   "aggregations": {
      "max_fees": {
         "value": 3500.0
      }
   }
}

Minimum Aggregation

This aggregation finds the minimum value of a specific numeric field in the aggregated documents.

POST /schools/_search?size=0
{
   "aggs": {
      "min_fees": { "min": { "field": "fees" } }
   }
}

When running the above code, we get the following results-

{
   "took": 2,
   "timed_out": false
   "_shards": {
      "total": 1,
      "successful": 1,
      "skipped": 0,
      "failed": 0
   ,
   "hits": {
      "total": {
         "value": 2,
         "relation": "eq"
      ,
      "max_score": null,
      "hits": [
   ,
  "aggregations": {
      "min_fees": {
         "value": 2200.0
      }
   }
}

Aggregate Sum

This aggregation calculates the sum of a specific numeric field in the aggregated documents.

POST /schools/_search?size=0
{
   "aggs": {
      "total_fees": { "sum": { "field": "fees" } }
   }
}

When running the above code, we get the following results-

{
   "took": 8,
   "timed_out": false
   "_shards": {
      "total": 1,
      "successful": 1,
      "skipped": 0,
      "failed": 0
   ,
   "hits": {
      "total": {
         "value": 2,
         "relation": "eq"
      ,
      "max_score": null,
      "hits": [
   ,
   "aggregations": {
      "total_fees": {
         "value": 5700.0
      }
   }
}

In special cases, there are also other metric aggregations such as geo boundary aggregation and geo centroid aggregation to achieve geolocation.

Statistical Aggregation

A multi-value metric aggregation that can calculate statistics based on numeric values extracted from the aggregated documents.

POST /schools/_search?size=0
{
   "aggs": {
      "grades_stats": { "stats": { "field": "fees" } }
   }
}

When running the above code, we get the following results-

{
   "took": 2,
   "timed_out": false
   "_shards": {
      "total": 1,
      "successful": 1,
      "skipped": 0,
      "failed": 0
   ,
   "hits": {
      "total": {
         "value": 2,
         "relation": "eq"
      ,
      "max_score": null,
      "hits": [
   ,
   "aggregations": {
      "grades_stats": {
         "count": 2,
         "min": 2200.0,
         "max": 3500.0,
         "avg": 2850.0,
         "sum": 5700.0
      }
   }
}

Aggregate Metadata

You can use the meta tag to add some aggregate-related data when making the request, and obtain it as the response.

POST /schools/_search?size=0
{
   "aggs": {
      "min_fees": { "avg": { "field": "fees" } }
         "meta": {
            "dsc": "Lowest Fees This Year"
         }
      }
   }
}

When running the above code, we get the following results-

{
   "took": 0
   "timed_out": false
   "_shards": {
      "total": 1,
      "successful": 1,
      "skipped": 0,
      "failed": 0
   ,
   "hits": {
      "total": {
         "value": 2,
         "relation": "eq"
      ,
      "max_score": null,
      "hits": [
   ,
   "aggregations": {
      "min_fees": {
         "meta": {
            "dsc": "Lowest Fees This Year"
         ,
         "value": 2850.0
      }
   }
}