# How to sum two variable in REST API (script)

**URL:** <https://discuss.elastic.co/t/how-to-sum-two-variable-in-rest-api-script/114359>\
**Category:** Elasticsearch\
**Created:** [January 6, 2018, 9:07am UTC](https://discuss.elastic.co/t/how-to-sum-two-variable-in-rest-api-script/114359 "2018-01-06T09:07:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![f26227279](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/f26227279/32/21296_2.png) [@f26227279](https://discuss.elastic.co/u/f26227279)\
**Post date:** [January 6, 2018, 9:07am UTC](https://discuss.elastic.co/t/how-to-sum-two-variable-in-rest-api-script/114359/1 "2018-01-06T09:07:05Z")

</div>

such as title, I want to sum two variable using script syntax.

such as sum download1+download2 and upload1+upload2:

```
"aggs": {
     "download1" : {
        "filter" : { "term": { "IPV4_DST_ADDR":"120.127.160.91"} },
        "aggs" : {
            "downlod_bytes" : { "sum" : { "field" : "IN_BYTES" } }
        }
    },
    "download2" : {
        "filter" : { "term": { "IPV4_SRC_ADDR":"120.127.160.91"} },
        "aggs" : {
            "downlod_bytes" : { "sum" : { "field" : "OUT_BYTES" } }
        }
    },"upload1" : {
        "filter" : { "term": { "IPV4_DST_ADDR":"120.127.160.91"} },
        "aggs" : {
            "upload_bytes" : { "sum" : { "field" : "OUT_BYTES" } }
        }
    },"upload2" : {
        "filter" : { "term": { "IPV4_SRC_ADDR":"120.127.160.91"} },
        "aggs" : {
            "upload_bytes" : { "sum" : { "field" : "IN_BYTES" } }
        },
  "aggs":{
    "sum_bytes": {
      "sum": {
        "script": {
          "source": "download1 + download2"//sum two variable.but I don't know how to write correct code here.
        }
      }
     }
    }
   }

```

thank you in advance!

---

<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:** [February 3, 2018, 9:13am UTC](https://discuss.elastic.co/t/how-to-sum-two-variable-in-rest-api-script/114359/2 "2018-02-03T09:13:07Z")

</div>

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