{"action":"closed","issue":{"url":"https://api.github.com/repos/elastic/elasticsearch/issues/15945","labels_url":"https://api.github.com/repos/elastic/elasticsearch/issues/15945/labels{/name}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/15945/comments","events_url":"https://api.github.com/repos/elastic/elasticsearch/issues/15945/events","html_url":"https://github.com/elastic/elasticsearch/issues/15945","id":126325834,"number":15945,"title":"Nonunique terms are being accounted multiple times in terms aggregation of child/nested documents","user":{"login":"serj-p","id":2565455,"avatar_url":"https://avatars.githubusercontent.com/u/2565455?v=3","gravatar_id":"","url":"https://api.github.com/users/serj-p","html_url":"https://github.com/serj-p","followers_url":"https://api.github.com/users/serj-p/followers","following_url":"https://api.github.com/users/serj-p/following{/other_user}","gists_url":"https://api.github.com/users/serj-p/gists{/gist_id}","starred_url":"https://api.github.com/users/serj-p/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/serj-p/subscriptions","organizations_url":"https://api.github.com/users/serj-p/orgs","repos_url":"https://api.github.com/users/serj-p/repos","events_url":"https://api.github.com/users/serj-p/events{/privacy}","received_events_url":"https://api.github.com/users/serj-p/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-13T02:27:18Z","updated_at":"2016-01-13T11:08:38Z","closed_at":"2016-01-13T11:08:38Z","body":"My domain is the following: a parent doc has some child docs with some string field. I want to calculate a frequency of parents having particular field in the child doc. When this particular field doesn't have duplicates amond same parent - results of query below are OK. But when some parent has multiple children with same value of the field, calculation sums this duplicates and gives wrong results. When I perform equal search query it gives me right count of parent docs. \r\n\r\n\r\n        curl -XPUT 127.0.0.1:9200/test_index -d '\r\n        {\r\n          \"mappings\": {\r\n            \"test_parent\": {\r\n                \"properties\": {\r\n                    \"test_filter_field\": {\r\n                        \"type\": \"string\"\r\n                    }\r\n                }\r\n            },\r\n            \"test_child\": {\r\n              \"_parent\": {\r\n                \"type\": \"test_parent\"\r\n              },\r\n              \"properties\": {\r\n                  \"test_non_nested_field\": {\r\n                      \"type\": \"string\"\r\n                  },\r\n                  \"test_nested_field\": {\r\n                      \"type\": \"nested\",\r\n                      \"properties\": {\r\n                        \"nested_filter_field\": {\r\n                            \"type\": \"string\"\r\n                        },\r\n                        \"nested_term_field\": {\r\n                            \"type\": \"string\"\r\n                        }\r\n                      }\r\n                  }\r\n              }\r\n            }\r\n          }\r\n        }'\r\n        \r\n        curl -XPUT 127.0.0.1:9200/test_index/test_parent/parent1 -d '\r\n        {\r\n            \"test_filter_field\" : \"1\"\r\n        }'\r\n        \r\n        curl -XPUT 127.0.0.1:9200/test_index/test_child/child4?parent=parent1 -d '\r\n        {\r\n            \"test_non_nested_field\": \"non_nested_term_field11\"\r\n        }'\r\n        \r\n        curl -XPUT 127.0.0.1:9200/test_index/test_child/child5?parent=parent1 -d '\r\n        {\r\n            \"test_non_nested_field\": \"non_nested_term_field11\"\r\n        }'\r\n        \r\n        curl -XPOST 127.0.0.1:9200/test_index/test_parent/_search?pretty=true -d '\r\n        {\r\n          \"aggs\": {\r\n            \"top\": {\r\n              \"filter\": {\r\n                \"term\": {\r\n                  \"test_filter_field\": \"1\"\r\n                }\r\n              },\r\n              \"aggs\": {\r\n                \"children.nested.filter\": {\r\n                  \"children\": {\r\n                    \"type\": \"test_child\"\r\n                  },\r\n                  \"aggs\": {\r\n                    \"field\": {\r\n                      \"terms\": {\r\n                        \"field\": \"test_child.test_non_nested_field\"\r\n                      }\r\n                    }\r\n                  }\r\n                }\r\n              }\r\n            }\r\n          }\r\n        }\r\n        '\r\n\r\nIt gives me \r\n\r\n        \"aggregations\" : {\r\n        \"top\" : {\r\n          \"doc_count\" : 1,\r\n          \"children.nested.filter\" : {\r\n            \"doc_count\" : 2,\r\n            \"field\" : {\r\n              \"doc_count_error_upper_bound\" : 0,\r\n              \"sum_other_doc_count\" : 0,\r\n              \"buckets\" : [ {\r\n                \"key\" : \"non_nested_term_field11\",\r\n                \"doc_count\" : 2  <------ I expect it here to give me 1\r\n              } ]\r\n            }\r\n          }\r\n        }\r\n        }\r\n\r\nHere is equal search query, which gives correct result\r\n\r\n        curl -XPOST 127.0.0.1:9200/test_index/test_parent/_search?pretty=true -d '\r\n        {\r\n            \"query\": {\r\n                \"has_child\": {\r\n                    \"query\": {\r\n                        \"multi_match\": {\r\n                            \"query\": \"non_nested_term_field11\",\r\n                            \"fields\": [\r\n                                \"test_non_nested_field\"\r\n                            ]\r\n                        }\r\n                    },\r\n                    \"type\": \"test_child\"\r\n                }\r\n            }\r\n        }\r\n\r\nresponse\r\n\r\n        {\r\n        \r\n          \"hits\" : {\r\n            \"total\" : 1,  <--- obviously correct\r\n            \"max_score\" : 1.0,\r\n            \"hits\" : [\r\n                {\r\n                  \"_index\" : \"test_index\",\r\n                  \"_type\" : \"test_parent\",\r\n                  \"_id\" : \"parent1\",\r\n                  \"_score\" : 1.0,\r\n                  \"_source\": {\r\n                        \"test_filter_field\" : \"1\"\r\n                  }\r\n                }\r\n            ]\r\n          }\r\n        }"}}