# Metrics Aggregations: custom stats functions, script stats, user-defined metrics, custom metrics

**URL:** <https://discuss.elastic.co/t/metrics-aggregations-custom-stats-functions-script-stats-user-defined-metrics-custom-metrics/19177>\
**Category:** Elasticsearch\
**Created:** [August 9, 2014, 6:30pm UTC](https://discuss.elastic.co/t/metrics-aggregations-custom-stats-functions-script-stats-user-defined-metrics-custom-metrics/19177 "2014-08-09T18:30:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jin\_Huang](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Jin\_Huang](https://discuss.elastic.co/u/Jin_Huang)\
**Post date:** [August 9, 2014, 6:30pm UTC](https://discuss.elastic.co/t/metrics-aggregations-custom-stats-functions-script-stats-user-defined-metrics-custom-metrics/19177/1 "2014-08-09T18:30:49Z")

</div>

Hi people,

Can someone provide some detailed information on how to extend the sets of  
currently available metrics in the aggregation framework?

For now, Elasticsearch provides support for the metrics: count, min, max,  
avg, sum, sum\_of\_squares, variance, and std\_deviation. How can I define my  
own?

I understand that there might some hard constraints on the type of metrics  
could be defined. But it would be nice if someone can provide a simple  
example of how to write my own.

Here is a couple of suggested examples that I would want to know how to  
write:

1. I want to calculate all the n-th central moments, with n=2,3,4,..10. By  
the way, Elasticsearch already provides the 2nd central moment, i.e. square  
of the standard deviation.
2. I want to get the second order statics. That is, I want to the second  
smallest number.

In general, I would like to know what kind of custom aggregator is easy to  
write? If the above examples require a deep dive into some of the source  
codes, please DO! I don't mind if the answer runs 10 pages long as long as  
someone can provide me with some pointers on how to get started.

Thanks,  
Jin

--  
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/814573e2-50e8-4dbd-ba4a-192c932a03fc%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/814573e2-50e8-4dbd-ba4a-192c932a03fc%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, 1:09am UTC](https://discuss.elastic.co/t/metrics-aggregations-custom-stats-functions-script-stats-user-defined-metrics-custom-metrics/19177/2 "2017-07-06T01:09:47Z")

</div>


