# Is there a way to query all child and grandchild documents by using the parent/grandparentID?

**URL:** <https://discuss.elastic.co/t/is-there-a-way-to-query-all-child-and-grandchild-documents-by-using-the-parent-grandparentid/199240>\
**Category:** Elasticsearch\
**Created:** [September 12, 2019, 11:09am UTC](https://discuss.elastic.co/t/is-there-a-way-to-query-all-child-and-grandchild-documents-by-using-the-parent-grandparentid/199240 "2019-09-12T11:09:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prasanth\_Rajendran](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prasanth_rajendran/32/54073_2.png) [@Prasanth\_Rajendran](https://discuss.elastic.co/u/Prasanth_Rajendran)\
**Post date:** [September 12, 2019, 11:09am UTC](https://discuss.elastic.co/t/is-there-a-way-to-query-all-child-and-grandchild-documents-by-using-the-parent-grandparentid/199240/1 "2019-09-12T11:09:48Z")

</div>

I have a below scenario: I am creating index called company where I am creating multilevel relationship

```auto
         company
            |
    |---------------|
department supplier
    |
employee

```

I have created below mapping:

```auto
PUT /company
{
  "mappings": {
    "_doc":{
      "properties":{
        "join_field":{
          "type":"join",
          "relations":{
            "company": ["supplier", "department"],
            "department": "employee"
          }
        }
      }
    }
  }
}

```

I am able to query all the departments under company using the parent id (company id)

```auto
GET /company/_doc/_search
{
    "query":{
      "parent_id":{
        "type":"department",
        "id":1
      }
    }
}

```

I am able to query all the employees under the department of the company

```auto
GET /company/_doc/_search?routing=1
{
  "query":{
    "has_parent":{
      "parent_type":"department",
      "query":{
        "term":{
          "name.keyword":"Development"
        }
      }
    }
  }
}

```

finally, my question is, how to query all the department and employees of a company in a single query instead of fetching all departments and use those departments to fetch the employees? NOTE: I am using `elastic search 7+` version

---

<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:** [October 10, 2019, 11:24am UTC](https://discuss.elastic.co/t/is-there-a-way-to-query-all-child-and-grandchild-documents-by-using-the-parent-grandparentid/199240/2 "2019-10-10T11:24:13Z")

</div>

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