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"; -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):
> 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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elasticsearch/5774a2de-1c4b-4e91-865b-b95522690db7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to