Grand children aggregation is returning the zero result in elasticsearch 2.2.0 version

Hi,

I am not able to fetch the data for the for the grand child aggregation.

for example : I have used the parent and child relationship to create the index and types.
index name is "employeedetails", which is having following three types
country -> employee -> department

  1. country is parent of employee
  2. employee is parent of department

Here is my query to fetch the department data from rest service browser.

URL : http://localhost:9200/employeedetails/country/_search?pretty

Query is : Find total department count.

{
"_source": false,
"aggs": {
"employee": {
"children": {
"type": "employee"
},
"aggs": {
"department": {
"children": {
"type": "department"
},
"aggs": {
"departmentCount": {
"cardinality": {
"field": "department_id",
"precision_threshold": 4000
}
}
}
}
}
}
}
}

I tried one more way to access department, that also returning the 0 results, same query works for employee.

URL : http://localhost:9200/employeedetails/employee/_search?pretty

Query is : Find total department count.

{
"_source": false,
"aggs": {
"department": {
"children": {
"type": "department"
},
"aggs": {
"departmentCount": {
"cardinality": {
"field": "department_id",
"precision_threshold": 4000
}
}
}
}
}

Note : In elasticsearch.yml I have the configuration of "index.number_of_shards: 1", in order to maintain only one shard.

Could some one suggest me whats wrong with my approach?

The aggregation looks good. I think if you remove the type from the url you do get results. If not can you share your entire search request here?

If you also use only one primary shard in production you may consider just using the terms aggregation, because in your case the bucket count will always be correct.

Thank you for responding. I tried the same query by removing the type from the URL, it didn't worked. However, my goal is to achieve the aggregations on the grand child like, count, sum, avg etc.

I am trying from the parent type employee and country from both.

please find the complete request.
URL : http://localhost:9200/employeedetails/employee/_search?pretty

Query is : Find total department count.
{
"_source": false,
"aggs": {
"department": {
"children": {
"type": "department"
},
"aggs": {
"departmentCount": {
"cardinality": {
"field": "department_id",
"precision_threshold": 4000
}
}
}
}
}
}

What ES version are you using?

I tried using your search request and things work out here (sense snippets):

PUT test
{
"mappings": {
"country" : {
},
"employee" : {
"_parent": {
"type": "country"
}
},
"department" : {
"_parent": {
"type": "employee"
},
"properties": {
"department_id" : {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}

PUT test/country/1
{}

PUT test/employee/1?parent=1
{}

PUT test/employee/2?parent=1
{}

PUT test/department/1?parent=1
{
"department_id" : "1"
}

PUT test/department/2?parent=1
{
"department_id" : "2"
}

PUT test/department/3?parent=2
{
"department_id" : "3"
}

GET /_search
{
"_source": false,
"aggs": {
"department": {
"children": {
"type": "department"
},
"aggs": {
"departmentCount": {
"cardinality": {
"field": "department_id",
"precision_threshold": 4000
}
}
}
}
}
}

I am using ES 2.2.0 version. I'll check this again.

I am able to get the result for this. But I have one more index data with similar hierarchy it is throwing the exception in the console

nested: SearchParseException[[children] no [_parent] field not configured that points to a parent type];
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:853)
at org.elasticsearch.search.SearchService.createContext(SearchService.java:652)
at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:618)
at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:369)
at org.elasticsearch.search.action.SearchServiceTransportAction$SearchQueryTransportHandler.messageReceived(SearchServiceTransportAction.java:368)
at org.elasticsearch.search.action.SearchServiceTransportAction$SearchQueryTransportHandler.messageReceived(SearchServiceTransportAction.java:365)
at org.elasticsearch.transport.TransportService$4.doRun(TransportService.java:350)
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: SearchParseException[[children] no [_parent] field not configured that points to a parent type]
at org.elasticsearch.search.aggregations.bucket.children.ChildrenParser.parse(ChildrenParser.java:82)
at org.elasticsearch.search.aggregations.AggregatorParsers.parseAggregators(AggregatorParsers.java:198)
at org.elasticsearch.search.aggregations.AggregatorParsers.parseAggregators(AggregatorParsers.java:103)
at org.elasticsearch.search.aggregations.AggregationParseElement.parse(AggregationParseElement.java:60)
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:836)
... 10 more

Though, my mapping for that type having the "_parent" configuration.

Somehow the the mapping children doesn't have a _parent field. Can you share your mapping?

Thank you. I have got the issue with my approach. I am looking is there any way I can achieve that please let me know?

Here is my mapping and data.

http://localhost:9200/test_
{
    "mappings": {
        "country": {
            "properties": {
                "country_id": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        },
        "employee": {
            "_parent": {
                "type": "country"
            },
            "properties": {
                "employee_id": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        },
        "department": {
            "_parent": {
                "type": "employee"
            },
            "properties": {
                "department_id": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}

http://localhost:9200/test_/country/1

{
"country_id" : "1"
}

http://localhost:9200/test_/employee/1?parent=1

{
"employee_id" : "1",
"dept_id":"dept1"
}

http://localhost:9200/test_/employee/2?parent=2

{
"employee_id" : "2",
"dept_id":"dept2"
}

http://localhost:9200/test_/department/1?parent=dept1

{
"department_id" : "dept1"
}

http://localhost:9200/test_/department/1?parent=dept2

{
"department_id" : "dept2"
}

in this case above search query will not work.

My use case is parent and 1st child has the one unique key and 1st child and 2nd child relationship has another key called dept1 and dept2. Hence when I am searching for the last child aggregation, it will say 0 records as it didn't find the key from any of the parent. Is there any way I can handle this ?

The reason the children agg isn't working is because the department documents do not point to do an employee that exists. dept1 and dept2 are no valid employee documents. The only two ids you can use here are 1 and 2 (which are employee ids).

Also employee with id 2 does not point to an existing country. There is no country with id 2.

What is the purpose of the department_id, employee_id, dept_id and country_id fields? The ids of a department, employee and country are already in the _id field. The children aggregation doesn't use these field.

Thank You So much for the response. I am able to solve this problem, This was the issue with the _parent routing. The ids are basically for the uniqueness of the documents.