Just to drive the point home, let’s make one final modification to our example before moving on to new topics. Let’s add two metrics to calculate the min and max price for each make:
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": { "avg": { "field": "price" }
},
"make" : {
"terms" : {
"field" : "make"
},
"aggs" : { (1)
"min_price" : { "min": { "field": "price"} }, (2)
"max_price" : { "max": { "field": "price"} } (3)
}
}
}
}
}
}
-
We need to add another
aggs
level for nesting. -
Then we include a
min
metric. -
And a
max
metric.
Which gives us the following output (again, truncated):
{
...
"aggregations": {
"colors": {
"buckets": [
{
"key": "red",
"doc_count": 4,
"make": {
"buckets": [
{
"key": "honda",
"doc_count": 3,
"min_price": {
"value": 10000 (1)
},
"max_price": {
"value": 20000 (1)
}
},
{
"key": "bmw",
"doc_count": 1,
"min_price": {
"value": 80000
},
"max_price": {
"value": 80000
}
}
]
},
"avg_price": {
"value": 32500
}
},
...
-
The
min
andmax
metrics that we added now appear under eachmake
With those two buckets, we’ve expanded the information derived from this query to include the following:
-
There are four red cars.
-
The average price of a red car is $32,500.
-
Three of the red cars are made by Honda, and one is a BMW.
-
The cheapest red Honda is $10,000.
-
The most expensive red Honda is $20,000.