# How to sum a filed in top N document

**URL:** <https://discuss.elastic.co/t/how-to-sum-a-filed-in-top-n-document/52530>\
**Category:** Elasticsearch\
**Created:** [June 12, 2016, 7:28am UTC](https://discuss.elastic.co/t/how-to-sum-a-filed-in-top-n-document/52530 "2016-06-12T07:28:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyingivy](https://avatars.discourse-cdn.com/v4/letter/l/71e660/32.png) [@lyingivy](https://discuss.elastic.co/u/lyingivy)\
**Post date:** [June 12, 2016, 7:28am UTC](https://discuss.elastic.co/t/how-to-sum-a-filed-in-top-n-document/52530/1 "2016-06-12T07:28:21Z")

</div>

hi all , i have a problem with es.  
i find N documentfrom es via order by a filed--logtime, and then i want to sum a filed--delta in those top n document.  
i tried top-hits ,but this agg can't add aggs any more.  
and i try use code blow.  
But i can't get the right i want.  
please help me.  
here is my code:  
{  
"size" : 0,  
"query" : {  
"bool" : {  
"must" : {  
"match" : {  
"userid" : {  
"query" : "447494679",  
"type" : "boolean"  
}  
}  
}  
}  
},  
"sort" : [ {  
"logtime" : {  
"order" : "desc"  
}  
} ],  
"aggs" : {  
"term" : {  
"terms" : {  
"field" : "userid",  
"size":10  
},  
"aggs" : {  
"sum" : {  
"sum" : {  
"field":"delta"  
}  
}  
}  
}  
}  
}'

---

<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, 10:44pm UTC](https://discuss.elastic.co/t/how-to-sum-a-filed-in-top-n-document/52530/2 "2017-07-05T22:44:34Z")

</div>


