# Group by and sum operation for fields

**URL:** <https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181>\
**Category:** Elasticsearch\
**Created:** [February 9, 2018, 7:13am UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181 "2018-02-09T07:13:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![cekeriya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cekeriya/32/27594_2.png) [@cekeriya](https://discuss.elastic.co/u/cekeriya)\
**Post date:** [February 9, 2018, 7:13am UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181/1 "2018-02-09T07:13:04Z")

</div>

First of all I want to tell you that I am new at ElasticSearch

I have a JSON in following format. I want to group by "CallTo" field and sum of "Count" value for each "CallTo" I will use this values for visulation in Kibana

My first question is what my index should be for group by and sum operation that I mentioned? And my second question is what is the query?

My Json

```
{
  "Labels": "qwerty",
  "Type": "type1",
  "Id": "id12345",
  "FieldName": [
    {
      "CallTo": "Tom",
      "Count": 2
    },
    {
      "CallTo": "Jessica",
      "Count": 4
    },
    {
      "RegionCode": "US",
      "Count": 1
    },
    {
      "RegionCode": "DE",
      "Count": 5
    },
    {
      "CallCategory": "K1",
      "Count": 6
    }
  ],
  "OtherField": [
    {
      "Key": "bin5",
      "Value": 0
    },
    {
      "Key": "bin1",
      "Value": 0
    },
    {
      "Key": "bin3",
      "Value": 2
    },
    {
      "Key": "binOther",
      "Value": 0
    }
  ],
  "XField": [
    {
      "Key": "bin50000",
      "Value": 1
    },
    {
      "Key": "bin10000",
      "Value": 3
    },
    {
      "Key": "bin30000",
      "Value": 4
    },
    {
      "Key": "binOther",
      "Value": 7
    }
  ]
}

```

My expected result my be like this

```
{
  {
    "CallTo": "Tom",
    "Count": 23
  },
  {
    "CallTo": "Jessica",
    "Count": 44
  },
  {
    "RegionCode": "US",
    "Count": 18
  },
  {
    "RegionCode": "DE",
    "Count": 58
  },
  {
    "CallCategory": "K1",
    "Count": 46
  }
}

```

Also I am open for alternative solution even change Json format

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 9, 2018, 7:34am UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181/2 "2018-02-09T07:34:35Z")

</div>

I wonder if you should instead index every single "call" instead of indexing a list of calls.  
But for that it would be better to explain what the use case is about and what the documents represent IMO.

---

<div class="post-metadata">

**Author:** ![cekeriya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cekeriya/32/27594_2.png) [@cekeriya](https://discuss.elastic.co/u/cekeriya)\
**Post date:** [February 9, 2018, 8:07am UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181/3 "2018-02-09T08:07:54Z")

</div>

It is an another solution but I guess this approach may causes some performance problem or others(I can't be sure about that because I am new in Elasticsearch).

FieldName array size could be millions. If I index every single "call" It will cause anything?

Can you show your solution for input JSON please ?

Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 9, 2018, 8:36am UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181/4 "2018-02-09T08:36:08Z")

</div>

> I guess this approach may causes some performance problem

I think the total opposite.

---

<div class="post-metadata">

**Author:** ![cekeriya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cekeriya/32/27594_2.png) [@cekeriya](https://discuss.elastic.co/u/cekeriya)\
**Post date:** [February 9, 2018, 10:07am UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181/5 "2018-02-09T10:07:41Z")

</div>

What is your input json format than?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 9, 2018, 10:38am UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181/6 "2018-02-09T10:38:39Z")

</div>

i'm not sure because I don't know a lot about your data. But something like:

```auto
PUT calls/doc/1
{
  "Labels": "qwerty",
  "Type": "type1",
  "Id": "id12345",
  "CallTo": "Tom",
  "RegionCode": "US",
  "CallCategory": "K1"
}

```

---

<div class="post-metadata">

**Author:** ![cekeriya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cekeriya/32/27594_2.png) [@cekeriya](https://discuss.elastic.co/u/cekeriya)\
**Post date:** [February 9, 2018, 11:39am UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181/7 "2018-02-09T11:39:29Z")

</div>

First of all I am really appreciate for your answer, but your proposition is not proper for my case.  
Let me explain deeply what is the meaning of my input json which I attach in my answer

I work on some voip call event to get some valuable data during a time interval. In specified time interval I start an aggregation policy to get JSON, I mean storing aggregation result JSON is more important than storing all call event.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 9, 2018, 4:37pm UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181/8 "2018-02-09T16:37:33Z")

</div>

> In specified time interval I start an aggregation policy to get JSON, I mean storing aggregation result JSON is more important than storing all call event.

Why not doing the aggregation in real time in elasticsearch instead? Using whatever period you want...

---

<div class="post-metadata">

**Author:** ![cekeriya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cekeriya/32/27594_2.png) [@cekeriya](https://discuss.elastic.co/u/cekeriya)\
**Post date:** [February 12, 2018, 5:25am UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181/9 "2018-02-12T05:25:15Z")

</div>

Aggregation process is out of my control. I get JSON from ElasticSearch for group by and sum operations etc. My responsibility is on only output jsons(attached above). Therefore we should focus on it

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 12, 2018, 12:39pm UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181/10 "2018-02-12T12:39:20Z")

</div>

I thought you said that you can control how data is produced:

> Also I am open for alternative solution even change Json format

Can't you?

---

<div class="post-metadata">

**Author:** ![cekeriya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cekeriya/32/27594_2.png) [@cekeriya](https://discuss.elastic.co/u/cekeriya)\
**Post date:** [February 13, 2018, 12:48pm UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181/11 "2018-02-13T12:48:59Z")

</div>

I use this approach and it works for me now =)  
Thanks

---

<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:** [March 13, 2018, 1:01pm UTC](https://discuss.elastic.co/t/group-by-and-sum-operation-for-fields/119181/13 "2018-03-13T13:01:15Z")

</div>

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