1
votes

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
                }
            }
        }
    }
}
1
Hey got any solution for this problem >ted

1 Answers

0
votes

There is not a way to pull the data from the other two aggregations and do additional operations on them.

Your best bet is to do that division operation on the value of the daily_spend and daily_clicks aggregations after you have retrieved the data from the query.