Question:
- How do I get the average sales the clerk's are doing in each region and store?
- How do I not include
clerkSums
in the responses? I only care aboutsalesStats
andcostStats
?
Imagine I have a single index with 2 number columns, sales
and cost
. And 3 string columns, region
, store
, clerk
.
How would I get the average
and max
for both number values, sales
and cost
, for each clerk
? How do I get those values by region
? Or region
and store
?
For clerk
stats:
It seems like I need to create a terms bucket for the clerks with their sales
and cost
sums. Then do a stats_bucket
query over that bucket for each of the sales
and cost
numbers.
"aggregations": {
"clerkStats": {
"terms": { "field": "clerk" },
"aggs": {
"sales": { "sum": { "field": "sales" } },
"cost": { "sum": { "field": "cost" } }
}
},
"salesStats": {
"stats_bucket": { "buckets_path": "clerkStats>sales" }
},
"costStats": {
"stats_bucket": { "buckets_path": "clerkStats>cost" }
}
}
This gives the following useful properties
-
salesStats.avg
would have the averagesales
for allclerk
s -
salesStats.max
would have the maxsales
for allclerk
s -
costStats.avg
would have the averagecosts
for allclerk
s -
costStats.max
would have the maxcosts
for allclerk
s
To wrap that by region
it would be
"aggregations": {
"region": {
"terms": { "field": "region" },
"aggs": {
"aggs": {
"clerkSums": {
"terms": { "field": "clerk" },
"aggs": {
"sales": { "sum": { "field": "sales" } },
"cost": { "sum": { "field": "cost" } }
}
},
"salesStats": {
"stats_bucket": { "buckets_path": "clerkSums>sales" }
},
"costStats": {
"stats_bucket": { "buckets_path": "clerkSums>cost" }
}
}
}
}
}
This gives the following useful properties in region buckets
-
salesStats.avg
being the averagesales
for allclerk
s in that region -
salesStats.max
being the maxsales
for allclerk
s in that region -
costStats.avg
being the averagecost
for allclerk
s in that region -
costStats.max
being the maxcost
for allclerk
s in that region
For region
and store
:
Same thing as the region
alone query, but in a composite query with both region and store?
"aggregations": {
"combo": {
"composite": {
"sources": [
{ "region": { "terms": { "field": "region" } } },
{ "store": { "terms": { "field": "store" } } }
]
},
"aggs": {
"aggs": {
"clerkSums": {
"terms": { "field": "clerk" },
"aggs": {
"sales": { "sum": { "field": "sales" } },
"cost": { "sum": { "field": "cost" } }
}
},
"salesStats": {
"stats_bucket": { "buckets_path": "clerkSums>sales" }
},
"costStats": {
"stats_bucket": { "buckets_path": "clerkSums>cost" }
}
}
}
}
}
This does not work. You cannot include the clerkSums inside the composite aggregation.
The only workaround I know of is to do 2 terms aggregations to nest region and store.
Question:
- How do I get the average sales the clerk's are doing in each region and store?
- How do I not include
clerkSums
in the responses? I only care aboutsalesStats
andcostStats
?