# Ctx.payload.hits.hits not working

**URL:** <https://discuss.elastic.co/t/ctx-payload-hits-hits-not-working/180524>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [May 10, 2019, 9:41am UTC](https://discuss.elastic.co/t/ctx-payload-hits-hits-not-working/180524 "2019-05-10T09:41:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yagami23](https://avatars.discourse-cdn.com/v4/letter/y/e19adc/32.png) [@yagami23](https://discuss.elastic.co/u/yagami23)\
**Post date:** [May 10, 2019, 9:41am UTC](https://discuss.elastic.co/t/ctx-payload-hits-hits-not-working/180524/1 "2019-05-10T09:41:23Z")

</div>

Hi, below is my watcher content, its not working when add in transform section into it, the whole looping for ctx.payload.hits.hits is not working, the email receive only showing " The alert condition for 'OSB - ni-ecomm-wsc-cust-sync error was triggered:" . After remove the transform section, it's working again, the email receive showing all the results, please advise.

```
{
  "trigger": {
    "schedule": {
      "interval": "1m"
    }
  } ,
      "input": {
        "search": {
          "request": {
            "search_type": "query_then_fetch",
        "indices": [
          "websys-weblogic-*"
        ],
        "types": [],
        "body": {
          "query": {
            "bool": {
              "must": [
                {
                  "match_phrase": {
                    "message": "ni-ecomm-wsc-cust-sync error"
                  }
                },
                {
                  "match": {
                    "fields.env": "prod"
                  }
                },
                {
                  "wildcard": {
                    "source.keyword": "/opt/apps/logs/osb-server/weblogic/osb-server*.log"
                  }
                }
              ],
              "filter": [
                {
                  "range": {
                    "@timestamp": {
                      "gte": "now-15m"
                    }
                  }
                }
              ]
            }
          },
          "sort": [
            {
              "@timestamp": {
                "order": "desc",
                "unmapped_type": "boolean"
              }
            }
          ]
        }
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gt": 0
      }
    }
  },
  "actions": {
    "send_email": {
      "email": {
        "profile": "standard",
        "to": [
          "xxx@xxx.com"
        ],
        "subject": "(ElasticSearch) OSB - ni-ecomm-wsc-cust-sync error",
        "body": {
          "html": "<html><h3>The alert condition for 'OSB - ni-ecomm-wsc-cust-sync error was triggered:</h3></html><br>{{#ctx.payload.hits.hits}}\tDate: {{_source.@timestamp}}<br>\tHostname:{{_source.host.name}}<br>\tMessage: {{_source.message}}<hr><br>{{/ctx.payload.hits.hits}}"
        }
      }
    }
  },
  **"transform": {**
    **"script": {**
      **"source": "return[ctx.metadata.t1 = Instant.ofEpochMilli(ctx.execution_time.getMillis()).minus(15,** **ChronoUnit.MINUTES)]",**
      **"lang": "painless"**
    **}**
  **},**
  "throttle_period_in_millis": 14400000
}
```

---

<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:** [May 10, 2019, 11:32am UTC](https://discuss.elastic.co/t/ctx-payload-hits-hits-not-working/180524/2 "2019-05-10T11:32:00Z")

</div>

Please take your time and properly format your messages. This makes it impossible to parse the JSON due to the missing indentation and seemingly random formatting. You can use markdown here, so please make use of that.

a script transform needs to return a payload, which your script does not do.

See [https://www.elastic.co/guide/en/elastic-stack-overview/7.0/transform-script.html](https://www.elastic.co/guide/en/elastic-stack-overview/7.0/transform-script.html)

---

<div class="post-metadata">

**Author:** ![yagami23](https://avatars.discourse-cdn.com/v4/letter/y/e19adc/32.png) [@yagami23](https://discuss.elastic.co/u/yagami23)\
**Post date:** [May 13, 2019, 2:09am UTC](https://discuss.elastic.co/t/ctx-payload-hits-hits-not-working/180524/3 "2019-05-13T02:09:24Z")

</div>

Hi , i have add in "return" in transform, but the {{#ctx.payload.hits.hits}} {{/ctx.payload.hits.hits}} still showing blank result. If i remove the whole transform section, the {{#ctx.payload.hits.hits}} {{/ctx.payload.hits.hits}} is working and showing all the output.

---

<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:** [June 10, 2019, 2:09am UTC](https://discuss.elastic.co/t/ctx-payload-hits-hits-not-working/180524/4 "2019-06-10T02:09:33Z")

</div>

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