# Inconsistent results when aggregate by field from array

**URL:** <https://discuss.elastic.co/t/inconsistent-results-when-aggregate-by-field-from-array/22976>\
**Category:** Elasticsearch\
**Created:** [March 30, 2015, 9:18am UTC](https://discuss.elastic.co/t/inconsistent-results-when-aggregate-by-field-from-array/22976 "2015-03-30T09:18:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yana2301](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yana2301/32/133248_2.png) [@yana2301](https://discuss.elastic.co/u/yana2301)\
**Post date:** [March 30, 2015, 9:18am UTC](https://discuss.elastic.co/t/inconsistent-results-when-aggregate-by-field-from-array/22976/1 "2015-03-30T09:18:47Z")

</div>

Hi All,

I have array of objects in each document in index. I want to group by and retrieve stats based on fields from this array. But stats is calculated based on sum of all values in this array.  
Could you please suggest, is there mistake in my query or expected behavior for now?  
Example of my document:  
{  
"city":"London",  
"arrayField":[  
{  
"groupByField":"value1",  
"statsField":10  
},  
{  
"groupByField":"value1",  
"statsField":20  
},  
{  
"groupByField":"value2",  
"statsField":10  
},  
{  
"groupByField":"value2",  
"statsField":5  
}  
]  
}

Example of my query:

{  
"size" : 0,  
"aggregations" : {  
"filter" : {  
"filter" : {  
"bool" : {  
"must" : {  
"match\_all" : { }  
}  
}  
},  
"aggregations" : {  
"terms" : {  
"terms" : {  
"field" : "arrayField.groupByField",  
"size" : 100000  
},  
"aggregations" : {  
"districts.population" : {  
"stats" : {  
"field" : "arrayField.statsField"  
}  
}  
}  
}  
}  
}  
}  
}

I expect to get results:  
for group1: sum = 30  
for group2:sum = 15

I actually get:

for group1:sum = 45  
for group2:sum = 45

---

<div class="post-metadata">

**Author:** ![thanodnl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thanodnl/32/44907_2.png) [@thanodnl](https://discuss.elastic.co/u/thanodnl)\
**Post date:** [April 14, 2015, 4:46pm UTC](https://discuss.elastic.co/t/inconsistent-results-when-aggregate-by-field-from-array/22976/2 "2015-04-14T16:46:10Z")

</div>

Hi,

You want to look at nested objects:

> **[Nested Objects | Elasticsearch: The Definitive Guide \[master\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/guide/master/nested-objects.html)**

-- Nils  
Tip: try formatting your post, it was hard to read.

On Tuesday, March 31, 2015 at 4:37:40 AM UTC+2, Iana Bondarskaia wrote:

> ## Hi All, I have array of objects in each document in index. I want to group by and retrieve stats based on fields from this array. But stats is calculated based on sum of all values in this array. Could you please suggest, is there mistake in my query or expected behavior for now? Example of my document: { "city":"London", "arrayField":[{ "groupByField":"value1", "statsField":10 }, { "groupByField":"value1", "statsField":20 }, { "groupByField":"value2", "statsField":10 }, { "groupByField":"value2", "statsField":5 }] } Example of my query: { "size" : 0, "aggregations" : { "filter" : { "filter" : { "bool" : { "must" : { "match\_all" : { } } } }, "aggregations" : { "terms" : { "terms" : { "field" : "arrayField.groupByField", "size" : 100000 }, "aggregations" : { "districts.population" : { "stats" : { "field" : "arrayField.statsField" } } } } } } } } I expect to get results: for group1: sum = 30 for group2:sum = 15 I actually get: for group1:sum = 45 for group2:sum = 45
> 
> View this message in context: Inconsistent results when aggregate by  
> field from array  
> [http://elasticsearch-users.115913.n3.nabble.com/Inconsistent-results-when-aggregate-by-field-from-array-tp4072818.html](http://elasticsearch-users.115913.n3.nabble.com/Inconsistent-results-when-aggregate-by-field-from-array-tp4072818.html)  
> Sent from the Elasticsearch Users mailing list archive  
> [http://elasticsearch-users.115913.n3.nabble.com/](http://elasticsearch-users.115913.n3.nabble.com/) at [Nabble.com](http://Nabble.com).

--  
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/de0251ab-333f-4c3d-a5ec-480106ccbb39%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/de0251ab-333f-4c3d-a5ec-480106ccbb39%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:19am UTC](https://discuss.elastic.co/t/inconsistent-results-when-aggregate-by-field-from-array/22976/3 "2017-07-06T00:19:51Z")

</div>


