Match_query inside script?


I have a quite complex aggregation, its complexity is caused by absence of has_parent aggregation.
Since so it's implemented using groovy. The only problem I have is to filter docs being counted in the aggregation.

Aggregation looks like this:
the aim of this aggregation is to build the top of universities contacts are graduated from.
A contact doc has child docs, which are facebook profiles.
Last ones have nested fields representing universities, that's why I access _source field.

As you probably can see, I perform filtering in the beginning :

                        "match_phrase_prefix": {
                          "": "stan"

to exclude contacts not having relevant docs. "" is analyzed as

                "filter": [
                "type": "custom",
                "tokenizer": "standard"

the text being appleid to filter this field is being analyzed in the same way.
I found it very difficult to do the same processing of a field value and a filtering text inside of the script which should filter particular nested docs. That's why I am looking for some possibility to access ES API from the script to do so.

Thanks in advance for any advice