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

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

Bucket aggregations support scripts where the script returns the bucket value for each doc. Eg for terms aggregation. Does that satisfy your need or can you be more specific in what you are trying to achieve?

@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