# Cannot access the search results through the watch execution context - Ctx.payload.hits.total

**URL:** <https://discuss.elastic.co/t/cannot-access-the-search-results-through-the-watch-execution-context-ctx-payload-hits-total/125077>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [March 21, 2018, 9:05pm UTC](https://discuss.elastic.co/t/cannot-access-the-search-results-through-the-watch-execution-context-ctx-payload-hits-total/125077 "2018-03-21T21:05:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexus/32/12696_2.png) [@alexus](https://discuss.elastic.co/u/alexus)\
**Post date:** [March 21, 2018, 9:05pm UTC](https://discuss.elastic.co/t/cannot-access-the-search-results-through-the-watch-execution-context-ctx-payload-hits-total/125077/1 "2018-03-21T21:05:45Z")

</div>

I'm trying to access value from `{{ctx.payload.hits.total}}`([Search Input | X-Pack for the Elastic Stack](https://www.elastic.co/guide/en/x-pack/6.2/input-search.html#_accessing_the_search_results)) and send message to [Slack Action | X-Pack for the Elastic Stack](https://www.elastic.co/guide/en/x-pack/6.2/actions-slack.html#actions-slack)), yet not getting any values(

[Get Watch@Pastebin](https://pastebin.com/MPR2W8jD)

Please advise.

---

<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 21, 2018, 9:40pm UTC](https://discuss.elastic.co/t/cannot-access-the-search-results-through-the-watch-execution-context-ctx-payload-hits-total/125077/2 "2018-03-21T21:40:38Z")

</div>

can you paste the output of the [Execute Watch API](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/watcher-api-execute-watch.html)?

thanks!

---

<div class="post-metadata">

**Author:** ![alexus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexus/32/12696_2.png) [@alexus](https://discuss.elastic.co/u/alexus)\
**Post date:** [March 21, 2018, 10:01pm UTC](https://discuss.elastic.co/t/cannot-access-the-search-results-through-the-watch-execution-context-ctx-payload-hits-total/125077/3 "2018-03-21T22:01:22Z")

</div>

[Execute Watch@Pastebin](https://pastebin.com/w7Gf9K9F)

---

<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 21, 2018, 11:02pm UTC](https://discuss.elastic.co/t/cannot-access-the-search-results-through-the-watch-execution-context-ctx-payload-hits-total/125077/4 "2018-03-21T23:02:12Z")

</div>

the output shows a successful execution, not a failed one. Check the `result` output field. the condition is met and the slack action was executed successfully.

--Alex

---

<div class="post-metadata">

**Author:** ![alexus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexus/32/12696_2.png) [@alexus](https://discuss.elastic.co/u/alexus)\
**Post date:** [March 22, 2018, 1:34am UTC](https://discuss.elastic.co/t/cannot-access-the-search-results-through-the-watch-execution-context-ctx-payload-hits-total/125077/5 "2018-03-22T01:34:15Z")

</div>

from results:

```
  "actions": [
    {
      "id": "slack_1",
      "type": "slack",
      "status": "success",
      "slack": {
        "account": "monitoring",
        "sent_messages": [
          {
            "status": "success",
            "to": "test",
            "message": {
              "from": "9c644ed6-51bf-47ed-8dd6-7ae33e08f9ed",
              "text": "Watch [Logstash] has exceeded the threshold\n "
            }
          }
        ]
      }
    }
  ]

```

from my watch:

```
"actions": {
  "slack_1": {
    "slack": {
      "message": {
        "to": [
          "test"
        ],
        "text": "Watch [{{ctx.metadata.name}}] has exceeded the threshold\n{{ctx.payload.hits.total}} {{ctx.metadata.*}}"
      }
    }
  }
},

```

and even though:

```
      "hits": {
        "hits": [],
        "total": 12683,
        "max_score": 0
      },

```

`{{ctx.payload.hits.total}}` returns empty.

Please advise.

---

<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 22, 2018, 9:35am UTC](https://discuss.elastic.co/t/cannot-access-the-search-results-through-the-watch-execution-context-ctx-payload-hits-total/125077/6 "2018-03-22T09:35:45Z")

</div>

You are running a `transform` script that changes the payload to `result: 12683`, so there is no `payload.hits.total` anymore in your action.

---

<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 19, 2018, 9:36am UTC](https://discuss.elastic.co/t/cannot-access-the-search-results-through-the-watch-execution-context-ctx-payload-hits-total/125077/7 "2018-04-19T09:36:26Z")

</div>

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