# Add values of one key of all objects only where other keys have the required values and group by another value

**URL:** <https://discuss.elastic.co/t/add-values-of-one-key-of-all-objects-only-where-other-keys-have-the-required-values-and-group-by-another-value/188235>\
**Category:** Elasticsearch\
**Created:** [July 1, 2019, 5:56am UTC](https://discuss.elastic.co/t/add-values-of-one-key-of-all-objects-only-where-other-keys-have-the-required-values-and-group-by-another-value/188235 "2019-07-01T05:56:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShashankAC](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@ShashankAC](https://discuss.elastic.co/u/ShashankAC)\
**Post date:** [July 1, 2019, 5:56am UTC](https://discuss.elastic.co/t/add-values-of-one-key-of-all-objects-only-where-other-keys-have-the-required-values-and-group-by-another-value/188235/1 "2019-07-01T05:56:30Z")

</div>

I have about 5 key-value pairs in elasticsearch data per object, I want to add the values of certain keys(audio\_calls) of those objects where some other keys have the values I want(network\_type, weather). I want the result to be grouped by `area_num` .

For example

```auto
{
  .
  .
  "audio_calls":2000,
  "area": "a",
  "area_num": 1, 
  "network_type":"3g",
  "weather":"sunny"
  .
  .
}

{
  .
  .
  "audio_calls":1420,
  "area": "a",
  "area_num": 1, 
  "network_type":"4g",
  "weather":"stormy"
  .
  .
}

{
  .
  .
  "audio_calls": 1503, 
  "area": "c",
  "area_num": 2,
  "network_type":"4g",
  "weather":"cold"
} 

{
  .
  .
  "audio_calls": 4051, 
  "area": "c",
  "area_num": 2,
  "network_type":"4g",
  "weather":"sunny"
}

{
 . 
 .
 "audio_calls": 240,
 "area": "e",
 "area_num": 3,
 "network_type":"2g",
 "weather":"cloudy"
 .
 .
 .    
}
.
.
.

```

I am guessing I have to do this using aggregations but, I don't know how to conditionally add the `audio_calls` values, say add only those `audio_calls` where `network_type` is `3g` and `weather` is `sunny` .

With the (PHP)code I tried (Filtered Aggregation), aggregation simply added all the calls in all the objects without grouping by area\_num.

```auto
'aggs' => [
        'filter_by_conditions' => [
                'filter' => [
         'terms' => ['network_type' => $network_type],
         'terms' => ['weather' => $weather],
                            ],
                'aggs' => [
                    'sum_of_calls' => [
                       'sum' => ['field' => $call_type]
                                      ],
                    'aggs' => [
                         'terms' => ['field' => $area_num, 'size' => 1000]
                              ]
                          ]                   
          ]

```

where $call\_type = "audio\_calls"

The output does not group by area\_num, it simply adds all the audio\_calls and gives one number.

```auto
"aggregations":{"filter_by_conditions":  
{"doc_count":1665,"sum_of_calls":{"value":31464}}}}

```

---

<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 29, 2019, 5:56am UTC](https://discuss.elastic.co/t/add-values-of-one-key-of-all-objects-only-where-other-keys-have-the-required-values-and-group-by-another-value/188235/2 "2019-07-29T05:56:41Z")

</div>

Closed on July 29, 2019, 5:56am UTC

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