# Get only one level of documents when using path-hierarchy tokenizer

**URL:** <https://discuss.elastic.co/t/get-only-one-level-of-documents-when-using-path-hierarchy-tokenizer/266704>\
**Category:** Elasticsearch\
**Created:** [March 9, 2021, 1:53pm UTC](https://discuss.elastic.co/t/get-only-one-level-of-documents-when-using-path-hierarchy-tokenizer/266704 "2021-03-09T13:53:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![avhvr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/avhvr/32/85205_2.png) [@avhvr](https://discuss.elastic.co/u/avhvr)\
**Post date:** [March 9, 2021, 1:53pm UTC](https://discuss.elastic.co/t/get-only-one-level-of-documents-when-using-path-hierarchy-tokenizer/266704/1 "2021-03-09T13:53:01Z")

</div>

Hi,

I need to search documents (employee details) that are present in a tree structure.  
The number of levels in the hierarchy could be any.  
 ![](https://us1.discourse-cdn.com/elastic/original/3X/0/c/0c8e562f2403ff531b67968ea9d30e1267949998.jpeg).

Using [path-hierarchy tokenizer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pathhierarchy-tokenizer.html) and this post, I was able to index them and search them wrt to hierarchy.

```
PUT my_index
{
"settings": {
    "analysis": {
    "analyzer": {
        "hpath_analyzer": {
        "tokenizer": "path_hierarchy"
        }
    }
    }
},
"mappings": {
    "doc": {
    "properties": {
        "name": {"type": "text"},
        "hpath": {
        "type": "text",
        "analyzer": "hpath_analyzer"
        }
    }
    }
}
}

POST my_index/doc/root
{
"name": "Root",
"hpath": "/"
}

POST my_index/doc/branch_a
{
"name": "Branch A",
"hpath": "/root"
}

POST my_index/doc/branch_b
{
"name": "Branch B",
"hpath": "/root"
}

POST my_index/doc/leaf_c
{
"name": "Leaf C",
"hpath": "/root/branch_a"
}

POST my_index/doc/leaf_d
{
"name": "Leaf D",
"hpath": "/root/branch_a"
}

POST my_index/doc/leaf_e
{
"name": "Leaf E",
"hpath": "/root/branch_b"
}

POST my_index/doc/leaf_f
{
"name": "Leaf F",
"hpath": "/root/brach_b"
}

GET my_index/_search

GET my_index/_search
{
"query": {
    "term": {
    "hpath": "/root/branch_a"
    }
}
}

```

One usecase, I was not able to figure out is how do I limit the search here to only one level.

1. How do I search for only the immediate employees under "Root " such as "Branch A" and "Branch B" and not the leaves?
2. Is this the right way to index and search for hierarchical data?

Thanks,

---

<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 6, 2021, 1:53pm UTC](https://discuss.elastic.co/t/get-only-one-level-of-documents-when-using-path-hierarchy-tokenizer/266704/2 "2021-04-06T13:53:42Z")

</div>

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