I have a hierarchical structure (much like categories contained within categories) in my document like following:
1(parent for root category is: -1)
/ \
2 3
/ \
4 5
/
6
I have 2 separate fields in the mapping to express this relationship: parent
and path
as shown below:
document for "1"
{
"id": "1",
"parent": "-1",
"path": "-1"
}
document for "2"
{
"id": "2",
"parent": "1",
"path": "/1/2"
}
document for "4"
{
"id": "4",
"parent": "2",
"path": "/1/2/4"
}
and so on.
Here's the whole request to create this structure in index called "tree"
PUT tree
POST _bulk
{ "create" : { "_index" : "tree"}}
{ "id": "1", "parent": "-1", "path": "/1" }
{ "create" : { "_index" : "tree"}}
{ "id": "2", "parent": "1", "path": "/1/2" }
{ "create" : { "_index" : "tree"}}
{ "id": "3", "parent": "1", "path": "/1/3" }
{ "create" : { "_index" : "tree"}}
{ "id": "4", "parent": "2", "path": "/1/2/4" }
{ "create" : { "_index" : "tree"}}
{ "id": "6", "parent": "4", "path": "/1/2/4/6" }
{ "create" : { "_index" : "tree"}}
{ "id": "5", "parent": "3", "path": "/1/3/5" }
Now, I want to query and print the entire tree (note: I do not know the depth in advance) which should print the result like following (similar to top-navigation menu in ecom sites, for eg. Mens > Tops > Shirts > Collared shirt i.e. get the whole tree):
1
2
4
6
3
5
So, i tried an aggregate with top_hits like following:
"aggs": {
"sub-cat-tree": {
"terms": {
"field": "parent.keyword",
"size": "30"
},
"aggs": {
"tops": {
"top_hits": {
"size": 20
}
}
}
}
}
}
But this did not print the tree within the tree, instead it grouped by all parents.
I think I need a nested aggs, but I'm not sure how, as I do not know depth in advance.
Any help would be appreciated. Thanks.