# How do I use a Condition to Match Against a Value in a Loop from ctx.payload

**URL:** <https://discuss.elastic.co/t/how-do-i-use-a-condition-to-match-against-a-value-in-a-loop-from-ctx-payload/122910>\
**Category:** Elasticsearch\
**Created:** [March 7, 2018, 3:32pm UTC](https://discuss.elastic.co/t/how-do-i-use-a-condition-to-match-against-a-value-in-a-loop-from-ctx-payload/122910 "2018-03-07T15:32:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmdm](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@dmdm](https://discuss.elastic.co/u/dmdm)\
**Post date:** [March 7, 2018, 3:32pm UTC](https://discuss.elastic.co/t/how-do-i-use-a-condition-to-match-against-a-value-in-a-loop-from-ctx-payload/122910/1 "2018-03-07T15:32:02Z")

</div>

On the bottom of this example, in the actions section, I sort through all the snapshots in an S3 bucket, end goal is to send an alert to victorops if I have a failed snapshot. I finally figured out how to write out every state of each snapshot, but can't figure out how to do a compare to show only the ones that have failed. Here is an example of what I'm trying to do that currently does not work.

```auto
{
	"trigger": {
		"schedule": {
			"interval": "60s"
		}
	},
	"input": {
		"http": {
			"request": {
				"scheme": "http",
				"host": "analytics.devc.openwhere.net",
				"port": 9200,
				"method": "get",
				"path": "/_snapshot/s3_repository/_all",
				"params": {},
				"headers": {}
			}
		}
	},
	"condition": {
		"compare": {
			"{{#ctx.payload.snapshots}}{{state}}{{/ctx.payload.snapshots}}": {
				"eq": "FAILED"
			}
		},
	"actions": {
			"logging": {
				"logging": {
					"level": "info",
					"text": " Snapshot Sate: {{#ctx.payload.snapshots}}{{state}}{{/ctx.payload.snapshots}}"
				}
			}
		}
	}
}

```

The other questions is even when I get the compare to work would it print every snapshot state or just the ones that matched the condition?

---

<div class="post-metadata">

**Author:** ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)\
**Post date:** [March 15, 2018, 8:22pm UTC](https://discuss.elastic.co/t/how-do-i-use-a-condition-to-match-against-a-value-in-a-loop-from-ctx-payload/122910/2 "2018-03-15T20:22:59Z")

</div>

CC @spinscale

---

<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:** [March 15, 2018, 8:39pm UTC](https://discuss.elastic.co/t/how-do-i-use-a-condition-to-match-against-a-value-in-a-loop-from-ctx-payload/122910/3 "2018-03-15T20:39:04Z")

</div>

The conditions needs to check if any snapshots are marked as failed.

Then you need an [script](https://www.elastic.co/guide/en/x-pack/6.2/transform-script.html) transform in your action, that filters for those that are failed, something like this (written on top of my head)

```auto
return ['snapshots' : ctx.payload.snapshots.stream().filter(s -> s.state == "FAILED").collect(Collectors.toList())]

```

hope that helps.

--Alex

---

<div class="post-metadata">

**Author:** ![dmdm](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@dmdm](https://discuss.elastic.co/u/dmdm)\
**Post date:** [March 20, 2018, 6:39pm UTC](https://discuss.elastic.co/t/how-do-i-use-a-condition-to-match-against-a-value-in-a-loop-from-ctx-payload/122910/4 "2018-03-20T18:39:57Z")

</div>

Yes and thank you. I used it the following way in an action:

```auto
"actions": {
    "logging": {
      "transform": {
        "script": {
          "source": "return ['failedones' : ctx.payload.snapshots.stream().filter(s -> s.state == 'FAILED').collect(Collectors.toList())]",
          "lang": "painless"
        }
      },
      "logging": {
        "level": "info",
        "text": "The following snapshot failed: {{#ctx.payload}}{{failedones}}{{/ctx.payload}}"
      }
    }
  }

```

---

<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:** [April 17, 2018, 6:40pm UTC](https://discuss.elastic.co/t/how-do-i-use-a-condition-to-match-against-a-value-in-a-loop-from-ctx-payload/122910/5 "2018-04-17T18:40:05Z")

</div>

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