OpenSearch/docs/en/watcher/input/chain.asciidoc
Alexander Reelsen 6406c9816a Watcher: Add transform input for chained input (elastic/x-pack-elasticsearch#2861)
The chained input in watcher is a useful feature to
call several endpoints before execution a condition.
However it was pretty hard to modify data from a previous
input in order to be able to execute it in another input.

This commit adds a another input, called a `transform` input,
which allows you to do a transform as another input in a chained
input.

See this example

```
"input" : {
  "chain" : {
    "inputs" : [ <1>
      {
        "first" : {
          "simple" : { "path" : "/_search" }
        }
      },
      {
        "second" : {
          "transform" : {
            "script" : "return [ 'path' : 'ctx.payload.first.path' + '/' ]"
          }
        }
      },
      {
        "third" : {
          "http" : {
            "request" : {
              "host" : "localhost",
              "port" : 9200,
              "path" : "{{ctx.payload.second.path}}" <2>
            }
          }
        }
      }
    ]
  }
}
```

This allows for far more flexibility before executing the next input in a chained
one.

Original commit: elastic/x-pack-elasticsearch@3af9ba6e9b
2017-11-27 13:27:56 +01:00

93 lines
2.5 KiB
Plaintext

[[input-chain]]
=== Chain Input
Use the `chain` input to load data from multiple sources into the watch
execution context when the watch is triggered. The inputs in a chain
are processed in order and the data loaded by an input can be accessed by the
subsequent inputs in the chain.
The `chain` input enables you perform actions based on data from multiple
sources. You can also use the data collected by one input to load data
from another source.
For example, the following chain input loads data from an HTTP server using the
path set by a `simple` input:
[source,js]
--------------------------------------------------
"input" : {
"chain" : {
"inputs" : [ <1>
{
"first" : {
"simple" : { "path" : "/_search" }
}
},
{
"second" : {
"http" : {
"request" : {
"host" : "localhost",
"port" : 9200,
"path" : "{{ctx.payload.first.path}}" <2>
}
}
}
}
]
}
}
--------------------------------------------------
<1> The inputs in a chain are specified as an array to guarantee the order in
which the inputs are processed. (JSON does not guarantee the order of
arbitrary objects.)
<2> Loads the `path` set by the `first` input.
==== Accessing Chained Input Data
To reference data loaded by a particular input, you use the input's name,
`ctx.payload.<input-name>.<value>`.
==== Transforming Chained Input Data
In certain use-cases the output of the first input should be used as input
in a subsequent input. This requires you to do a transform, before you pass
the data on to the next input.
In order to achieve this you can use a transform input between the two
specified inputs, see the following example. Note, that the first input will
still be available in its original form in `ctx.payload.first`.
[source,js]
--------------------------------------------------
"input" : {
"chain" : {
"inputs" : [ <1>
{
"first" : {
"simple" : { "path" : "/_search" }
}
},
{
"second" : {
"transform" : {
"script" : "return [ 'path' : 'ctx.payload.first.path' + '/' ]"
}
}
},
{
"third" : {
"http" : {
"request" : {
"host" : "localhost",
"port" : 9200,
"path" : "{{ctx.payload.second.path}}" <2>
}
}
}
}
]
}
}
--------------------------------------------------