How do I index hierarchical data?

I have data similar to file paths that I would like to index. (basically list of tokens separated by delimiter)

> Ex data:
> a/b/c/d/e
> a/b/c/
> a/m/n
> x/y/z

Once I index, I should be able to query to get the immediate children for a given token as shown below.

> For prefix of a, immediate children are [b, m]
> for prefix of x, immediate children are [y]
> Also tokens at root would be [a,x]

Have a look at the Path Hierarchy Tokenizer.

I actually looked into it. I was able to use the path tokenizer so that a string such a a.b.c would create [a,a.b, a.b.c] terms. But I am not sure if that would be able to address the kind of search query I was looking for.

I looked at terms aggregation which could help with this kinda data but with lots of data, terms aggregation is not able to support this kind of real time query. For example just to find out the root token, it has to do an aggregation at that level. Imagine having a millions of documents and doing aggregation at that level.