# Query within a query

**URL:** <https://discuss.elastic.co/t/query-within-a-query/38742>\
**Category:** Elasticsearch\
**Created:** [January 8, 2016, 3:36pm UTC](https://discuss.elastic.co/t/query-within-a-query/38742 "2016-01-08T15:36:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sundar\_V](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sundar_v/32/6928_2.png) [@Sundar\_V](https://discuss.elastic.co/u/Sundar_V)\
**Post date:** [January 8, 2016, 3:36pm UTC](https://discuss.elastic.co/t/query-within-a-query/38742/1 "2016-01-08T15:36:16Z")

</div>

I’m using parent-child relationship.  
Before 2.0.0, the filtered query used to first filter the query and then run the second query. So this code was working

{  
"query": {  
"filtered": {  
"filter": {  
"term": {  
"kind": "rock"  
}  
},  
"query": {  
"has\_child": {  
"type": "song",  
"score\_mode": "sum",  
"query": {  
"match\_all": {}  
}  
}  
}  
}  
}  
}

However, now since filtered query is deprecated, I’m trying with the bool query and this one fails

{  
"query": {  
"bool": {  
"must": {  
"match\_all": {}  
},  
"filter": {  
"term": {  
"kind": "rock"  
}  
}  
},  
"has\_child": {  
"type": "song",  
"score\_mode": "sum",  
"query": {  
"match\_all": {}  
}  
}  
}  
}

How to write this?

---

<div class="post-metadata">

**Author:** ![shaunak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shaunak/32/6643_2.png) [@shaunak](https://discuss.elastic.co/u/shaunak)\
**Post date:** [January 8, 2016, 4:08pm UTC](https://discuss.elastic.co/t/query-within-a-query/38742/2 "2016-01-08T16:08:13Z")

</div>

Try this instead:

```
{
  "query": {
    "bool": {
      "must": [
        {
          "has_child": {
            "type": "song",
            "score_mode": "sum", 
            "query": {
              "match_all": {}
            }
          }
        }
      ],
      "filter": {
        "term": {
          "kind": "rock"
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![Sundar\_V](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sundar_v/32/6928_2.png) [@Sundar\_V](https://discuss.elastic.co/u/Sundar_V)\
**Post date:** [January 10, 2016, 12:16am UTC](https://discuss.elastic.co/t/query-within-a-query/38742/3 "2016-01-10T00:16:56Z")

</div>

Thanks @shaunak. That works!

---

<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:25pm UTC](https://discuss.elastic.co/t/query-within-a-query/38742/4 "2017-07-05T23:25:44Z")

</div>


