# Deep Hierarchy with Count

**URL:** <https://discuss.elastic.co/t/deep-hierarchy-with-count/314943>\
**Category:** Elasticsearch\
**Tags:** eql-elastic-query-language\
**Created:** [September 22, 2022, 12:28pm UTC](https://discuss.elastic.co/t/deep-hierarchy-with-count/314943 "2022-09-22T12:28:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![m-dot-jung](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m-dot-jung/32/111114_2.png) [@m-dot-jung](https://discuss.elastic.co/u/m-dot-jung)\
**Post date:** [September 22, 2022, 12:28pm UTC](https://discuss.elastic.co/t/deep-hierarchy-with-count/314943/1 "2022-09-22T12:28:06Z")

</div>

Hi,

I tried modelling a document hierarchy where each record might have a parent record. The maximum depth of this hierarchy is variable.  
Now, I want to query for certain documents by ID the number of children - best would be if I could already combine that into a single query where I select the number of children as an added member.

However, it seems that the selection via has\_child is only supporting one level. The following example works properly for the proper parent "Root" (showing 2 children - which would be "Child 1" and "Child 2"), but does not work deeper in the hierarchy, e.g. if I start from "Child 1", then it does not show any child even though there should be one ("Grandchild 1"). Here is the code:

```auto
PUT /hier-test
{
  "mappings": {
    "properties": {
      "hier": {
        "type": "join",
        "relations": {
          "parent": "child"
        }
      }
    }
  }
}

POST _bulk
{"index":{"_id":"Root","_index":"hier-test","routing":"1"}}
{"hier":{"name":"parent"}}
{"index":{"_id":"Child 1","_index":"hier-test","routing":"1"}}
{"hier":{"name":"child","parent":"Root"}}
{"index":{"_id":"Child 2","_index":"hier-test","routing":"1"}}
{"hier":{"name":"child","parent":"Root"}}
{"index":{"_id":"Grandchild 1","_index":"hier-test","routing":"1"}}
{"hier":{"name":"child","parent":"Child 1"}}

POST hier-test/_search?size=0
{
  "aggs": {
    "filter-id-count-children": {
      "filter": {
        "term": {
          "_id": "Root"
        }
      },
      "aggs": {
        "count-children": {
          "children": {
            "type": "child"
          }
        }
      }
    }
  }
}

POST hier-test/_search?size=0
{
  "aggs": {
    "filter-id-count-children": {
      "filter": {
        "term": {
          "_id": "Child 1"
        }
      },
      "aggs": {
        "count-children": {
          "children": {
            "type": "child"
          }
        }
      }
    }
  }
}

```

Any idea, how I can do this properly?

---

<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 20, 2022, 12:28pm UTC](https://discuss.elastic.co/t/deep-hierarchy-with-count/314943/2 "2022-10-20T12:28:43Z")

</div>

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