# Match\_query inside script?

**URL:** <https://discuss.elastic.co/t/match-query-inside-script/40359>\
**Category:** Elasticsearch\
**Created:** [January 28, 2016, 12:45pm UTC](https://discuss.elastic.co/t/match-query-inside-script/40359 "2016-01-28T12:45:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serg\_Pilipenko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/serg_pilipenko/32/13833_2.png) [@Serg\_Pilipenko](https://discuss.elastic.co/u/Serg_Pilipenko)\
**Post date:** [January 28, 2016, 12:45pm UTC](https://discuss.elastic.co/t/match-query-inside-script/40359/1 "2016-01-28T12:45:48Z")

</div>

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](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](http://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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:20pm UTC](https://discuss.elastic.co/t/match-query-inside-script/40359/2 "2017-07-05T23:20:48Z")

</div>


