# Recursive search query for all leaf nodes(childs)

**URL:** <https://discuss.elastic.co/t/recursive-search-query-for-all-leaf-nodes-childs/266791>\
**Category:** Elasticsearch\
**Created:** [March 10, 2021, 10:02am UTC](https://discuss.elastic.co/t/recursive-search-query-for-all-leaf-nodes-childs/266791 "2021-03-10T10:02:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paresh\_Vaniya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paresh_vaniya/32/79807_2.png) [@Paresh\_Vaniya](https://discuss.elastic.co/u/Paresh_Vaniya)\
**Post date:** [March 10, 2021, 10:02am UTC](https://discuss.elastic.co/t/recursive-search-query-for-all-leaf-nodes-childs/266791/1 "2021-03-10T10:02:10Z")

</div>

Hi All,

I need help for getting all the leaf nodes details using elasticsearch query. If I have search for root node id and it contains childEntities then it should get all the child id and its name in result.

I have tried lot many things but it didn't work. So can someone help me on this. Please find three documents :

Doc 1 : {  
"id": "2e12uu89dmn",  
"name": "Test1",  
"description": "Test1",  
"parentEntities": ,  
"childEntities": [  
{  
"children": [  
{  
"id": "wudq8u8028ddu",  
"name": "Test2"

```
	}]
}]

```

},  
Doc 2 : {  
"id": "wudq8u8028ddu",  
"name": "Test2",  
"description": "Test2",  
"parentEntities": [  
{  
"id": "2e12uu89dmn",  
"name": "Test1"  
}  
],  
"childEntities": [  
{  
"children": [  
{  
"id": "euryeiuwiw",  
"name": "Test3"

```
	}]
}]

```

}  
Doc 3 : {  
"id": "euryeiuwiw",  
"name": "Test3",  
"description": "Test3",  
"parentEntities": [  
{  
"id": "2e12uu89dmn",  
"name": "Test1"  
},  
{  
"id": "wudq8u8028ddu",  
"name": "Test2"  
}  
],  
"childEntities":   
}  
So let say if I search for parent "id": "2e12uu89dmn" in elasticsearch query then I should get all the leaf nodes(children).

---

<div class="post-metadata">

**Author:** ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)\
**Post date:** [March 11, 2021, 10:35am UTC](https://discuss.elastic.co/t/recursive-search-query-for-all-leaf-nodes-childs/266791/2 "2021-03-11T10:35:04Z")

</div>

Hello @Paresh_Vaniya

We've reviewed your question and with a huge help by @spinscale we think a good solution might be using the following strategy, based on [path hierarchy tokenizer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pathhierarchy-tokenizer.html).

Assumptions:

- Each document has a unique ID

```auto
DELETE file-path-test
PUT file-path-test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_path_tree": {
          "tokenizer": "custom_hierarchy"
        },
        "custom_path_tree_reversed": {
          "tokenizer": "custom_hierarchy_reversed"
        }
      },
      "tokenizer": {
        "custom_hierarchy": {
          "type": "path_hierarchy",
          "delimiter": "/"
        },
        "custom_hierarchy_reversed": {
          "type": "path_hierarchy",
          "delimiter": "/",
          "reverse": "true"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "file_path": {
        "type": "text",
        "fields": {
          "tree": {
            "type": "text",
            "analyzer": "custom_path_tree"
          },
          "tree_reversed": {
            "type": "text",
            "analyzer": "custom_path_tree_reversed"
          }
        }
      }
    }
  }
}

# We need to use the ID as document ID when indexing
POST file-path-test/_doc/2e12uu89dmn
{
  "file_path": "/2e12uu89dmn",
    "name": "Test1",
  "description": "Test1"
}

# We need to use the ID as document ID when indexing
POST file-path-test/_doc/wudq8u8028ddu
{
  "file_path": "/2e12uu89dmn/wudq8u8028ddu",
    "name": "Test2",
  "description": "Test2"
}

# We need to use the ID as document ID when indexing
POST file-path-test/_doc/euryeiuwiw
{
  "file_path": "/2e12uu89dmn/wudq8u8028ddu/euryeiuwiw",
    "name": "Test3",
  "description": "Test3"
}

# This query can be used to search an element if you know its "absolute path"
GET file-path-test/_search
{
  "query": {
    "term": {
      "file_path.tree": "/2e12uu89dmn"
    }
  }
}

# This query can be used to get all the children from an arbitrary ID
GET file-path-test/_search
{
  "query": {
    "terms": {
      "file_path.tree": {
        "index": "file-path-test",
        "id": "2e12uu89dmn",
        "path": "file_path"
      }
    }
  }
}

```

Another advantage is a given entity can be added to multiple parents, adding an array of paths.  
E.g.

```auto
POST file-path-test/_doc/aaaeeeuwiw
{
  "file_path": ["/2e12uu89dmn/wudq8u8028ddu/aaaeeeuwiw", "/2e12uu89dmn/aaaeeeuwiw"],
    "name": "Test4",
  "description": "Test4"
}

```

---

<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:** [April 8, 2021, 10:35am UTC](https://discuss.elastic.co/t/recursive-search-query-for-all-leaf-nodes-childs/266791/3 "2021-04-08T10:35:34Z")

</div>

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