Match_query inside script?

Hey,

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: https://gist.github.com/serj-p/c4fcc9810b3b627de294
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": {
                          "organizations.name": "stan"
                        }
                      }

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

           {
                "filter": [
                    "lowercase",
                    "standard",
                    "trim",
                    "asciifolding",
                ],
                "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