# Group by on the sum of aggregation

**URL:** <https://discuss.elastic.co/t/group-by-on-the-sum-of-aggregation/128902>\
**Category:** Kibana\
**Created:** [April 20, 2018, 3:03pm UTC](https://discuss.elastic.co/t/group-by-on-the-sum-of-aggregation/128902 "2018-04-20T15:03:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![didinyah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/didinyah/32/30859_2.png) [@didinyah](https://discuss.elastic.co/u/didinyah)\
**Post date:** [April 20, 2018, 3:03pm UTC](https://discuss.elastic.co/t/group-by-on-the-sum-of-aggregation/128902/1 "2018-04-20T15:03:04Z")

</div>

Hi,

I would like to know if there's a way to do an aggregation on the result of the sum.

Let's say I have 8 documents.

6 of them have 1 revision  
2 of them have 2 revisions

I want to get :

1 : 6 hits  
2 : 2 hits

This is what I managed to get for the moment :  
[https://imgur.com/pTSsrHT](https://imgur.com/pTSsrHT)  
We can see the number of revision for EACH document, that's already cool but not enough.

And this is what I would like (the black numbers indicating the number of "hits", remplace 8 by 6)  
[https://imgur.com/mT57mhe](https://imgur.com/mT57mhe)

Thanks

---

<div class="post-metadata">

**Author:** ![ppisljar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ppisljar/32/11588_2.png) [@ppisljar](https://discuss.elastic.co/u/ppisljar)\
**Post date:** [April 20, 2018, 3:58pm UTC](https://discuss.elastic.co/t/group-by-on-the-sum-of-aggregation/128902/2 "2018-04-20T15:58:44Z")

</div>

how is revision indicated in your documents ? i am assuming something like this:

```auto
{ id: 'doc1', rev: 1 }
{ id: 'doc2', rev: 1 }
{ id: 'doc3', rev: 1 }
{ id: 'doc1', rev: 2 }

```

you could try something like:

- pie chart
- split slices, term agg, rev field
- leave count as a metric agg

---

<div class="post-metadata">

**Author:** ![didinyah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/didinyah/32/30859_2.png) [@didinyah](https://discuss.elastic.co/u/didinyah)\
**Post date:** [April 23, 2018, 8:33am UTC](https://discuss.elastic.co/t/group-by-on-the-sum-of-aggregation/128902/3 "2018-04-23T08:33:10Z")

</div>

Hi, Thank for the answer.

Actually the revision field is a personal scripted field by doing the sum of a field :  
`if(doc['operation'].value == "Revisionchange") { return 1; } return 0;`

So if I'm doing the sum by splicing with docID it will give me the right number right ?

When I do the pie chart, splice by this scripted field and leave count as a metric I only have this pie : [https://imgur.com/a/bVKVrY0](https://imgur.com/a/bVKVrY0)

Must I change the scripted field or do another operation ?

---

<div class="post-metadata">

**Author:** ![ppisljar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ppisljar/32/11588_2.png) [@ppisljar](https://discuss.elastic.co/u/ppisljar)\
**Post date:** [April 24, 2018, 1:22pm UTC](https://discuss.elastic.co/t/group-by-on-the-sum-of-aggregation/128902/4 "2018-04-24T13:22:16Z")

</div>

it doesn't seem you sum the revisions here ?  
anyway, something like this won't be possible with pie chart at the moment ...

you would need to reindex your data so it contains the number of revisions as a field in your documents.

you could reindex your data in a new index by:

- spliting on document id in the first index
- getting the revision number by summing the `revision` field
- index this in a new index

---

<div class="post-metadata">

**Author:** ![didinyah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/didinyah/32/30859_2.png) [@didinyah](https://discuss.elastic.co/u/didinyah)\
**Post date:** [April 27, 2018, 3:39pm UTC](https://discuss.elastic.co/t/group-by-on-the-sum-of-aggregation/128902/5 "2018-04-27T15:39:37Z")

</div>

Okay so I have to reindex all the data and add the field documentVersion after all ^^'  
Thank you for your help !

---

<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:** [May 25, 2018, 3:39pm UTC](https://discuss.elastic.co/t/group-by-on-the-sum-of-aggregation/128902/6 "2018-05-25T15:39:44Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
