Deep Hierarchy with Count

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:

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?

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