script.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. [role="xpack"]
  2. [[condition-script]]
  3. === {watcher} script condition
  4. ++++
  5. <titleabbrev>Script condition</titleabbrev>
  6. ++++
  7. A watch <<condition,condition>> that evaluates a script. The default scripting
  8. language is `painless`. You can use any of the scripting languages supported by
  9. Elasticsearch as long as the language supports evaluating expressions to Boolean
  10. values. Note that the `mustache` and `expression` languages are too limited to be
  11. used by this condition. For more information, see <<modules-scripting>>.
  12. ==== Using a script condition
  13. The following snippet configures an inline `script` condition that always returns
  14. `true`:
  15. [source,js]
  16. --------------------------------------------------
  17. "condition" : {
  18. "script" : "return true"
  19. }
  20. --------------------------------------------------
  21. // NOTCONSOLE
  22. This example defines a script as a simple string. This format is actually a
  23. shortcut for defining an <<condition-script-inline,inline>> script. The
  24. formal definition of a script is an object that specifies the script type and
  25. optional language and parameter values. If the `lang` attribute is omitted, the
  26. language defaults to `painless`. Elasticsearch supports two types of scripts,
  27. <<condition-script-inline,inline>> and <<condition-script-stored,stored>>.
  28. For example, the following snippet shows a formal definition of an `inline`
  29. script that explicitly specifies the language and defines a single script
  30. parameter, `result`:
  31. [source,js]
  32. --------------------------------------------------
  33. "condition" : {
  34. "script" : {
  35. "source" : "return params.result",
  36. "lang" : "painless",
  37. "params" : {
  38. "result" : true
  39. }
  40. }
  41. }
  42. --------------------------------------------------
  43. // NOTCONSOLE
  44. [[condition-script-inline]]
  45. ==== Inline scripts
  46. Inline scripts are scripts that are defined in the condition itself. The
  47. following snippet shows the formal configuration of a simple painless script that
  48. always returns `true`.
  49. [source,js]
  50. --------------------------------------------------
  51. "condition" : {
  52. "script" : {
  53. "source" : "return true"
  54. }
  55. }
  56. --------------------------------------------------
  57. // NOTCONSOLE
  58. [[condition-script-stored]]
  59. ==== Stored scripts
  60. Stored scripts refer to scripts that were
  61. <<modules-scripting-using,stored>> in Elasticsearch. The following
  62. snippet shows how to refer to a script by its `id`:
  63. [source,js]
  64. --------------------------------------------------
  65. "condition" : {
  66. "script" : {
  67. "id" : "my_script"
  68. }
  69. }
  70. --------------------------------------------------
  71. // NOTCONSOLE
  72. As with <<condition-script-inline,inline>> scripts, you can also specify the
  73. script language and parameters:
  74. [source,js]
  75. --------------------------------------------------
  76. "condition" : {
  77. "script" : {
  78. "id" : "my_script",
  79. "lang" : "javascript",
  80. "params" : { "color" : "red" }
  81. }
  82. }
  83. --------------------------------------------------
  84. // NOTCONSOLE
  85. [[accessing-watch-payload]]
  86. ==== Accessing the watch payload
  87. A script can access the current watch execution context, including the payload
  88. data, as well as any parameters passed in through the condition definition.
  89. For example, the following snippet defines a watch that uses a
  90. <<input-search,`search` input>> and uses a `script` condition to check if the
  91. number of hits is above a specified threshold:
  92. [source,js]
  93. --------------------------------------------------
  94. {
  95. "input" : {
  96. "search" : {
  97. "indices" : "log-events",
  98. "body" : {
  99. "size" : 0,
  100. "query" : { "match" : { "status" : "error" } }
  101. }
  102. }
  103. },
  104. "condition" : {
  105. "script" : {
  106. "source" : "return ctx.payload.hits.total > params.threshold",
  107. "params" : {
  108. "threshold" : 5
  109. }
  110. }
  111. }
  112. }
  113. --------------------------------------------------
  114. // NOTCONSOLE
  115. When you're using a scripted condition to evaluate an Elasticsearch response,
  116. keep in mind that the fields in the response are no longer in their native data
  117. types. For example, the `@timestamp` in the response is a string, rather than a
  118. `DateTime`. To compare the response `@timestamp` against the `ctx.execution_time`,
  119. you need to parse the `@timestamp` string into a `ZonedDateTime`. For example:
  120. [source,js]
  121. --------------------------------------------------
  122. java.time.ZonedDateTime.parse(@timestamp)
  123. --------------------------------------------------
  124. // NOTCONSOLE
  125. You can reference the following variables in the watch context:
  126. [options="header"]
  127. |======
  128. | Name | Description
  129. | `ctx.watch_id` | The id of the watch that is currently executing.
  130. | `ctx.execution_time` | The time execution of this watch started.
  131. | `ctx.trigger.triggered_time` | The time this watch was triggered.
  132. | `ctx.trigger.scheduled_time` | The time this watch was supposed to be triggered.
  133. | `ctx.metadata.*` | Any metadata associated with the watch.
  134. | `ctx.payload.*` | The payload data loaded by the watch's input.
  135. |======