# Cannot divide multiple context values in Elasticsearch watcher

**URL:** <https://discuss.elastic.co/t/cannot-divide-multiple-context-values-in-elasticsearch-watcher/100652>\
**Category:** Elasticsearch\
**Created:** [September 15, 2017, 8:20am UTC](https://discuss.elastic.co/t/cannot-divide-multiple-context-values-in-elasticsearch-watcher/100652 "2017-09-15T08:20:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpchenot](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@jpchenot](https://discuss.elastic.co/u/jpchenot)\
**Post date:** [September 15, 2017, 8:20am UTC](https://discuss.elastic.co/t/cannot-divide-multiple-context-values-in-elasticsearch-watcher/100652/1 "2017-09-15T08:20:53Z")

</div>

I have created a watch in Elasticsearch to alert me if the ratio of http errors is greater than 15% of total requests over 60 minutes. I am using chain inputs to generate the dividend and divisor values for my ratio calculation.

In my condition I am using scripting to do the division and check if it is greater than my ratio.

However, whenever I use 2 ctx parameters to do the division, it always equals to zero.

If I play with it and only use one of ctx param, then it works fine.

It seems that we cannot use 2 ctx params in a condition.

Does anyone know how to get around this?

Below is my watch.

Thanks.

```
{
  "trigger" : {
    "schedule" : {
      "interval" : "5m"
    }
  },
  "input" : {
    "chain":{
      "inputs": [
      {
        "first": {
          "search" : {
            "request" : {
              "indices" : ["logstash-*"],
              "body" : {
                "query" : { 
                  "bool":{
                    "must": [
                      {
                      "match" : {"d.uri": "xxxxxxxx"}
                      },
                      { 
                      "match" : {"topic": "xxxxxxxx"}
                      }
                    ],
                    "filter": {
                      "range": {
                        "@timestamp": {
                          "gte": "now-60m"
                        }
                      }
                    }
                  }
                }
              }
            },
            "extract": ["hits.total"]
          }
        }
      },
      {
        "second": {
          "search" : {
            "request" : {
              "body" : {
                "query" : { 
                  "bool":{
                    "must": [
                      {
                      "match" : {"d.uri": "xxxxxxxx"}
                      },
                      { 
                      "match" : {"topic": "xxxxxxxx"}
                      },
                      { 
                      "match" : {"d.status": "401"}
                      }
                    ],
                    "filter": {
                      "range": {
                        "@timestamp": {
                          "gte": "now-60m"
                        }
                      }
                    }
                  }
                }
              }
            },
            "extract": ["hits.total"]
          }
        }
      }
      ]
    }
  },
  "condition" : {
    "script" : {
      "source" : "return (ctx.payload.second.hits.total / ctx.payload.first.hits.total) == 0"
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![jpchenot](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@jpchenot](https://discuss.elastic.co/u/jpchenot)\
**Post date:** [September 15, 2017, 2:09pm UTC](https://discuss.elastic.co/t/cannot-divide-multiple-context-values-in-elasticsearch-watcher/100652/2 "2017-09-15T14:09:32Z")

</div>

The issue comes in fact from the fact that I was doing an integer division to get to a ratio in the form of 0.xx. I reversed the operation and it is working fine.

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [September 18, 2017, 11:40pm UTC](https://discuss.elastic.co/t/cannot-divide-multiple-context-values-in-elasticsearch-watcher/100652/3 "2017-09-18T23:40:33Z")

</div>

Thanks for updating with your solution!

---

<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:** [October 16, 2017, 11:40pm UTC](https://discuss.elastic.co/t/cannot-divide-multiple-context-values-in-elasticsearch-watcher/100652/4 "2017-10-16T23:40:55Z")

</div>

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