# How to compare two aggregation output in watcher in condition

**URL:** https://discuss.elastic.co/t/how-to-compare-two-aggregation-output-in-watcher-in-condition/168388
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting
**Created:** [February 14, 2019, 10:34am UTC](https://discuss.elastic.co/t/how-to-compare-two-aggregation-output-in-watcher-in-condition/168388 "2019-02-14T10:34:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![RamyaGowda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramyagowda/32/27440_2.png) [@RamyaGowda](https://discuss.elastic.co/u/RamyaGowda)
#### Post date: [February 14, 2019, 10:34am UTC](https://discuss.elastic.co/t/how-to-compare-two-aggregation-output-in-watcher-in-condition/168388/1 "2019-02-14T10:34:48Z")

</div>

I need to compare two aggregation queries output array n watcher condition, i have used chain input to execute my queries .  
not properly getting how can compare those two bucket arrays in watcher condtion.  
Could i get any help from you !

This is my watcher:

> > {  
> > "trigger": {  
> > "schedule": {  
> > "interval": "30m"  
> > }  
> > },  
> > "input": {  
> > "chain": {  
> > "inputs": [  
> > {  
> > "first": {  
> > "search": {  
> > "request": {  
> > "search\_type": "query\_then\_fetch",  
> > "indices": [  
> > ".monitoring-beats-6-2019.02.14"  
> > ],  
> > "types": ,  
> > "body": {  
> > "query": {  
> > "bool": {  
> > "must": [  
> > {  
> > "match\_all": {}  
> > }  
> > ]  
> > }  
> > },  
> > "aggs": {  
> > "AggregatedData": {  
> > "terms": {  
> > "field": "beats\_stats.beat.name"  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > },  
> > {  
> > "second": {  
> > "search": {  
> > "request": {  
> > "search\_type": "query\_then\_fetch",  
> > "indices": [  
> > ".monitoring-beats-6-2019.02.14"  
> > ],  
> > "types": ,  
> > "body": {  
> > "query": {  
> > "bool": {  
> > "must": [  
> > {  
> > "match\_all": {}  
> > }  
> > ],  
> > "filter": {  
> > "range": {  
> > "timestamp": {  
> > "gte": "now-5m",  
> > "lte": "now"  
> > }  
> > }  
> > }  
> > }  
> > },  
> > "aggs": {  
> > "MinuteAggregateData": {  
> > "terms": {  
> > "field": "beats\_stats.beat.name"  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > ]  
> > }  
> > },  
> > "condition": {  
> > "script": {  
> > "source": "return ctx.payload.aggregations",  
> > "lang": "painless"  
> > }  
> > },  
> > "actions": {}  
> > }

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [February 15, 2019, 9:03am UTC](https://discuss.elastic.co/t/how-to-compare-two-aggregation-output-in-watcher-in-condition/168388/2 "2019-02-15T09:03:15Z")

</div>

Hey,

can you explain in more detail what the problem is? Is it accessing the fields?

`ctx.payload.first.aggs.AggregatedData` and `ctx.payload.second.aggs.AggregatedData` contains the different buckets. The next step would probably be to walk through them and compare them, but I do not know what exactly your plan is with those, so it is hard to tell for me.

Please check out the [examples repo](https://github.com/elastic/examples/tree/master/Alerting) that contains a few examples to get more accustomized with scripting.

---

<div class="post-metadata">

### Author: ![RamyaGowda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramyagowda/32/27440_2.png) [@RamyaGowda](https://discuss.elastic.co/u/RamyaGowda)
#### Post date: [February 15, 2019, 9:32am UTC](https://discuss.elastic.co/t/how-to-compare-two-aggregation-output-in-watcher-in-condition/168388/3 "2019-02-15T09:32:03Z")

</div>

Heyy @spinscale

Yeah both the aggregations are contains array of bucket which is having beats names.

First aggregation result:

> {  
> "aggregations" : {  
> "AggregatedData " : {  
> "doc\_count\_error\_upper\_bound" : 0,  
> "sum\_other\_doc\_count" : 0,  
> "buckets" : [  
> {  
> "key" : "beat1",  
> "doc\_count" : 30  
> },  
> {  
> "key" : "beat2",  
> "doc\_count" : 30  
> },  
> {  
> "key" : "beat3",  
> "doc\_count" : 30  
> },  
> {  
> "key" : "beat4",  
> "doc\_count" : 30  
> }  
> ]  
> }  
> }  
> }

second aggregation result:

> {  
> "aggregations" : {  
> "AggregatedData " : {  
> "doc\_count\_error\_upper\_bound" : 0,  
> "sum\_other\_doc\_count" : 0,  
> "buckets" : [  
> {  
> "key" : "beat2",  
> "doc\_count" : 30  
> },  
> {  
> "key" : "beat3",  
> "doc\_count" : 30  
> }  
> ]  
> }  
> }  
> }

I need to compare these two arrays in watcher condition (something like that not pretty sure).  
and getting outcome as **beat1** and **beat2** are missing in second array.

Thanks

---

<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: [March 15, 2019, 9:32am UTC](https://discuss.elastic.co/t/how-to-compare-two-aggregation-output-in-watcher-in-condition/168388/4 "2019-03-15T09:32:09Z")

</div>

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