Recursive search query for all leaf nodes(childs)

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

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.

Assumptions:

  • Each document has a unique ID
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.

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

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