# Kibana - Watcher Script

**URL:** <https://discuss.elastic.co/t/kibana-watcher-script/232389>\
**Category:** Elasticsearch\
**Created:** [May 13, 2020, 8:20am UTC](https://discuss.elastic.co/t/kibana-watcher-script/232389 "2020-05-13T08:20:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vidhya](https://avatars.discourse-cdn.com/v4/letter/v/7feea3/32.png) [@Vidhya](https://discuss.elastic.co/u/Vidhya)\
**Post date:** [May 13, 2020, 8:20am UTC](https://discuss.elastic.co/t/kibana-watcher-script/232389/1 "2020-05-13T08:20:45Z")

</div>

Trying to execute the below query from Watcher in Kibana to get the result of result.input.payload.hits.total from Watcher history (last executed) and getting Null Value in result.

ctx.payload.hits.hits.\_source.result.input.payload.hits.total - -getting NULL value

I'm trying to get the total value as "total": 11689"

```auto
{
  "trigger": {
    "schedule": {
      "interval": "30m"
    }
  },
  "input": {
    "search": {
      "request": {
        "body": {
           "query" : {
    "match" : { "watch_id": "test_watcher" }
   
  },
 
  "_source": "result.input.payload.hits.total",

   "sort" : [
    { "result.execution_time" : "desc" }
    ],
    "size": 1
        },
        "indices": [
          ".watcher-history-9-2020.05.13"
        ]
      }
    }
    
    
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.hits._source.result.input.payload.hits.total": {
        "gte": 10
      }
    }
  },
  "actions": {
    "my-logging-action": {
      "logging": {
        "text": "There are {{ctx.payload.hits.total}} documents in your index. Threshold is 10."
      }
    }
  }
}

```

Output

```auto
{
  "watch_id": "_inlined_",
  "node": "khjqRl2ZQcm74yMp8nDFGg",
  "state": "execution_not_needed",
  "user": "testuser",
  "status": {
    "state": {
      "active": true,
      "timestamp": "2020-05-13T08:21:26.542Z"
    },
    "last_checked": "2020-05-13T08:21:26.542Z",
    "actions": {
      "my-logging-action": {
        "ack": {
          "timestamp": "2020-05-13T08:21:26.542Z",
          "state": "awaits_successful_execution"
        }
      }
    },
    "execution_state": "execution_not_needed",
    "version": -1
  },
  "trigger_event": {
    "type": "manual",
    "triggered_time": "2020-05-13T08:21:26.542Z",
    "manual": {
      "schedule": {
        "scheduled_time": "2020-05-13T08:21:26.542Z"
      }
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          ".watcher-history-9-2020.05.13"
        ],
        "types": [],
        "body": {
          "query": {
            "match": {
              "watch_id": "test_watcher"
            }
          },
          "_source": "result.input.payload.hits.total",
          "sort": [
            {
              "result.execution_time": "desc"
            }
          ],
          "size": 1
        }
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.hits._source.result.input.payload.hits.total": {
        "gte": 10
      }
    }
  },
  "metadata": {
    "xpack": {
      "type": "json"
    }
  },
  "result": {
    "execution_time": "2020-05-13T08:21:26.542Z",
    "execution_duration": 1,
    "input": {
      "type": "search",
      "status": "success",
      "payload": {
        "_shards": {
          "total": 1,
          "failed": 0,
          "successful": 1,
          "skipped": 0
        },
        "hits": {
          "hits": [
            {
              "_index": ".watcher-history-9-2020.05.13",
              "_type": "doc",
              "_source": {
                "result": {
                  "input": {
                    "payload": {
                      "hits": {
                        "total": 11689
                      }
                    }
                  }
                }
              },
              "_id": "test_watcher_5e27d70e-a8bf-474c-ae1a-3e2ab34a8803-2020-05-13T08:21:00.335Z",
              "sort": [
                1589358060335
              ],
              "_score": null
            }
          ],
          "total": 227,
          "max_score": null
        },
        "took": 0,
        "timed_out": false
      },
      "search": {
        "request": {
          "search_type": "query_then_fetch",
          "indices": [
            ".watcher-history-9-2020.05.13"
          ],
          "types": [],
          "body": {
            "query": {
              "match": {
                "watch_id": "test_watcher"
              }
            },
            "_source": "result.input.payload.hits.total",
            "sort": [
              {
                "result.execution_time": "desc"
              }
            ],
            "size": 1
          }
        }
      }
    },
    "condition": {
      "type": "compare",
      "status": "success",
      "met": false,
      "compare": {
        "resolved_values": {
          "ctx.payload.hits.hits._source.result.input.payload.hits.total": null
        }
      }
    },
    "actions": []
  },
  "messages": []
}

```

---

<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, 2020, 8:20am UTC](https://discuss.elastic.co/t/kibana-watcher-script/232389/2 "2020-06-10T08:20:46Z")

</div>

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