Search code examples
elasticsearchelasticsearch-7

How to get all grandchild in elasticsearch 7.10 from the grandparent


I have a relationship in my elasticsearch in which A is a parent and B is a child of A and than C is a child of B.

A->B->C.

At the time of insertion the relationship was the same B have A as parent and in the insertion of grandchild B was the parent.

Now when I wanted to get all the grandchild using the following query its returning None.

{
    "query": {
        "has_parent": {
            "parent_type": "A",
            "query": {
                "has_parent": {
                    "parent_type": "B",
                    "query": {
                        "match_all": {}
                    }
                }
            }
        }
    }
}

What is the issue ? Is it something I am missing when storing any help is appreciated.


Solution

  • I have achieved it using the following query.

    {
        "query": {
            "has_parent": {
                "parent_type": "B",
                "query": {
                    "has_parent": {
                        "parent_type": "A",
                        "query": {
                            "match_all": {}
                        }
                    }
                }
            }
        }
    }
    

    It was basically the parent should be nested instead child.