title: Elasticsearch: sum of each element of array
body: <p>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:</p>
  
  <pre><code>{
    "arr": [5, 4, 3, 2, 1],
    "name": "test"
  }
  {
    "arr": [4, 3, 2, 1, 0],
    "name": "test"
  }
  {
    "arr": [1, 0, 0, 0, 0],
    "name": "test"
  }
  </code></pre>
  
  <p>I want to use an aggregation (or some other es method) to return</p>
  
  <pre><code>{
    "arr": [10, 7, 5, 3, 1]
  }
  </code></pre>
  
  <p>The aggregation that has come the closest is</p>
  
  <pre><code>{
    "size":0,
    "aggs": {
      "sum_by_index": {
          "filter": {
            "terms": {
              "name": ["test"]
            }
          }
        },
        "aggs": {
          "names": {
            "terms": {
              "field": "name"
            },
            "aggs": {
              "arrs": {
                "terms": {
                  "field": "arr"
                }
              }
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>But this results in a sum per <em>value</em> in the array, where I want the sum per <em>index</em> of the array. Thoughts anyone?</p>