# Run a subquery for each of the filtered documents

**URL:** <https://discuss.elastic.co/t/run-a-subquery-for-each-of-the-filtered-documents/237925>\
**Category:** Elasticsearch\
**Created:** [June 20, 2020, 4:12pm UTC](https://discuss.elastic.co/t/run-a-subquery-for-each-of-the-filtered-documents/237925 "2020-06-20T16:12:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kamboh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kamboh/32/65722_2.png) [@kamboh](https://discuss.elastic.co/u/kamboh)\
**Post date:** [June 20, 2020, 4:12pm UTC](https://discuss.elastic.co/t/run-a-subquery-for-each-of-the-filtered-documents/237925/1 "2020-06-20T16:12:48Z")

</div>

I have an index named employees with the following structure:

```auto
{
id: integer,
name: text,
age: integer,
cityId: integer,
resumeText: text <--------- parsed resume text
}

```

I want to search employees with certain criteria e.g having age \> 40, resumeText contains a specific skill or employee belongs to a certain city etc, and have the following query for so far requirement:

```auto
{
query:{
bool:{
should:[
{
term:{
cityId:2990
},
{
match:{
resumeText:"marketing"
},
{
match:{
resumeText:"critical thinking"
}}}
],
filter:{
range:{
age:{
gte:40
}}}}}
}

```

This gives me expected results but i want to know also among the returned documents/employees which are the ones whose resumeText contains the mentioned skills. e.g in the response, I want to get documents having mentioned that this document had matched "critical thinking" , this employee had matched both the skills and this employee didn't match any skills (as it was returned based on other filters)

What changes do i need to do to get the desired results:

- can aggregation help?
- can we rum a script for EACH filtered document to compute desired result (sub query for each document)?
- any other approach?

---

<div class="post-metadata">

**Author:** ![Vinayak\_Sapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinayak_sapre/32/45939_2.png) [@Vinayak\_Sapre](https://discuss.elastic.co/u/Vinayak_Sapre)\
**Post date:** [July 17, 2020, 8:50am UTC](https://discuss.elastic.co/t/run-a-subquery-for-each-of-the-filtered-documents/237925/2 "2020-07-17T08:50:50Z")

</div>

@kamboh  
Have you looked at named queries? [https://www.elastic.co/guide/en/elasticsearch/reference/7.8/search-request-body.html#request-body-search-queries-and-filters](https://www.elastic.co/guide/en/elasticsearch/reference/7.8/search-request-body.html#request-body-search-queries-and-filters)

---

<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:** [August 14, 2020, 8:51am UTC](https://discuss.elastic.co/t/run-a-subquery-for-each-of-the-filtered-documents/237925/3 "2020-08-14T08:51:03Z")

</div>

Closed on August 14, 2020, 8:51am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
