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

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.
.

Using path-hierarchy tokenizer 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,

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