Metric Aggregations in Elasticsearch
Introduction
Elasticsearch provides powerful aggregation capabilities that enable you to analyze your data and extract valuable insights. Metric aggregations are a specific type of aggregation focusing on numerical data. They allow you to perform calculations such as averages, sums, and minimum/maximum values on your data.
Types of Metric Aggregations
There are several types of metric aggregations in Elasticsearch:
- Avg Aggregation: Calculates the average of numeric values.
- Sum Aggregation: Computes the sum of numeric values.
- Min Aggregation: Finds the minimum value in numeric fields.
- Max Aggregation: Finds the maximum value in numeric fields.
- Stats Aggregation: Provides a summary of statistics, including count, min, max, average, and sum.
- Extended Stats Aggregation: Offers extended statistics such as variance and standard deviation, in addition to the standard stats.
- Value Count Aggregation: Counts the number of values in a dataset.
- Percentiles Aggregation: Computes percentiles based on numeric values.
- Cardinality Aggregation: Estimates the cardinality (i.e., the count of distinct values).
Examples
Average Aggregation
This example demonstrates how to calculate the average value of a field.
{
"aggs": {
"average_price": {
"avg": {
"field": "price"
}
}
}
}
{
"aggregations": {
"average_price": {
"value": 25.0
}
}
}
Sum Aggregation
This example shows how to calculate the sum of numeric values in a field.
{
"aggs": {
"total_sales": {
"sum": {
"field": "sales"
}
}
}
}
{
"aggregations": {
"total_sales": {
"value": 1500.0
}
}
}
Min Aggregation
This example demonstrates how to find the minimum value of a field.
{
"aggs": {
"min_price": {
"min": {
"field": "price"
}
}
}
}
{
"aggregations": {
"min_price": {
"value": 10.0
}
}
}
Max Aggregation
This example shows how to find the maximum value of a field.
{
"aggs": {
"max_price": {
"max": {
"field": "price"
}
}
}
}
{
"aggregations": {
"max_price": {
"value": 50.0
}
}
}
Stats Aggregation
This example provides a summary of statistics for a numeric field.
{
"aggs": {
"price_stats": {
"stats": {
"field": "price"
}
}
}
}
{
"aggregations": {
"price_stats": {
"count": 10,
"min": 10.0,
"max": 50.0,
"avg": 25.0,
"sum": 250.0
}
}
}
Extended Stats Aggregation
This example offers extended statistics for a numeric field.
{
"aggs": {
"price_extended_stats": {
"extended_stats": {
"field": "price"
}
}
}
}
{
"aggregations": {
"price_extended_stats": {
"count": 10,
"min": 10.0,
"max": 50.0,
"avg": 25.0,
"sum": 250.0,
"sum_of_squares": 12500.0,
"variance": 200.0,
"std_deviation": 14.142135623730951
}
}
}
Value Count Aggregation
This example counts the number of values in a field.
{
"aggs": {
"value_count": {
"value_count": {
"field": "price"
}
}
}
}
{
"aggregations": {
"value_count": {
"value": 10
}
}
}
Percentiles Aggregation
This example computes percentiles for numeric values in a field.
{
"aggs": {
"price_percentiles": {
"percentiles": {
"field": "price"
}
}
}
}
{
"aggregations": {
"price_percentiles": {
"values": {
"1.0": 12.0,
"5.0": 15.0,
"25.0": 20.0,
"50.0": 25.0,
"75.0": 30.0,
"95.0": 45.0,
"99.0": 50.0
}
}
}
}
Cardinality Aggregation
This example estimates the cardinality (i.e., the count of distinct values) of a field.
{
"aggs": {
"unique_prices": {
"cardinality": {
"field": "price"
}
}
}
}
{
"aggregations": {
"unique_prices": {
"value": 7
}
}
}
