Is it possible to produce a script field for use in an Aggregation

Yes, I am aware of the performance implications, but the search is not time sensitive. I have considered those options but unfortunately they are not available.
I want to know how I could go about coding this requirement. I have tried using a Java Native Plugin but I'm not certain what interface to implement.
Thanks.