# Is it possible to write a ES script plugin that processes all document belongs to each bucket?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-write-a-es-script-plugin-that-processes-all-document-belongs-to-each-bucket/207188>\
**Category:** Elasticsearch\
**Created:** [November 8, 2019, 11:09pm UTC](https://discuss.elastic.co/t/is-it-possible-to-write-a-es-script-plugin-that-processes-all-document-belongs-to-each-bucket/207188 "2019-11-08T23:09:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wenhoujx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wenhoujx/32/73759_2.png) [@wenhoujx](https://discuss.elastic.co/u/wenhoujx)\
**Post date:** [November 8, 2019, 11:09pm UTC](https://discuss.elastic.co/t/is-it-possible-to-write-a-es-script-plugin-that-processes-all-document-belongs-to-each-bucket/207188/1 "2019-11-08T23:09:57Z")

</div>

Similar to the ES scripted metric aggregation that processes documents in each bucket, my use case is bit complicated and requires flexibility, i am wondering if it's possible to write a ES script plugin that summaries documents in a bucket, if so, can you point me to examples ?

Thanks

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [November 18, 2019, 8:28pm UTC](https://discuss.elastic.co/t/is-it-possible-to-write-a-es-script-plugin-that-processes-all-document-belongs-to-each-bucket/207188/2 "2019-11-18T20:28:34Z")

</div>

Bucket aggregations support scripts where the script returns the bucket value for each doc. Eg for [`terms` aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script). Does that satisfy your need or can you be more specific in what you are trying to achieve?

---

<div class="post-metadata">

**Author:** ![wenhoujx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wenhoujx/32/73759_2.png) [@wenhoujx](https://discuss.elastic.co/u/wenhoujx)\
**Post date:** [November 18, 2019, 10:33pm UTC](https://discuss.elastic.co/t/is-it-possible-to-write-a-es-script-plugin-that-processes-all-document-belongs-to-each-bucket/207188/3 "2019-11-18T22:33:28Z")

</div>

@rjernst i checked out the terms aggregation script, it seems the script only has access to the bucket key value? i want to collect from documents in the same bucket, another field value into an array .

Also, any chance you know how to access the number of docs in each bucket from scripting?  
the use case is i need to collect exactly one field value per doc, and it would be with allocation to do `new ArrayList<>(<hits in this bucket>)`.

thanks

---

<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:** [December 16, 2019, 10:33pm UTC](https://discuss.elastic.co/t/is-it-possible-to-write-a-es-script-plugin-that-processes-all-document-belongs-to-each-bucket/207188/4 "2019-12-16T22:33:33Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
