# Nested query mixed with parent query

**URL:** https://discuss.elastic.co/t/nested-query-mixed-with-parent-query/165666
**Category:** Elasticsearch
**Created:** [January 24, 2019, 9:13pm UTC](https://discuss.elastic.co/t/nested-query-mixed-with-parent-query/165666 "2019-01-24T21:13:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jenssc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenssc/32/77950_2.png) [@jenssc](https://discuss.elastic.co/u/jenssc)
#### Post date: [January 24, 2019, 9:13pm UTC](https://discuss.elastic.co/t/nested-query-mixed-with-parent-query/165666/1 "2019-01-24T21:13:06Z")

</div>

Hi  
I'm trying to do a nested query in combination with query on fields in the parent. But how do I figure out if a match hits the parent field in the same time as I get an inner hit?

Best regards  
Jens Christiansen

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [January 27, 2019, 3:00pm UTC](https://discuss.elastic.co/t/nested-query-mixed-with-parent-query/165666/2 "2019-01-27T15:00:03Z")

</div>

You could use a [bool query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) with a must clause that has two queries: the nested query and the query on the parent field. Only documents that match both queries will be returned as a hit.

```auto
GET my_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "path_to_nested_doc",
            "query": { YOUR_NESTED_QUERY }
          }
        },
        {
          YOUR_QUERY_ON_THE_PARENT_FIELD
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![jenssc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenssc/32/77950_2.png) [@jenssc](https://discuss.elastic.co/u/jenssc)
#### Post date: [February 2, 2019, 12:11pm UTC](https://discuss.elastic.co/t/nested-query-mixed-with-parent-query/165666/3 "2019-02-02T12:11:27Z")

</div>

Dear Abdon  
Great 🙂

Thanks for your solution and nice to hear from you again.

---

<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: [March 2, 2019, 12:11pm UTC](https://discuss.elastic.co/t/nested-query-mixed-with-parent-query/165666/4 "2019-03-02T12:11:29Z")

</div>

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