How to write script

I am trying to store the results of elasticsearch aggregation in a new index. Now my question is, how to write a script to index the results of an aggregation?
Could anyone give a example showing me how to write a script for this?