Elasticsearch lets you compute metrics on a bucket from the fields of the hits in the document. But what if I want to compute a metric from other metrics on the aggregation.
For example, I'm storing data on advertising campaigns in Elasticsearch. I aggregate documents by date, and add daily_spend
and daily_clicks
metrics (by summing up the spend
and clicks
metrics on the documents in each day
bucket). Now I want to add a cpm
metric, which is computed from the daily_spend
and daily_clicks
metrics. How do I do that?
Here's my query:
{
"query": {
"filtered": {
"filter": {
"term": {
"campaign_id": 1234
}
}
}
},
"aggs": {
"day": {
"terms": {
"field": "day"
},
"aggs": {
"daily_spend": {
"sum": {
"field": "spend"
}
},
"daily_clicks": {
"sum": {
"field": "clicks"
}
},
"cpc": {
//daily_spend/daily_clicks
}
}
}
}
}