compare.asciidoc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. [[condition-compare]]
  2. === Compare Condition
  3. Use the `compare` condition to perform a simple comparison against a value in
  4. the watch payload. You can use the `compare` condition without enabling
  5. dynamic scripting.
  6. [[condition-compare-operators]]
  7. . Supported Comparison Operators
  8. [options="header"]
  9. |======
  10. | Name | Description
  11. | `eq` | Returns `true` when the resolved value equals the given one (applies
  12. to numeric, string, list, object and values)
  13. | `not_eq` | Returns `true` when the resolved value does not equal the given one
  14. (applies to numeric, string, list, object and null values)
  15. | `gt` | Returns `true` when the resolved value is greater than the given
  16. one (applies to numeric and string values)
  17. | `gte` | Returns `true` when the resolved value is greater/equal than/to the
  18. given one (applies to numeric and string values)
  19. | `lt` | Returns `true` when the resolved value is less than the given one
  20. (applies to numeric and string values)
  21. | `lte` | Returns `true` when the resolved value is less/equal than/to the
  22. given one (applies to numeric and string values)
  23. |======
  24. ==== Using a Compare Condition
  25. To use the `compare` condition, you specify the value in the execution context
  26. that you want to evaluate, a <<condition-compare-operators,comparison operator>>,
  27. and the value you want to compare against. For example, the following `compare`
  28. condition returns `true` if the number of the total hits in the <<input-search,
  29. search result>> is greater than or equal to 5:
  30. [source,js]
  31. --------------------------------------------------
  32. {
  33. "condition" : {
  34. "compare" : {
  35. "ctx.payload.hits.total.value" : { <1>
  36. "gte" : 5 <2>
  37. }
  38. }
  39. }
  40. }
  41. --------------------------------------------------
  42. // NOTCONSOLE
  43. <1> Use dot notation to reference a value in the execution context.
  44. <2> Specify a comparison operator and the value you want to compare against.
  45. [[compare-condition-date-math]]
  46. When comparing dates and times, you can use date math expressions
  47. of the form `<{expression}>`. For example, the following expression returns
  48. `true` if the watch was executed within the last five minutes:
  49. [source,js]
  50. --------------------------------------------------
  51. {
  52. "condition" : {
  53. "compare" : {
  54. "ctx.execution_time" : {
  55. "gte" : "<{now-5m}>"
  56. }
  57. }
  58. }
  59. }
  60. --------------------------------------------------
  61. // NOTCONSOLE
  62. You can also compare two values in the execution context by specifying the
  63. compared value as a path of the form of `{{path}}`. For example, the following
  64. condition compares the `ctx.payload.aggregations.status.buckets.error.doc_count`
  65. to the `ctx.payload.aggregations.handled.buckets.true.doc_count`:
  66. [source,js]
  67. --------------------------------------------------
  68. {
  69. "condition" : {
  70. "compare" : {
  71. "ctx.payload.aggregations.status.buckets.error.doc_count" : {
  72. "not_eq" : "{{ctx.payload.aggregations.handled.buckets.true.doc_count}}"
  73. }
  74. }
  75. }
  76. }
  77. --------------------------------------------------
  78. // NOTCONSOLE
  79. ==== Accessing Values in the Execution Context
  80. You use "dot-notation" to access values in the execution context. Values loaded
  81. into the execution context by the input are prefixed by `ctx.payload`.
  82. You can reference entries in arrays using their zero-based array indices.
  83. For example, to access the third element of the `ctx.payload.hits.hits`
  84. array, use `ctx.payload.hits.hits.2`.
  85. [options="header"]
  86. |======
  87. | Name | Description
  88. | `ctx.watch_id` | The id of the watch that is currently executing.
  89. | `ctx.execution_time` | The time execution of this watch started.
  90. | `ctx.trigger.triggered_time` | The time this watch was triggered.
  91. | `ctx.trigger.scheduled_time` | The time this watch was supposed to be triggered.
  92. | `ctx.metadata.*` | Any metadata associated with the watch.
  93. | `ctx.payload.*` | The payload data loaded by the watch's input.
  94. |======