# Get the entire sub-tree / tree structure for a document based on a field

**URL:** <https://discuss.elastic.co/t/get-the-entire-sub-tree-tree-structure-for-a-document-based-on-a-field/291614>\
**Category:** Elasticsearch\
**Created:** [December 13, 2021, 9:42am UTC](https://discuss.elastic.co/t/get-the-entire-sub-tree-tree-structure-for-a-document-based-on-a-field/291614 "2021-12-13T09:42:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![olivebranch](https://avatars.discourse-cdn.com/v4/letter/o/7feea3/32.png) [@olivebranch](https://discuss.elastic.co/u/olivebranch)\
**Post date:** [December 13, 2021, 9:42am UTC](https://discuss.elastic.co/t/get-the-entire-sub-tree-tree-structure-for-a-document-based-on-a-field/291614/1 "2021-12-13T09:42:21Z")

</div>

I have a hierarchical structure (much like categories contained within categories) in my document like following:

```auto
                   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"

```auto
{
  "id": "1",
  "parent": "-1",
  "path": "-1"  
}

```

document for "2"

```auto
{
  "id": "2",
  "parent": "1",
  "path": "/1/2"  
}

```

document for "4"

```auto
{
  "id": "4",
  "parent": "2",
  "path": "/1/2/4"  
}

```

and so on.

Here's the whole request to create this structure in index called "tree"

```auto
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):

```auto
1
   2
      4
         6
  3
     5

```

So, i tried an aggregate with top\_hits like following:

```auto
"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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 13, 2021, 9:55am UTC](https://discuss.elastic.co/t/get-the-entire-sub-tree-tree-structure-for-a-document-based-on-a-field/291614/2 "2021-12-13T09:55:20Z")

</div>

Welcome!

I'd look at the [Path hierarchy tokenizer | Elasticsearch Guide [7.16] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pathhierarchy-tokenizer.html) for this use case.  
Did you look at it yet?

May be that would help? Unsure though. 🙂

---

<div class="post-metadata">

**Author:** ![olivebranch](https://avatars.discourse-cdn.com/v4/letter/o/7feea3/32.png) [@olivebranch](https://discuss.elastic.co/u/olivebranch)\
**Post date:** [December 13, 2021, 10:30am UTC](https://discuss.elastic.co/t/get-the-entire-sub-tree-tree-structure-for-a-document-based-on-a-field/291614/3 "2021-12-13T10:30:06Z")

</div>

Thank you for your reply.

I did look at path hierarchy tokenizer, infact, when I look the `_mapping` of my index, it uses the `path hierarchy tokenizer`, but I'm not sure how to use it to fetch the entire tree.  
I also tried using the [collapse](https://www.elastic.co/guide/en/elasticsearch/reference/current/collapse-search-results.html) unsuccessfully.  
I think closest I've come to the solution is the query I posted on my question itself i.e using `top_hits`, but that gives a result like following:

```auto
1
    2
    3
2
    4
3
    5
4
    6

```

instead of the entire hierarchy (within hierarchy). I can use the this result, but my Application will have to do the work to traverse the result set and build the entire hierarchy, I was really hoping this was possible to do using query itself in Elastic.

---

<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:** [January 10, 2022, 10:30am UTC](https://discuss.elastic.co/t/get-the-entire-sub-tree-tree-structure-for-a-document-based-on-a-field/291614/4 "2022-01-10T10:30:38Z")

</div>

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