0
0
Elasticsearchquery~5 mins

Stats and extended stats in Elasticsearch

Choose your learning style9 modes available
Introduction

Stats and extended stats help you quickly get useful numbers about your data, like averages and totals, without writing complex code.

You want to find the average price of products in your store.
You need to know the minimum and maximum values in a dataset, like temperatures recorded.
You want to calculate the sum of sales over a period.
You want to get more details like variance or standard deviation to understand data spread.
You want to quickly get count, sum, min, max, and average in one request.
Syntax
Elasticsearch
{
  "aggs": {
    "stats_name": {
      "stats": {
        "field": "field_name"
      }
    },
    "extended_stats_name": {
      "extended_stats": {
        "field": "field_name"
      }
    }
  }
}

Use stats to get count, min, max, avg, and sum.

Use extended_stats to get all stats plus variance, std deviation, and sum of squares.

Examples
This example gets count, min, max, avg, and sum of the price field.
Elasticsearch
{
  "aggs": {
    "price_stats": {
      "stats": {
        "field": "price"
      }
    }
  }
}
This example gets all stats plus variance and standard deviation for the price field.
Elasticsearch
{
  "aggs": {
    "price_extended_stats": {
      "extended_stats": {
        "field": "price"
      }
    }
  }
}
Sample Program

This query asks Elasticsearch to return statistics about the sales field. It returns simple stats and extended stats in one request. The "size": 0 means we don't want actual documents, just the stats.

Elasticsearch
{
  "size": 0,
  "aggs": {
    "sales_stats": {
      "stats": {
        "field": "sales"
      }
    },
    "sales_extended_stats": {
      "extended_stats": {
        "field": "sales"
      }
    }
  }
}
OutputSuccess
Important Notes

Stats aggregation is fast and useful for quick summaries.

Extended stats give more insight but take slightly more processing.

If your field has missing values, those documents are ignored in stats.

Summary

Stats aggregation gives count, min, max, avg, and sum.

Extended stats add variance, standard deviation, and sum of squares.

Use these to understand your data quickly without extra calculations.