# Scriptable Metric Aggregation #5923

Closed
opened this Issue Apr 23, 2014 · 3 comments

Projects
None yet
4 participants
Contributor

### mattweber commented Apr 23, 2014

 It would be great if we could have a script-based metric aggregation that can refer to other metric aggregations from within the script. For example, given the data and query given in this gist: https://gist.github.com/mattweber/71033b1bf2ebed1afd8e It would like to be able to do something like the following for a "profit" calculation: ``````"cost_agg": { "filter": { "term": { "type": "cost" } }, "aggs": { "cost_sum_agg": { "sum": { "field": "value" } } } }, "sale_agg": { "filter": { "term": { "type": "sale" } }, "aggs": { "sale_sum_agg": { "sum": { "field": "value" } } } }, "profit_agg": { "metric_script": { "script": "sale_agg>sale_sum_agg - cost_agg>cost_sum_agg" } } `````` So using the data from the gist we would get a response such as: ``````{ "key" : "foobar", "doc_count" : 4, "sale_agg" : { "doc_count" : 2, "sale_sum_agg" : { "value" : 65.48 } }, "cost_agg" : { "doc_count" : 2, "cost_sum_agg" : { "value" : 24.68 } }, "profit_agg": { "value": 40.80 } } `````` I believe something like this would be possible given the fact that the the `order` parameter of a Terms Aggregation can refer to metric sub-aggregation values. The logic and retrieving the values should be very similar. Exposing them to a script might be the hard part. I will look into this further but I wanted to open the issue in case you are working on similar functionality or have any suggestions for me.
Contributor

### uboness commented Apr 28, 2014

 @mattweber The one thing we have right now in the aggs framework is that each agg is agnostic of other aggs and we'd like to keep it this way. We do have plans to come out with a scriptable metrics agg that will enable you to compute the profit (look at it as a scriptable map/reduce logic)... we'll create a public issue for it soon... I'll keep this one open and once we have the public issue for it will link the two
Contributor Author

### mattweber commented Apr 28, 2014

 That sounds great, thanks for the update!
Contributor Author

### mattweber commented Jul 23, 2014

 @uboness Just pinging to see if there has been any progress toward functionality that would allow this type of aggregation?

Merged

### colings86 added a commit to colings86/elasticsearch that referenced this issue Aug 20, 2014

``` Aggregations: Scriptable Metrics Aggregation ```
```A metrics aggregation which runs specified scripts at the init, collect, combine, and reduce phases

Closes elastic#5923```
``` 7f943f0 ```

### colings86 added a commit that referenced this issue Aug 20, 2014

``` Aggregations: Scriptable Metrics Aggregation ```
```A metrics aggregation which runs specified scripts at the init, collect, combine, and reduce phases

Closes #5923```
``` bd4a950 ```

### colings86 added a commit that referenced this issue Sep 8, 2014

``` Aggregations: Scriptable Metrics Aggregation ```
```A metrics aggregation which runs specified scripts at the init, collect, combine, and reduce phases

Closes #5923```
``` c754825 ```

Open