Search input
Use the search
input to load the results of an Elasticsearch search request into the execution context when the watch is triggered. See Search Input Attributes for all of the supported attributes.
In the search input’s request
object, you specify:
- The indices you want to search
- The search type
- The search request body
The search request body supports the full Elasticsearch Query DSL—it’s the same as the body of an Elasticsearch _search
request.
For example, the following input retrieves all event
documents from the logs
index:
"input" : {
"search" : {
"request" : {
"indices" : [ "logs" ],
"body" : {
"query" : { "match_all" : {}}
}
}
}
}
You can use date math and wildcards when specifying indices. For example, the following input loads the latest VIXZ quote from today’s daily quotes index:
{
"input" : {
"search" : {
"request" : {
"indices" : [ "<stock-quotes-{now/d}>" ],
"body" : {
"size" : 1,
"sort" : {
"timestamp" : { "order" : "desc"}
},
"query" : {
"term" : { "symbol" : "vix"}
}
}
}
}
}
}
Extracting specific fields
You can specify which fields in the search response you want to load into the watch payload with the extract
attribute. This is useful when a search generates a large response and you are only interested in particular fields.
For example, the following input loads only the total number of hits into the watch payload:
"input": {
"search": {
"request": {
"indices": [ ".watcher-history*" ]
},
"extract": [ "hits.total.value" ]
}
},
Using Templates
The search
input supports search templates. For example, the following snippet references the indexed template called my_template
and passes a value of 23 to fill in the template’s value
parameter:
{
"input" : {
"search" : {
"request" : {
"indices" : [ "logs" ],
"template" : {
"id" : "my_template",
"params" : {
"value" : 23
}
}
}
}
}
...
}
Applying conditions
The search
input is often used in conjunction with the script
condition. For example, the following snippet adds a condition to check if the search returned more than five hits:
{
"input" : {
"search" : {
"request" : {
"indices" : [ "logs" ],
"body" : {
"query" : { "match_all" : {} }
}
}
}
},
"condition" : {
"compare" : { "ctx.payload.hits.total.value" : { "gt" : 5 }}
}
...
}
Accessing the search results
Conditions, transforms, and actions can access the search results through the watch execution context. For example:
- To load all of the search hits into an email body, use
ctx.payload.hits
. - To reference the total number of hits, use
ctx.payload.hits.total.value
. - To access a particular hit, use its zero-based array index. For example, to get the third hit, use
ctx.payload.hits.hits.2
. - To get a field value from a particular hit, use
ctx.payload.hits.hits.<index>.fields.<fieldname>
. For example, to get the message field from the first hit, usectx.payload.hits.hits.0.fields.message
.
The total number of hits in the search response is returned as an object in the response. It contains a value
, the number of hits, and a relation
that indicates if the value is accurate ("eq"
) or a lower bound of the total hits that match the query ("gte"
). You can set track_total_hits
to true in the search request to tell Elasticsearch to always track the number of hits accurately.
Search Input Attributes
Name | Required | Default | Description |
---|---|---|---|
| no |
| The type of search request to perform. Valid values are: |
| no | - | The indices to search. If omitted, all indices are searched, which is the default behaviour in Elasticsearch. |
| no | - | The body of the request. The request body follows the same structure you normally send in the body of a REST |
| no | - | The body of the search template. See configure templates for more information. |
| no |
| How to expand wildcards. Valid values are: |
| no |
| Whether the search should ignore unavailable indices. See |
| no |
| Whether to allow a search where a wildcard indices expression results in no concrete indices. See allow_no_indices for more information. |
| no | - | A array of JSON keys to extract from the search response and load as the payload. When a search generates a large response, you can use |
| no | 30s | The timeout for waiting for the search api call to return. If no response is returned within this time, the search input times out and fails. This setting overrides the default search operations timeouts. |
You can reference the following variables in the execution context when specifying the request body
:
Name | Description |
---|---|
| The id of the watch that is currently executing. |
| The time execution of this watch started. |
| The time this watch was triggered. |
| The time this watch was supposed to be triggered. |
| Any metadata associated with the watch. |