Parent child deep level hierarchy

I want to implement multilevel inheritance in elasticsearch .And I know already that parent and child should be stored on the same shard .for example
a->b->-c>d->e
here a is the top parent
b is child of a
c is child of b
d is child of c and so on .
and i need this because i am managing rights management in elasticsearch .
as documents under a folder will inherit the rights from it and all its child .
How can this be done ?
is there any way to do so ?

Yes, you just have a parent, with child, grandchild, great grandchild etc.

ok ! But can I query on all the ancestors of a child by using has child query .Given that I don't know at which level is its parent ,grandparent etc. I mean I just want to search in all ancestors of a node ,and don't have any idea who is who's parent .

Although using deep parent-child relationships at first may seem like a natural fit to this problem, it may not be the best way to model it in Elasticsearch, especially if the depth of the hierarchies can vary. Flattening data, at least to some extent, is often a better approach as it can scale and perform better as all data does not need to reside in the same shard. The drawback is naturally that more entities in the hierarchy need to be updated when privileges change. Your access patterns, rate of change and hierarchy may affect the level of flattening that is appropriate.

One way of modelling this could e.g. be to store all folders in the hierarchy as individual parent level documents and all resources as child documents linked to a parent. Each folder could hold the full path, possibly analyzed using the path hierarchy tokenizer.

The tokenized path can then be used to easily identify all folders affected by a change to another folder. Updates to higher levels in the folder hierarchy could result in a large number of dependant folders needing to be updated, but if this is a reasonably rare event, it may be worth it as querying may be simpler and faster.