# Conditional action only if list exists

**URL:** <https://discuss.elastic.co/t/conditional-action-only-if-list-exists/121213>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [February 23, 2018, 1:06pm UTC](https://discuss.elastic.co/t/conditional-action-only-if-list-exists/121213 "2018-02-23T13:06:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Stevens](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@Steve\_Stevens](https://discuss.elastic.co/u/Steve_Stevens)\
**Post date:** [February 23, 2018, 1:06pm UTC](https://discuss.elastic.co/t/conditional-action-only-if-list-exists/121213/1 "2018-02-23T13:06:34Z")

</div>

I have a watcher that performs a transform on matched records and then posts the result into a different index. I am making use of "ctx.payload.\_doc" to insert into the index. Depending on a complex action in the transform there is the potential to have an empty \_doc list come back out. I'm not sure how to write the action condition to stop it trying to index nothing as it seems to "fail" on receiving the empty set.

Is it possibly to create a "total" field via a transform to perform the conditional against or would that interfere with the index actions mechanism to autoindex the list?

```
"condition":{
    "compare":{
      "ctx.payload.hits.total":{
        "gt":0
      }
    }
  },
  "transform":{
    "script":{
      "inline":"return ['_doc': []]",
      "lang": "painless"
    }
  },
  "actions":{
    "condition": {
          ??????
      }
    },
    "index_payload":{
      "index":{
        "index":"hazelcast",
        "doc_type":"hazelcast"
      }
    }
  }
```

---

<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 27, 2018, 12:46pm UTC](https://discuss.elastic.co/t/conditional-action-only-if-list-exists/121213/2 "2018-02-27T12:46:07Z")

</div>

have you tried something like `ctx.payload._doc.size() > 0`

---

<div class="post-metadata">

**Author:** ![Steve\_Stevens](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@Steve\_Stevens](https://discuss.elastic.co/u/Steve_Stevens)\
**Post date:** [February 27, 2018, 1:41pm UTC](https://discuss.elastic.co/t/conditional-action-only-if-list-exists/121213/3 "2018-02-27T13:41:10Z")

</div>

as painless?! I don't know why I didn't think of doing that...  
placed condition within index\_payload and it worked beautifully. Thanks!

"actions": {  
"index\_payload": {  
"condition":{  
"script":{  
"source":"return ctx.payload.\_doc.size() \> 0;",  
"lang":"painless"  
}  
},  
"index": {  
"index": "hazelcast",  
"doc\_type": "hazelcast"  
}  
}  
}

---

<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 27, 2018, 1:41pm UTC](https://discuss.elastic.co/t/conditional-action-only-if-list-exists/121213/4 "2018-03-27T13:41:54Z")

</div>

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