# Group by field and then sum the groups

**URL:** <https://discuss.elastic.co/t/group-by-field-and-then-sum-the-groups/19103>\
**Category:** Elasticsearch\
**Created:** [August 5, 2014, 8:11pm UTC](https://discuss.elastic.co/t/group-by-field-and-then-sum-the-groups/19103 "2014-08-05T20:11:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cameron\_Barker](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@Cameron\_Barker](https://discuss.elastic.co/u/Cameron_Barker)\
**Post date:** [August 5, 2014, 8:11pm UTC](https://discuss.elastic.co/t/group-by-field-and-then-sum-the-groups/19103/1 "2014-08-05T20:11:59Z")

</div>

Hi all,

I have an elastic database of posts, each post has a _user\_id_ and has  
_likes_ field. My goal is to output for a query how many likes in total  
each user has.

I wondered if any one had any advice/direction I could take to achieve this?

input:  
{user\_id: 10, likes: 20}  
{user\_id: 9, likes: 10}  
{user\_id: 10, likes: 25}  
{user\_id: 9, likes: 15}

output:  
User: 10 likes: 45  
User: 9 likes: 25

--  
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/d016c5d0-ad03-488e-9c7b-73cf85999e10%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d016c5d0-ad03-488e-9c7b-73cf85999e10%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Tihomir\_Lichev1](https://avatars.discourse-cdn.com/v4/letter/t/f08c70/32.png) [@Tihomir\_Lichev1](https://discuss.elastic.co/u/Tihomir_Lichev1)\
**Post date:** [August 6, 2014, 5:32am UTC](https://discuss.elastic.co/t/group-by-field-and-then-sum-the-groups/19103/2 "2014-08-06T05:32:41Z")

</div>

You can use aggregations:  
{  
"aggs": {  
"user\_likes": {  
"terms": {  
"field": "user\_id"  
},  
"aggs": {  
"likes\_sum": {  
"terms": {  
"field": "likes"  
}  
}  
}  
}  
}  
}

05 август 2014, вторник, 23:11:59 UTC+3, Cameron Barker написа:

> Hi all,
> 
> I have an elastic database of posts, each post has a _user\_id_ and has  
> _likes_ field. My goal is to output for a query how many likes in total  
> each user has.
> 
> I wondered if any one had any advice/direction I could take to achieve  
> this?
> 
> input:  
> {user\_id: 10, likes: 20}  
> {user\_id: 9, likes: 10}  
> {user\_id: 10, likes: 25}  
> {user\_id: 9, likes: 15}
> 
> output:  
> User: 10 likes: 45  
> User: 9 likes: 25

--  
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/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [August 6, 2014, 7:06am UTC](https://discuss.elastic.co/t/group-by-field-and-then-sum-the-groups/19103/3 "2014-08-06T07:06:19Z")

</div>

Hi,

I think second "aggs" use "sum" instead of "terms", in "likes\_sum".

2014-08-06 14:32 GMT+09:00 Tihomir Lichev [shoteff@gmail.com](mailto:shoteff@gmail.com):

> You can use aggregations:  
> {  
> "aggs": {  
> "user\_likes": {  
> "terms": {  
> "field": "user\_id"  
> },  
> "aggs": {  
> "likes\_sum": {  
> "terms": {  
> "field": "likes"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> 05 август 2014, вторник, 23:11:59 UTC+3, Cameron Barker написа:
> 
> > Hi all,
> > 
> > I have an elastic database of posts, each post has a _user\_id_ and has  
> > _likes_ field. My goal is to output for a query how many likes in total  
> > each user has.
> > 
> > I wondered if any one had any advice/direction I could take to achieve  
> > this?
> > 
> > input:  
> > {user\_id: 10, likes: 20}  
> > {user\_id: 9, likes: 10}  
> > {user\_id: 10, likes: 25}  
> > {user\_id: 9, likes: 15}
> > 
> > output:  
> > User: 10 likes: 45  
> > User: 9 likes: 25
> > 
> > --  
> > 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/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

## --

Jun Ohtani  
blog : [http://blog.johtani.info](http://blog.johtani.info)

--  
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/CAPW8A5zuHq6a-SF7N0zCtgPQmTSFhkcgzqOFT2vodukm3uf70A%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPW8A5zuHq6a-SF7N0zCtgPQmTSFhkcgzqOFT2vodukm3uf70A%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Tihomir\_Lichev1](https://avatars.discourse-cdn.com/v4/letter/t/f08c70/32.png) [@Tihomir\_Lichev1](https://discuss.elastic.co/u/Tihomir_Lichev1)\
**Post date:** [August 6, 2014, 7:49am UTC](https://discuss.elastic.co/t/group-by-field-and-then-sum-the-groups/19103/4 "2014-08-06T07:49:57Z")

</div>

Thanks! You're absolutely right. Copy/paste error 🙂

{  
"aggs": {  
"user\_likes": {  
"terms": {  
"field": "user\_id"  
},  
"aggs": {  
"likes\_sum": {  
"sum": {  
"field": "likes"  
}  
}  
}  
}  
}  
}

06 август 2014, сряда, 10:06:38 UTC+3, Jun Ohtani написа:

> Hi,
> 
> I think second "aggs" use "sum" instead of "terms", in "likes\_sum".
> 
> 2014-08-06 14:32 GMT+09:00 Tihomir Lichev \<[sho...@gmail.com](mailto:sho...@gmail.com) \<javascript:\>\>  
> :
> 
> > You can use aggregations:  
> > {  
> > "aggs": {  
> > "user\_likes": {  
> > "terms": {  
> > "field": "user\_id"  
> > },  
> > "aggs": {  
> > "likes\_sum": {  
> > "terms": {  
> > "field": "likes"  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > 05 август 2014, вторник, 23:11:59 UTC+3, Cameron Barker написа:
> > 
> > > Hi all,
> > > 
> > > I have an elastic database of posts, each post has a _user\_id_ and has  
> > > _likes_ field. My goal is to output for a query how many likes in  
> > > total each user has.
> > > 
> > > I wondered if any one had any advice/direction I could take to achieve  
> > > this?
> > > 
> > > input:  
> > > {user\_id: 10, likes: 20}  
> > > {user\_id: 9, likes: 10}  
> > > {user\_id: 10, likes: 25}  
> > > {user\_id: 9, likes: 15}
> > > 
> > > output:  
> > > User: 10 likes: 45  
> > > User: 9 likes: 25
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> ## --
> 
> Jun Ohtani  
> blog : [http://blog.johtani.info](http://blog.johtani.info)

--  
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/5631ceaa-8ef6-4fb8-8431-b42c032db953%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5631ceaa-8ef6-4fb8-8431-b42c032db953%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Cameron\_Barker](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@Cameron\_Barker](https://discuss.elastic.co/u/Cameron_Barker)\
**Post date:** [August 6, 2014, 4:19pm UTC](https://discuss.elastic.co/t/group-by-field-and-then-sum-the-groups/19103/5 "2014-08-06T16:19:39Z")

</div>

This worked perfectly! Thank you for your help.

On Wednesday, August 6, 2014 3:49:57 AM UTC-4, Tihomir Lichev wrote:

> Thanks! You're absolutely right. Copy/paste error 🙂
> 
> {  
> "aggs": {  
> "user\_likes": {  
> "terms": {  
> "field": "user\_id"  
> },  
> "aggs": {  
> "likes\_sum": {  
> "sum": {  
> "field": "likes"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> 06 август 2014, сряда, 10:06:38 UTC+3, Jun Ohtani написа:
> 
> > Hi,
> > 
> > I think second "aggs" use "sum" instead of "terms", in "likes\_sum".
> > 
> > 2014-08-06 14:32 GMT+09:00 Tihomir Lichev [sho...@gmail.com](mailto:sho...@gmail.com):
> > 
> > > You can use aggregations:  
> > > {  
> > > "aggs": {  
> > > "user\_likes": {  
> > > "terms": {  
> > > "field": "user\_id"  
> > > },  
> > > "aggs": {  
> > > "likes\_sum": {  
> > > "terms": {  
> > > "field": "likes"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > 05 август 2014, вторник, 23:11:59 UTC+3, Cameron Barker написа:
> > > 
> > > > Hi all,
> > > > 
> > > > I have an elastic database of posts, each post has a _user\_id_ and has  
> > > > _likes_ field. My goal is to output for a query how many likes in  
> > > > total each user has.
> > > > 
> > > > I wondered if any one had any advice/direction I could take to achieve  
> > > > this?
> > > > 
> > > > input:  
> > > > {user\_id: 10, likes: 20}  
> > > > {user\_id: 9, likes: 10}  
> > > > {user\_id: 10, likes: 25}  
> > > > {user\_id: 9, likes: 15}
> > > > 
> > > > output:  
> > > > User: 10 likes: 45  
> > > > User: 9 likes: 25
> > > > 
> > > > --  
> > > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > > To view this discussion on the web visit  
> > > > [https://groups.google.com/d/msgid/elasticsearch/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com)  
> > > > [https://groups.google.com/d/msgid/elasticsearch/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/7bec5f9a-16cf-4723-87a6-7e95de45d0ea%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > .
> > > 
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > ## --
> > 
> > Jun Ohtani  
> > blog : [http://blog.johtani.info](http://blog.johtani.info)

--  
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/fb530eeb-0a9f-4460-905a-92f0d74fa5ff%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fb530eeb-0a9f-4460-905a-92f0d74fa5ff%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, 1:10am UTC](https://discuss.elastic.co/t/group-by-field-and-then-sum-the-groups/19103/6 "2017-07-06T01:10:23Z")

</div>


