# Has Child Query on Nested Object field

**URL:** <https://discuss.elastic.co/t/has-child-query-on-nested-object-field/24736>\
**Category:** Elasticsearch\
**Created:** [July 1, 2015, 2:48pm UTC](https://discuss.elastic.co/t/has-child-query-on-nested-object-field/24736 "2015-07-01T14:48:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bevans88](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@bevans88](https://discuss.elastic.co/u/bevans88)\
**Post date:** [July 1, 2015, 2:48pm UTC](https://discuss.elastic.co/t/has-child-query-on-nested-object-field/24736/1 "2015-07-01T14:48:56Z")

</div>

Hoping someone can help here and tell me if the query I'm attempting to run is possible/supported by Elasticsearch (I've read through the docs and forums and been unable to find any answer).

I have a child document (Child) that has a parent document set on it, with the child document having a number of nested fields that require nested queries to be run. I wish to run a has child query on the parent document, with the inner query on the child document being against a nested field. For example:

```
  "query" : {
    "has_child" : {
      "query" : {
        "bool" : {
          "must" : {
            "bool" : {
              "must" : {
                "nested" : {
                  "query" : {
                    "bool" : {
                      "must" : {
                        "filtered" : {
                          "query" : {
                            "match_all" : { }
                          },
                          "filter" : {
                            "term" : {
                              "Owner.Type" : "User",
                              "_cache" : true
                            }
                          }
                        }
                      }
                    }
                  },
                  "path" : "Owner"
                }
              }
            }
          }
        }
      },
      "child_type" : "Child"
    }
  }

```

This should return valid results based on the documents in Elasticsearch, however the query successfully runs with no results returned.

Any suggestions?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)\
**Post date:** [July 1, 2015, 3:15pm UTC](https://discuss.elastic.co/t/has-child-query-on-nested-object-field/24736/2 "2015-07-01T15:15:53Z")

</div>

I haven't tested it but your query should look something like this:

```
{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "has_child": {
                    "type": "Child",
                    "query": {
                        "nested": {
                            "path": "Owner",
                            "term": {
                                "Owner.Type": "User",
                                "_cache": true
                            }
                        }
                    }
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![bevans88](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@bevans88](https://discuss.elastic.co/u/bevans88)\
**Post date:** [July 1, 2015, 3:25pm UTC](https://discuss.elastic.co/t/has-child-query-on-nested-object-field/24736/3 "2015-07-01T15:25:01Z")

</div>

Just tried using the has\_child filter in place of the has\_child query and it does indeed work.

Thanks for the help.

---

<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 6, 2017, 12:04am UTC](https://discuss.elastic.co/t/has-child-query-on-nested-object-field/24736/4 "2017-07-06T00:04:10Z")

</div>


