# Sum aggregation on array values

**URL:** <https://discuss.elastic.co/t/sum-aggregation-on-array-values/28365>\
**Category:** Elasticsearch\
**Created:** [August 31, 2015, 5:09pm UTC](https://discuss.elastic.co/t/sum-aggregation-on-array-values/28365 "2015-08-31T17:09:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lokeshhctm](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@lokeshhctm](https://discuss.elastic.co/u/lokeshhctm)\
**Post date:** [August 31, 2015, 5:09pm UTC](https://discuss.elastic.co/t/sum-aggregation-on-array-values/28365/1 "2015-08-31T17:09:43Z")

</div>

I have an index with several columns having values like number of requests and a few columns are array fields.  
I need to have sum of value column with group by of values in array column.  
e.g. rows are like:

rowID, requests, array fields  
1, 50, [a,b,c]  
2, 100, [a,b]  
3, 30 , [b,c]

So i want result as:

a = 150  
b = 180  
c = 80

The query i am trying always takes a lot of time. The query is:

{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {}  
}  
},  
"size": 0,  
"aggs": {  
"2": {  
"terms": {  
"field": "ARRAY COLUMN",  
"size": 400,  
"order": {  
"1": "desc"  
}  
},  
"aggs": {  
"1": {  
"sum": {  
"field": "VALUES COLUMN"  
}  
}  
}  
}  
}  
}

What is the best way to have this aggregation to make it faster.

---

<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 5, 2017, 11:53pm UTC](https://discuss.elastic.co/t/sum-aggregation-on-array-values/28365/2 "2017-07-05T23:53:02Z")

</div>


