# Custom aggregator logic when gathering data from multiple indexes

**URL:** <https://discuss.elastic.co/t/custom-aggregator-logic-when-gathering-data-from-multiple-indexes/49238>\
**Category:** Elasticsearch\
**Created:** [May 4, 2016, 11:52pm UTC](https://discuss.elastic.co/t/custom-aggregator-logic-when-gathering-data-from-multiple-indexes/49238 "2016-05-04T23:52:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddorian43](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ddorian43/32/36093_2.png) [@ddorian43](https://discuss.elastic.co/u/ddorian43)\
**Post date:** [May 4, 2016, 11:52pm UTC](https://discuss.elastic.co/t/custom-aggregator-logic-when-gathering-data-from-multiple-indexes/49238/1 "2016-05-04T23:52:51Z")

</div>

Hi!,

I'm doing a "terms aggregation" + "top-bucket" subaggregation on each bucket. And I have several indexes: index1, index2, index3, index4 etc.

And I want to get x=20 "buckets" on the top aggregation.

Can I(how?) modify the aggregation functioning, so I can do:

```
aggregate on index1
if buckets < 20:
    aggregate on index2 + merge
if buckets <20:
    aggregate on index3 + merge
if buckets < 20:
    aggregate on index4 + merge
etc

```

I understand I can do this on the client, with custom merging logic, but maybe it's faster/better/possible? to do it in the server ?

Thank You

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 6, 2016, 12:26am UTC](https://discuss.elastic.co/t/custom-aggregator-logic-when-gathering-data-from-multiple-indexes/49238/2 "2016-05-06T00:26:17Z")

</div>

You may be able to do this with a scripted agg, but it's not going to amazingly fast.

---

<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 5, 2017, 10:53pm UTC](https://discuss.elastic.co/t/custom-aggregator-logic-when-gathering-data-from-multiple-indexes/49238/3 "2017-07-05T22:53:29Z")

</div>

Closed on July 5, 2017, 10:53pm UTC


