# Weighted average scripted metric usage

**URL:** <https://discuss.elastic.co/t/weighted-average-scripted-metric-usage/21566>\
**Category:** Elasticsearch\
**Created:** [January 9, 2015, 10:42pm UTC](https://discuss.elastic.co/t/weighted-average-scripted-metric-usage/21566 "2015-01-09T22:42:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kallin\_Nagelberg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kallin_nagelberg/32/1007_2.png) [@Kallin\_Nagelberg](https://discuss.elastic.co/u/Kallin_Nagelberg)\
**Post date:** [January 9, 2015, 10:42pm UTC](https://discuss.elastic.co/t/weighted-average-scripted-metric-usage/21566/1 "2015-01-09T22:42:42Z")

</div>

The current 1.4 docs mention that the scripted\_metric aggregation is  
experimental, and to share our usages. I've found a really great use on our  
project so I thought I'd share!

While the 'stats' metric provides great data like 'sum' and 'average', we  
needed to calculate a weighted average. In this case, the weighting field  
is 'principal\_amount', and the field we'd like the metric on is 'rate'. A  
CURL for this agg is here:

curl -XGET "[http://localhost:9200/my\_index/\_search](http://localhost:9200/my_index/_search)" -d'  
{  
"size": 0,  
"aggs": {  
"test\_script": {  
"scripted\_metric": {  
"init\_script": "\_agg["numerator"] = []; \_agg["denominator"] =  
[];",  
"map\_script": "\_agg.numerator \<\< (doc["principal\_amount"].value \*  
doc["rate"].value); \_agg.denominator \<\< doc["principal\_amount"].value",  
"combine\_script" : "num\_sum = 0; den\_sum = 0; for (t in  
\_agg.numerator) { num\_sum += t }; for (t in \_agg.denominator) { den\_sum +=  
t };return [num\_sum: num\_sum, den\_sum: den\_sum]",  
"reduce\_script" : "num\_sum = 0; den\_sum=0; for (a in \_aggs) {  
num\_sum += a.num\_sum; den\_sum += a.den\_sum; }; return num\_sum/den\_sum"  
}  
}  
}  
}'

For reference, a weighted average is defined here (it's pretty simple):

> **[Weighted Average](https://www.investopedia.com/terms/w/weightedaverage.asp)**
>
> An average in which each quantity to be averaged is assigned a weight. These weightings determine the relative importance of each quantity on the average. Weightings are the equivalent of having that many like items with the same value involved in...

Without this great new aggregation type, I guess I'd have to index the  
product of 'rate' and 'principal\_amount' so that I could run a stats agg on  
that. It would work, but not as clean.

One thing I notice here is that there is quite a bit of redundancy between  
the 'combine' and 'reduce' scripts. I haven't fully explored what the most  
concise representation might look like, but it could be something to think  
about as this aggregation develops.

Thanks for including it!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8dd58a43-df8e-4c7a-9c88-a9ac33055fc4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8dd58a43-df8e-4c7a-9c88-a9ac33055fc4%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bowlby](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@bowlby](https://discuss.elastic.co/u/bowlby)\
**Post date:** [March 12, 2015, 8:14am UTC](https://discuss.elastic.co/t/weighted-average-scripted-metric-usage/21566/2 "2015-03-12T08:14:22Z")

</div>

I have data that has a weighting field and I'd like to visualize the  
weighted average in Kibana4. Is it possible to visualize this query in  
Kibana4?

On Friday, 9 January 2015 23:42:42 UTC+1, Kallin Nagelberg wrote:

> The current 1.4 docs mention that the scripted\_metric aggregation is  
> experimental, and to share our usages. I've found a really great use on our  
> project so I thought I'd share!
> 
> While the 'stats' metric provides great data like 'sum' and 'average', we  
> needed to calculate a weighted average. In this case, the weighting field  
> is 'principal\_amount', and the field we'd like the metric on is 'rate'. A  
> CURL for this agg is here:
> 
> curl -XGET "[http://localhost:9200/my\_index/\_search](http://localhost:9200/my_index/_search)" -d'  
> {  
> "size": 0,  
> "aggs": {  
> "test\_script": {  
> "scripted\_metric": {  
> "init\_script": "\_agg["numerator"] = ; \_agg["denominator"] =  
> ;",  
> "map\_script": "\_agg.numerator \<\< (doc["principal\_amount"].value
> 
> - doc["rate"].value); \_agg.denominator \<\<  
> doc["principal\_amount"].value",  
> "combine\_script" : "num\_sum = 0; den\_sum = 0; for (t in  
> \_agg.numerator) { num\_sum += t }; for (t in \_agg.denominator) { den\_sum +=  
> t };return [num\_sum: num\_sum, den\_sum: den\_sum]",  
> "reduce\_script" : "num\_sum = 0; den\_sum=0; for (a in \_aggs) {  
> num\_sum += a.num\_sum; den\_sum += a.den\_sum; }; return num\_sum/den\_sum"  
> }  
> }  
> }  
> }'
> 
> For reference, a weighted average is defined here (it's pretty simple):  
> [Weighted Average: What Is It, How Is It Calculated and Used?](http://www.investopedia.com/terms/w/weightedaverage.asp)
> 
> Without this great new aggregation type, I guess I'd have to index the  
> product of 'rate' and 'principal\_amount' so that I could run a stats agg on  
> that. It would work, but not as clean.
> 
> One thing I notice here is that there is quite a bit of redundancy between  
> the 'combine' and 'reduce' scripts. I haven't fully explored what the most  
> concise representation might look like, but it could be something to think  
> about as this aggregation develops.
> 
> Thanks for including it!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5774a2de-1c4b-4e91-865b-b95522690db7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5774a2de-1c4b-4e91-865b-b95522690db7%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:27am UTC](https://discuss.elastic.co/t/weighted-average-scripted-metric-usage/21566/3 "2017-07-06T00:27:17Z")

</div>


