# Custom Aggregations

**URL:** https://discuss.elastic.co/t/custom-aggregations/15740
**Category:** Elasticsearch
**Created:** [February 11, 2014, 8:41pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740 "2014-02-11T20:41:44Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Justin\_Uang](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@Justin\_Uang](https://discuss.elastic.co/u/Justin_Uang)
#### Post date: [February 11, 2014, 8:41pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/1 "2014-02-11T20:41:44Z")

</div>

Is there any way we can define our own aggregation functions beyond the  
provided metric and bucket aggregations?

Thanks!

Justin

--  
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/90accf78-6540-4126-8268-7f997353ad74%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/90accf78-6540-4126-8268-7f997353ad74%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [February 12, 2014, 1:49pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/2 "2014-02-12T13:49:28Z")

</div>

Hey,

sure, you can write a custom plugin, which implements your custom  
aggregations, if you want to.  
Any specific functionality you are missing (might be interesting to get it  
in the core anyway?).

--Alex

On Tue, Feb 11, 2014 at 9:41 PM, Justin Uang [justin.uang@gmail.com](mailto:justin.uang@gmail.com) wrote:

> Is there any way we can define our own aggregation functions beyond the  
> provided metric and bucket aggregations?
> 
> Thanks!
> 
> Justin
> 
> --  
> 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/90accf78-6540-4126-8268-7f997353ad74%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/90accf78-6540-4126-8268-7f997353ad74%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/CAGCwEM9\_%3DqiV2dBUnMoj4rYkCdbbBCbt6%2Bd5K1w\_xh1dBzagSQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGCwEM9_%3DqiV2dBUnMoj4rYkCdbbBCbt6%2Bd5K1w_xh1dBzagSQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Justin\_Uang](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@Justin\_Uang](https://discuss.elastic.co/u/Justin_Uang)
#### Post date: [February 12, 2014, 4:41pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/3 "2014-02-12T16:41:55Z")

</div>

I'm just interested in the flexibility of this platform. How would I  
implement the custom aggregation?

On Wednesday, February 12, 2014 8:49:28 AM UTC-5, Alexander Reelsen wrote:

> Hey,
> 
> sure, you can write a custom plugin, which implements your custom  
> aggregations, if you want to.  
> Any specific functionality you are missing (might be interesting to get it  
> in the core anyway?).
> 
> --Alex
> 
> On Tue, Feb 11, 2014 at 9:41 PM, Justin Uang \<[justi...@gmail.com](mailto:justi...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Is there any way we can define our own aggregation functions beyond the  
> > provided metric and bucket aggregations?
> > 
> > Thanks!
> > 
> > Justin
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/90accf78-6540-4126-8268-7f997353ad74%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/90accf78-6540-4126-8268-7f997353ad74%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/02905d0a-a7a3-465b-9475-e9dedcefd658%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/02905d0a-a7a3-465b-9475-e9dedcefd658%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![osvaldopina](https://avatars.discourse-cdn.com/v4/letter/o/f04885/32.png) [@osvaldopina](https://discuss.elastic.co/u/osvaldopina)
#### Post date: [February 28, 2014, 5:48pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/4 "2014-02-28T17:48:25Z")

</div>

Hi,

I'm trying to create a new aggregator. I was able to understand the structure needed to create and register a new aggregator but I don't know who to get the document field values inside collect method. How can I do that?

---

<div class="post-metadata">

### Author: ![osvaldopina](https://avatars.discourse-cdn.com/v4/letter/o/f04885/32.png) [@osvaldopina](https://discuss.elastic.co/u/osvaldopina)
#### Post date: [March 1, 2014, 3:38pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/5 "2014-03-01T15:38:25Z")

</div>

Hi,

I'm trying to create a new aggregator plugin. I was able to understand the  
structure needed to create and register my new aggregator but I don't know  
who to get the document field values inside collect method. How can I do  
that?

Em quarta-feira, 12 de fevereiro de 2014 11h49min28s UTC-2, Alexander  
Reelsen escreveu:

> Hey,
> 
> sure, you can write a custom plugin, which implements your custom  
> aggregations, if you want to.  
> Any specific functionality you are missing (might be interesting to get it  
> in the core anyway?).
> 
> --Alex
> 
> On Tue, Feb 11, 2014 at 9:41 PM, Justin Uang \<[justi...@gmail.com](mailto:justi...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Is there any way we can define our own aggregation functions beyond the  
> > provided metric and bucket aggregations?
> > 
> > Thanks!
> > 
> > Justin
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/90accf78-6540-4126-8268-7f997353ad74%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/90accf78-6540-4126-8268-7f997353ad74%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/15e052d8-41d6-48f8-9f29-5482bc4bf8f0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/15e052d8-41d6-48f8-9f29-5482bc4bf8f0%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:19pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/6 "2014-08-09T18:19:51Z")

</div>

Hi Alex,

Is there an updates on this topic? I want to know how easy or how complex  
is it to write a custom aggregator. For example, 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.

In general, I would like to know what kind of custom aggregator is easy to  
write?

Thanks,  
Jin

On Wednesday, February 12, 2014 5:49:28 AM UTC-8, Alexander Reelsen wrote:

> Hey,
> 
> sure, you can write a custom plugin, which implements your custom  
> aggregations, if you want to.  
> Any specific functionality you are missing (might be interesting to get it  
> in the core anyway?).
> 
> --Alex
> 
> On Tue, Feb 11, 2014 at 9:41 PM, Justin Uang \<[justi...@gmail.com](mailto:justi...@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > Is there any way we can define our own aggregation functions beyond the  
> > provided metric and bucket aggregations?
> > 
> > Thanks!
> > 
> > Justin
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/90accf78-6540-4126-8268-7f997353ad74%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/90accf78-6540-4126-8268-7f997353ad74%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/b2b8894d-c26d-4787-af4c-c45f793befc6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b2b8894d-c26d-4787-af4c-c45f793befc6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [August 12, 2014, 1:46pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/7 "2014-08-12T13:46:56Z")

</div>

Hi Jin,

It is possible to write a custom aggregation, you can for example look at  
this plugin: [GitHub - algolia/elasticsearch-cardinality-plugin: This plugin extends Elasticsearch providing a new type of aggregation and a REST action to estimate the cardinality (number of uniq terms) of a field.](https://github.com/algolia/elasticsearch-cardinality-plugin)  
that implements a aggregation as a plugin.

If that would work for you, another option would be to contribute this  
aggregation to Elaticsearch so that it would be available out of the box  
(feel free to open an issue on Github and ping me (@jpountz), I'd be happy  
to help).

On Sat, Aug 9, 2014 at 8:19 PM, Jin Huang [huang.jin.f@gmail.com](mailto:huang.jin.f@gmail.com) wrote:

> Hi Alex,
> 
> Is there an updates on this topic? I want to know how easy or how complex  
> is it to write a custom aggregator. For example, 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.
> 
> In general, I would like to know what kind of custom aggregator is easy to  
> write?
> 
> Thanks,  
> Jin
> 
> On Wednesday, February 12, 2014 5:49:28 AM UTC-8, Alexander Reelsen wrote:
> 
> > Hey,
> > 
> > sure, you can write a custom plugin, which implements your custom  
> > aggregations, if you want to.  
> > Any specific functionality you are missing (might be interesting to get  
> > it in the core anyway?).
> > 
> > --Alex
> > 
> > On Tue, Feb 11, 2014 at 9:41 PM, Justin Uang [justi...@gmail.com](mailto:justi...@gmail.com) wrote:
> > 
> > > Is there any way we can define our own aggregation functions beyond the  
> > > provided metric and bucket aggregations?
> > > 
> > > Thanks!
> > > 
> > > Justin
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > > 
> > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > msgid/elasticsearch/90accf78-6540-4126-8268-7f997353ad74%  
> > > [40googlegroups.com](http://40googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > 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/b2b8894d-c26d-4787-af4c-c45f793befc6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b2b8894d-c26d-4787-af4c-c45f793befc6%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/b2b8894d-c26d-4787-af4c-c45f793befc6%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/b2b8894d-c26d-4787-af4c-c45f793befc6%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
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/CAL6Z4j5pEbWQKAptt10jtwOwuLRXJ5SzLmDY2yn5f\_4eqeaY1Q%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5pEbWQKAptt10jtwOwuLRXJ5SzLmDY2yn5f_4eqeaY1Q%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![xam](https://avatars.discourse-cdn.com/v4/letter/x/bb73d2/32.png) [@xam](https://discuss.elastic.co/u/xam)
#### Post date: [April 7, 2017, 3:03pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/8 "2017-04-07T15:03:40Z")

</div>

Hi,

I need a n dimension adjacency matrix.  
For example, if I have 3 filters A, B and C , I need to compute  
A&B, B&A, C&A but also A&B&C  
Unfortunatly, the new adjacency matrix in ES works for 2 dimentions.  
I plan to develop a new MultiBucketsAggregation plugin, based on the current adjacency matrix. I now my aggregation will be performant only if I have a few filters, as the number of intersecting buckets will explode for n=3 or more.  
Please let me know if my approach is correct.

Regards,

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [April 7, 2017, 4:08pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/9 "2017-04-07T16:08:05Z")

</div>

It is, I just want to warn you that maintaining an aggregation is a lot of work as we don't guarantee API backward compatibility of the aggregations API. If your use-case is rather generic, maybe open a Github issue to explain it and we might consider supporting it.

---

<div class="post-metadata">

### Author: ![Pratheek](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@Pratheek](https://discuss.elastic.co/u/Pratheek)
#### Post date: [June 19, 2017, 11:47am UTC](https://discuss.elastic.co/t/custom-aggregations/15740/10 "2017-06-19T11:47:52Z")

</div>

Hi can you please let me know the steps on how to create a custom aggregation.I found some template in src folder in kibana,but where does the logic exist? (for example to perform addition,how does it take two input)

---

<div class="post-metadata">

### Author: ![Vikas\_Kumar1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikas_kumar1/32/47867_2.png) [@Vikas\_Kumar1](https://discuss.elastic.co/u/Vikas_Kumar1)
#### Post date: [November 1, 2017, 12:09pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/11 "2017-11-01T12:09:37Z")

</div>

Hi

Can you share any link which can help to write a custom aggregation plugin. I don't even know how to start.Please help

---

<div class="post-metadata">

### Author: ![ciupam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ciupam/32/101728_2.png) [@ciupam](https://discuss.elastic.co/u/ciupam)
#### Post date: [February 11, 2022, 3:58pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/12 "2022-02-11T15:58:01Z")

</div>

Hi, is there any way to use sum aggregation with multiple fields? I don't mean having two (or more) seperate sum aggregations as it takes twice the time as the single aggregation. If that's not possible with the sum aggregation is there any way to write a custom aggregation which resolves this issue (basically sums all the fields and returns an object)?

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [February 11, 2022, 4:27pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/13 "2022-02-11T16:27:50Z")

</div>

You may use sum aggregation for each fields, and use [bucket script aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html) to sum all.  
It will take as long as the number of fields to be aggregated, but it should be faster than creating a custom script.

---

<div class="post-metadata">

### Author: ![ciupam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ciupam/32/101728_2.png) [@ciupam](https://discuss.elastic.co/u/ciupam)
#### Post date: [February 12, 2022, 10:33am UTC](https://discuss.elastic.co/t/custom-aggregations/15740/14 "2022-02-12T10:33:17Z")

</div>

I was rather thinking of using one sum aggregation and get the same result as I'd get using multiple sum aggregations (I don't need summing up all the fields, I just need something less complex than iterating over each doc with each aggregation - to be precise I'm talking about 50 fields where each sum aggregation takes about 2-3s).

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [February 12, 2022, 12:17pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/15 "2022-02-12T12:17:47Z")

</div>

You could try [scripted metric aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html). Though in general, scripts

> can’t use Elasticsearch’s index structures or related optimizations. This relationship can sometimes result in slower search speeds.  
> [Scripts, caching, and search speed | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/scripts-and-search-speed.html)

it is worth trying and do performance test.

Depending on your use case, you may be able to speed up the process by periodically aggregating the data using [transform](https://www.elastic.co/guide/en/elasticsearch/reference/current/transforms.html), or by using the[ingest pipeline](https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html) to pre-calculate the total value of the fields for each document at the time of ingest. Why don't you create a new post with your specific situation and the queries you want (query frequency, filtering conditions...) and discuss it with us?

---

<div class="post-metadata">

### Author: ![ciupam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ciupam/32/101728_2.png) [@ciupam](https://discuss.elastic.co/u/ciupam)
#### Post date: [February 14, 2022, 2:40pm UTC](https://discuss.elastic.co/t/custom-aggregations/15740/16 "2022-02-14T14:40:23Z")

</div>

Thx for the answer, tried scripted metric aggregation - it takes twice as long to complete compared to multiple sum aggregations. I might come back some other time with the new post as transforming or pre-calculating values is sth that won't work for us.

---

<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: [November 4, 2022, 4:01am UTC](https://discuss.elastic.co/t/custom-aggregations/15740/17 "2022-11-04T04:01:25Z")

</div>


