Calculate top common fields for matched documents

Hello Elastic Search experts,

I want to calculate top common fields, and their values for matched documents for a query, other than return the matched document itself. I want to calculate top common fields by a customized function (could be implemented by Java/Python or Scala).

The question is, is it possible to plug-in the customized function inside ElasticSearch cluster? Which has the benefit of saving bandwidth to transfer all documents back (which is huge number), v.s. only return top common fields (which size is much smaller).

If doable, any samples are appreciated.

thanks in advance,
Lin