0
0
Elasticsearchquery~5 mins

Why aggregations summarize data in Elasticsearch

Choose your learning style9 modes available
Introduction

Aggregations help you quickly find useful summaries from lots of data. They show totals, averages, counts, and other summaries so you understand your data better.

You want to count how many items match a search.
You need to find the average price of products in a store.
You want to group data by categories and see totals for each.
You want to find the highest or lowest value in your data.
You want to see trends or patterns by summarizing large data sets.
Syntax
Elasticsearch
{
  "aggs": {
    "aggregation_name": {
      "aggregation_type": {
        "field": "field_name"
      }
    }
  }
}

Replace aggregation_name with a name you choose for the summary.

Replace aggregation_type with the type of summary like terms, avg, max, or sum.

Examples
This sums up all the values in the price field.
Elasticsearch
{
  "aggs": {
    "total_sales": {
      "sum": {
        "field": "price"
      }
    }
  }
}
This calculates the average of the age field.
Elasticsearch
{
  "aggs": {
    "average_age": {
      "avg": {
        "field": "age"
      }
    }
  }
}
This groups data by categories and counts how many items are in each.
Elasticsearch
{
  "aggs": {
    "top_categories": {
      "terms": {
        "field": "category.keyword"
      }
    }
  }
}
Sample Program

This query finds the highest price in the data. The "size": 0 means we only want the summary, not the actual items.

Elasticsearch
{
  "size": 0,
  "aggs": {
    "max_price": {
      "max": {
        "field": "price"
      }
    }
  }
}
OutputSuccess
Important Notes

Aggregations do not change your data, they just summarize it.

You can combine many aggregations to get different summaries in one query.

Use "size": 0 to skip returning actual documents when you only want summaries.

Summary

Aggregations help you get quick summaries like totals, averages, and counts.

They are useful to understand large amounts of data easily.

You write aggregations inside the aggs part of your Elasticsearch query.