# Sum of each element of array

**URL:** <https://discuss.elastic.co/t/sum-of-each-element-of-array/291009>\
**Category:** Elasticsearch\
**Created:** [December 6, 2021, 5:09am UTC](https://discuss.elastic.co/t/sum-of-each-element-of-array/291009 "2021-12-06T05:09:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimple\_Goyal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dimple_goyal/32/77151_2.png) [@Dimple\_Goyal](https://discuss.elastic.co/u/Dimple_Goyal)\
**Post date:** [December 6, 2021, 5:09am UTC](https://discuss.elastic.co/t/sum-of-each-element-of-array/291009/1 "2021-12-06T05:09:36Z")

</div>

**I'm trying to use an Elasticsearch aggregation to 'merge' an array of integers based on a set of filters. I have documents that look like:**

```auto
{
  "arr": [5, 4, 3, 2, 1],
  "name": "test"
}
{
  "arr": [4, 3, 2, 1, 0],
  "name": "test"
}
{
  "arr": [1, 0, 0, 0, 0],
  "name": "test"
}

```

**I want to use an aggregation (or some other es method) to return**

```auto
{
  "arr": [10, 7, 5, 3, 1]
}

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [December 6, 2021, 5:11am UTC](https://discuss.elastic.co/t/sum-of-each-element-of-array/291009/2 "2021-12-06T05:11:28Z")

</div>

The core problem here is that arrays are never guaranteed to be ordered, so each time you got the results they might be different.

---

<div class="post-metadata">

**Author:** ![Dimple\_Goyal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dimple_goyal/32/77151_2.png) [@Dimple\_Goyal](https://discuss.elastic.co/u/Dimple_Goyal)\
**Post date:** [December 6, 2021, 5:13am UTC](https://discuss.elastic.co/t/sum-of-each-element-of-array/291009/3 "2021-12-06T05:13:52Z")

</div>

can you suggest me a way to achieve this?

---

<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:** [January 3, 2022, 5:14am UTC](https://discuss.elastic.co/t/sum-of-each-element-of-array/291009/4 "2022-01-03T05:14:10Z")

</div>

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