# Aggregations on multiple children cause wrong doc\_count

**URL:** <https://discuss.elastic.co/t/aggregations-on-multiple-children-cause-wrong-doc-count/22305>\
**Category:** Elasticsearch\
**Created:** [February 20, 2015, 10:26pm UTC](https://discuss.elastic.co/t/aggregations-on-multiple-children-cause-wrong-doc-count/22305 "2015-02-20T22:26:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthias](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@matthias](https://discuss.elastic.co/u/matthias)\
**Post date:** [February 20, 2015, 10:26pm UTC](https://discuss.elastic.co/t/aggregations-on-multiple-children-cause-wrong-doc-count/22305/1 "2015-02-20T22:26:19Z")

</div>

I have an index which consists of Groups, Members and Discussions. Both  
Discussions and Members are children of Groups.  
When doing an aggregation query to find out how many Members and  
Discussions there're for a specific Group(s) the aggregation which is 2nd  
ends up having a duplicated doc\_count.

My query looks like this, membershipCount will end up being double of the  
correct value:

{  
"query" : {  
"ids" : {  
"type" : "group",  
"values" : ["group\_id"]  
}  
},  
"aggregations" : {  
"groups" : {  
"terms" : {  
"field" : "id"  
},  
"aggregations" : {  
"discussionCount" : {  
"children" : {  
"type" : "discussion"  
}  
},  
"membershipCount" : {  
"children" : {  
"type" : "groupMembership"  
}  
}  
}  
}  
}  
}

If I simply switch membershipCount and discussionCount, making  
discussionCount the 2nd subaggregation the doc count for memberships is  
correct but discussionCount ends up doubled.

I'm able to get rid of this behavior by running a cardinality  
subaggregation like this:

{  
"query" : {  
"ids" : {  
"type" : "group",  
"values" : ["group\_id"]  
}  
},  
"aggregations" : {  
"groups" : {  
"terms" : {  
"field" : "id"  
},  
"aggregations" : {  
"discussionCount" : {  
"children" : {  
"type" : "discussion"  
},  
"aggregations" : {  
"activeDiscussionAggs" : {  
"cardinality" : {  
"field" : "id"  
}  
}  
}  
},  
"membershipCount" : {  
"children" : {  
"type" : "groupMembership"  
},  
"aggregations" : {  
"groupMembers" : {  
"cardinality" : {  
"field" : "id"  
}  
}  
}  
}  
}  
}  
}  
}

Adding "collect\_mode" : "breadth\_first" to the terms query even causes the  
first doc\_count to end up being 3times the correct count and the 2nd  
aggregation count 6times the correct count.  
Using cardinality everything works fine but I'd greatly appreciate if  
anyone could explain this behavior or if my aggregation should be done a  
different way.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/759c356f-94f2-43eb-b3f0-6a6d3a1089bc%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/759c356f-94f2-43eb-b3f0-6a6d3a1089bc%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood\_2](https://avatars.discourse-cdn.com/v4/letter/m/c77e96/32.png) [@Mark\_Harwood\_2](https://discuss.elastic.co/u/Mark_Harwood_2)\
**Post date:** [February 21, 2015, 9:11am UTC](https://discuss.elastic.co/t/aggregations-on-multiple-children-cause-wrong-doc-count/22305/2 "2015-02-21T09:11:26Z")

</div>

Hard to diagnose the query behaviour without the docs + mappings.  
Can you post examples?

On Friday, February 20, 2015 at 10:26:19 PM UTC, [matt...@netbrains.com](mailto:matt...@netbrains.com)  
wrote:

> I have an index which consists of Groups, Members and Discussions. Both  
> Discussions and Members are children of Groups.  
> When doing an aggregation query to find out how many Members and  
> Discussions there're for a specific Group(s) the aggregation which is 2nd  
> ends up having a duplicated doc\_count.
> 
> My query looks like this, membershipCount will end up being double of the  
> correct value:
> 
> {  
> "query" : {  
> "ids" : {  
> "type" : "group",  
> "values" : ["group\_id"]  
> }  
> },  
> "aggregations" : {  
> "groups" : {  
> "terms" : {  
> "field" : "id"  
> },  
> "aggregations" : {  
> "discussionCount" : {  
> "children" : {  
> "type" : "discussion"  
> }  
> },  
> "membershipCount" : {  
> "children" : {  
> "type" : "groupMembership"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> If I simply switch membershipCount and discussionCount, making  
> discussionCount the 2nd subaggregation the doc count for memberships is  
> correct but discussionCount ends up doubled.
> 
> I'm able to get rid of this behavior by running a cardinality  
> subaggregation like this:
> 
> {  
> "query" : {  
> "ids" : {  
> "type" : "group",  
> "values" : ["group\_id"]  
> }  
> },  
> "aggregations" : {  
> "groups" : {  
> "terms" : {  
> "field" : "id"  
> },  
> "aggregations" : {  
> "discussionCount" : {  
> "children" : {  
> "type" : "discussion"  
> },  
> "aggregations" : {  
> "activeDiscussionAggs" : {  
> "cardinality" : {  
> "field" : "id"  
> }  
> }  
> }  
> },  
> "membershipCount" : {  
> "children" : {  
> "type" : "groupMembership"  
> },  
> "aggregations" : {  
> "groupMembers" : {  
> "cardinality" : {  
> "field" : "id"  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> Adding "collect\_mode" : "breadth\_first" to the terms query even causes the  
> first doc\_count to end up being 3times the correct count and the 2nd  
> aggregation count 6times the correct count.  
> Using cardinality everything works fine but I'd greatly appreciate if  
> anyone could explain this behavior or if my aggregation should be done a  
> different way.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/2894615c-752b-41f0-9d61-e48ef2a10322%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2894615c-752b-41f0-9d61-e48ef2a10322%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![matthias](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@matthias](https://discuss.elastic.co/u/matthias)\
**Post date:** [February 23, 2015, 4:14pm UTC](https://discuss.elastic.co/t/aggregations-on-multiple-children-cause-wrong-doc-count/22305/3 "2015-02-23T16:14:50Z")

</div>

Here're the mappings:

{  
"discussion" : {  
"mappings" : {  
"groupMembership" : {  
"\_parent" : {  
"type" : "group"  
},  
"\_routing" : {  
"required" : true  
},  
"properties" : {  
"approved" : {  
"type" : "boolean",  
"store" : true  
},  
"id" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"parent" : {  
"type" : "string",  
"index" : "not\_analyzed",  
"store" : true  
},  
"userId" : {  
"type" : "string",  
"index" : "not\_analyzed"  
}  
}  
},  
"group" : {  
"properties" : {  
"createdDate" : {  
"type" : "date",  
"format" : "dateOptionalTime"  
},  
"deletedDate" : {  
"type" : "date",  
"format" : "dateOptionalTime"  
},  
"description" : {  
"type" : "string"  
},  
"id" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"modifiedDate" : {  
"type" : "date",  
"format" : "dateOptionalTime"  
},  
"name" : {  
"type" : "string",  
"store" : true  
},  
"targetType" : {  
"type" : "string"  
},  
"tenantId" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"visibility" : {  
"type" : "string",  
"index" : "not\_analyzed"  
}  
}  
},  
"discussion" : {  
"\_parent" : {  
"type" : "group"  
},  
"\_routing" : {  
"required" : true  
},  
"properties" : {  
"authorId" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"createdDate" : {  
"type" : "date",  
"format" : "date\_optional\_time"  
},  
"deletedDate" : {  
"type" : "date",  
"format" : "date\_optional\_time"  
},  
"id" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"message" : {  
"type" : "string"  
},  
"modifiedDate" : {  
"type" : "date",  
"format" : "date\_optional\_time"  
},  
"parent" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"subject" : {  
"type" : "string",  
"fields" : {  
"subject.raw" : {  
"type" : "string",  
"index" : "not\_analyzed"  
}  
}  
},  
"tenantId" : {  
"type" : "string",  
"index" : "not\_analyzed"  
}  
}  
}  
}  
}  
}

And some sample objects:  
{  
"\_index" : "discussion",  
"\_type" : "group",  
"\_id" : "7cc810d9-1412-48d0-97bd-830543230624",  
"\_score" : 1.0,  
"\_source":{"id":"7cc810d9-1412-48d0-97bd-830543230624","name":"CHAPTER 19. The Prophet.","description":""Shipmates, have ye shipped in that ship?"","visibility":"PUBLIC","targetType":null,"createdDate":1424460745000,"modifiedDate":1424460745000,"deletedDate":null,"tenantId":"18b37386-dd53-496a-82b4-26ef56d4fdf9"}  
}, {  
"\_index" : "discussion",  
"\_type" : "groupMembership",  
"\_id" : "f8beda9a-87cd-4783-b369-645aa1fecc8a",  
"\_score" : 1.0,  
"\_source":{"id":"f8beda9a-87cd-4783-b369-645aa1fecc8a","userId":"54570d06-44ae-4241-9118-f74ea5345703","parent":"7cc810d9-1412-48d0-97bd-830543230624","approved":true}  
}, {  
"\_index" : "discussion",  
"\_type" : "groupMembership",  
"\_id" : "98894b52-c764-4b4b-b737-dc9393a5a6b1",  
"\_score" : 1.0,  
"\_source":{"id":"98894b52-c764-4b4b-b737-dc9393a5a6b1","userId":"5c04f5bb-8d75-47bb-87a0-095ea3e05818","parent":"7cc810d9-1412-48d0-97bd-830543230624","approved":true}  
}, {  
"\_index" : "discussion",  
"\_type" : "groupMembership",  
"\_id" : "f51b93d8-55d9-4a3c-aa4a-e7c2abcd3bbe",  
"\_score" : 1.0,  
"\_source":{"id":"f51b93d8-55d9-4a3c-aa4a-e7c2abcd3bbe","userId":"11cdfd7c-59a4-49a8-b3d9-62c4b49bf761","parent":"7cc810d9-1412-48d0-97bd-830543230624","approved":true}  
}, {  
"\_index" : "discussion",  
"\_type" : "groupMembership",  
"\_id" : "145b9ae9-daf4-4de7-8245-74453565744b",  
"\_score" : 1.0,  
"\_source":{"id":"145b9ae9-daf4-4de7-8245-74453565744b","userId":"c5c103a3-c7c5-41ec-b1c8-0e4aa6861344","parent":"7cc810d9-1412-48d0-97bd-830543230624","approved":true}  
}, {  
"\_index" : "discussion",  
"\_type" : "discussion",  
"\_id" : "af2d431d-b8bc-49cc-87cd-6bbe724ad4b8",  
"\_score" : 1.0,  
"\_source":{"id":"af2d431d-b8bc-49cc-87cd-6bbe724ad4b8","authorId":"11cdfd7c-59a4-49a8-b3d9-62c4b49bf761","parent":"7cc810d9-1412-48d0-97bd-830543230624","subject":""Look ye; when Captain Ahab is all right, then this left arm of mine will be all right; not before."","message":""What do you know about him?"","createdDate":1424460748000,"modifiedDate":1424460748000,"deletedDate":null,"tenantId":"18b37386-dd53-496a-82b4-26ef56d4fdf9"}  
}, {  
"\_index" : "discussion",  
"\_type" : "discussion",  
"\_id" : "cb9ff3f1-9f27-4567-9625-a15cbfdd9544",  
"\_score" : 1.0,  
"\_source":{"id":"cb9ff3f1-9f27-4567-9625-a15cbfdd9544","authorId":"5c04f5bb-8d75-47bb-87a0-095ea3e05818","parent":"7cc810d9-1412-48d0-97bd-830543230624","subject":"said I.","message":""HE'S got enough, though, to make up for all deficiencies of that sort in other chaps," abruptly said the stranger, placing a nervous emphasis upon the word HE.","createdDate":1424460747000,"modifiedDate":1424460747000,"deletedDate":null,"tenantId":"18b37386-dd53-496a-82b4-26ef56d4fdf9"}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/858b2645-510e-4e40-8429-4e0fe7a87a2f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/858b2645-510e-4e40-8429-4e0fe7a87a2f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:30am UTC](https://discuss.elastic.co/t/aggregations-on-multiple-children-cause-wrong-doc-count/22305/4 "2017-07-06T00:30:44Z")

</div>


