Search Input
当监控被触发的时候,使用搜索输入把Elasticsearch搜索请求结果加入到执行上下文。有关支持的所有属性,请参阅搜索输入属性。
在搜索输入的request对象中,你可以指定:
- 您希望搜索的索引
- 搜索类型
- 搜索请求体
搜索请求体支持全Elasticsearch查询DSL--- 它与Elasticsearch _search请求体相同。
例如,以下输入从logs索引中检索所有event文档:
"input" : {
"search" : {
"request" : {
"indices" : [ "logs" ],
"types" : [ "event" ],
"body" : {
"query" : { "match_all" : {}}
}
}
}
}
当指定索引时,你可以使用日期函数和通配符。例如,以下输入将从当天的每日报价索引中加载最新的VIXZ报价:
{
"input" : {
"search" : {
"request" : {
"indices" : [ "<stock-quotes-{now/d}>" ],
"body" : {
"size" : 1,
"sort" : {
"timestamp" : { "order" : "desc"}
},
"query" : {
"term" : { "symbol" : "vix"}
}
}
}
}
}
}
Extracting Specific Fields(提取特定字段)
您可以通过使用extract属性来指定哪些搜索响应字段要加载到监视的有效载荷中。当搜索产生很大的响应并且你仅仅只对特定的字段感兴趣时,这很有用。
例如,以下输入仅仅把命中总数加载到监控有效载荷中:
"input": {
"search": {
"request": {
"indices": [ ".watcher-history*" ]
},
"extract": [ "hits.total" ]
}
},
Using Templates(使用模板)
搜索输入支持搜索模板。例如,以下代码片段引用叫着“my_template”的索引模板,并传递23值填充模板的value参数:
{
"input" : {
"search" : {
"request" : {
"indices" : [ "logs" ],
"template" : {
"id" : "my_template",
"params" : {
"value" : 23
}
}
}
}
}
...
}
Applying Conditions(应用条件)
搜索输入经常和脚本条件联合使用。例如,以下代码片段添加了一个条件以判断搜索是否返回5次以上:
{
"input" : {
"search" : {
"request" : {
"indices" : [ "logs" ],
"body" : {
"query" : { "match_all" : {} }
}
}
}
},
"condition" : {
"compare" : { "ctx.payload.hits.total" : { "gt" : 5 }}
}
...
}
Accessing the Search Results(访问搜索结果)
通过监控执行上下文,条件,转换和操作能访问搜索结果,例如:
- 加载所有搜索结果到电子邮件的内容中,可以使用ctx.payload.hits。
- 引用命中总数,可以使用ctx.payload.hits.total。
- 要访问特定命中,请使用其基于零的数组索引,例如,要获得第三个命中,请使用ctx.payload.hits.hits.2。
- 要从特定命中中取得一个字段的值,使用ctx.payload.hits.hits.<index>.fields.<filedname>。例如,从第一个命中中获取message字段。使用ctx.payload.hits.hits.0.fields.message
Search Input Attributes(搜索输入属性)
Name | Required | Default | Description |
---|---|---|---|
| no |
| 要执行搜索请求的类型,有效值有:dfs_query_and_fetch,dfs_query_then_fetch,query_and_fetch和query_then_fetch,Elasticsearch默认是query_then_fetch |
| no | - | 搜索的索引。假如省略,所有的索引都将搜索,这也是Elasticsearch中默认的行为。 |
| no | - | 搜索文档类型。假如省略,所有文档类型将被搜索,这也是Elasticsearch中的默认行为。 |
| no | - | 请求体。请求体遵循通常在REST _search请求体中发送的相同结构。该请求体可以是静态文本或包含mustach模板。 |
| no | - | 搜索主体的模板。参阅模板配置获取更多信息。 |
| no |
| 如何扩展通配符。有效值有:all,open,closed和none,参阅expand_wildcards获取更多信息。 |
| no |
| 搜索是否应该忽略不可用的索引。参阅ignore_unavailable获取更多信息。 |
| no |
| 是否允许通配符索引表达式没有具体索引的搜索.参阅allow_no_indices获取更多信息。 |
| no | - | 从搜索响应中提取的JSON秘钥数组,并作为有效载荷加载。当搜索产生大量响应,你可以使用extract选择相应的字段,而不是加载完整的响应。 |
| no | 30s | 等待搜索api调用的超时返回。如果再次时间内没有响应返回,搜索输入将超时并失败。此设置将覆盖默认搜索操作超时。 |
当指定请求主题时,你可以在执行上下文中应用以下变量:
Name | Description |
---|---|
| 监控正在执行的id |
| 该监控开始执行的时间 |
| 该监控被触发的时间 |
| 该监控支持触发的时间 |
| 与该监控关联的任何元数据 |
原文地址:https://www.elastic.co/guide/en/x-pack/5.0/input-search.html