加载中

搜索有效载荷转换

一种载荷转换,它在集群上执行搜索,并用返回的搜索响应替换观察(watch)执行上下文中的当前载荷。以下代码片段展示了如何在 watch 级别定义一个简单的搜索转换

{
  "transform" : {
    "search" : {
      "request" : {
        "body" : { "query" : { "match_all" : {} }}
      }
    }
  }
}
		

与其他基于搜索的结构一样,可以使用 Elasticsearch 支持的完整搜索 API。例如,以下搜索载荷转换对所有事件索引执行搜索,匹配优先级为 error 的事件

{
  "transform" : {
    "search" : {
      "request" : {
        "indices" : [ "events-*" ],
        "body" : {
          "size" : 0,
          "query" : {
            "match" : { "priority" : "error"}
          }
        }
      }
    }
  }
}
		

下表列出了搜索载荷转换的所有可用设置

名称 必填 默认值 描述
request.search_type query_then_fetch 搜索类型
request.indices 所有索引 要搜索的一个或多个索引。
request.body match_all 查询 请求体。请求体遵循您通常在 REST _search 请求体中发送的结构。该主体可以是静态文本,也可以包含 mustache 模板
request.indices_options.expand_wildcards open 确定如何展开索引通配符。由 openclosedhidden 组合而成的数组。或者也可以是 noneall 值。(参见多目标语法
request.indices_options.ignore_unavailable true 一个布尔值,确定搜索是否应宽松地忽略不可用的具体索引、别名或数据流。(参考多目标语法
request.indices_options.allow_no_indices true 一个布尔值,确定搜索是否允许 (1) 解析为无索引的通配符索引表达式,以及 (2) 最终解析的索引集为空的请求。(参考多目标语法
request.template - 搜索模板的主体。更多信息,请参见配置模板
timeout 30s 等待搜索 API 调用返回的超时时间。如果在此时间内没有返回响应,搜索载荷转换将超时并失败。此设置会覆盖默认超时时间。

搜索载荷转换支持 mustache 模板。这可以作为主体定义的一部分,或者指向现有的模板(在文件中定义,或作为脚本存储在 Elasticsearch 中)。

例如,以下代码片段展示了一个引用 watch 预定时间的搜索

{
  "transform" : {
    "search" : {
      "request" : {
        "indices" : [ "logstash-*" ],
        "body" : {
          "size" : 0,
          "query" : {
            "bool" : {
              "must" : {
                "match" : { "priority" : "error"}
              },
              "filter" : [
                {
                  "range" : {
                    "@timestamp" : {
                      "gte" : "{{ctx.trigger.scheduled_time}}||-30s",
                      "lte" : "{{ctx.trigger.triggered_time}}"
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}
		

模板的模型是提供的 template.params 设置与标准 watch 执行上下文模型的并集。

以下是使用引用所提供参数的模板的示例

{
  "transform" : {
    "search" : {
      "request" : {
        "indices" : [ "logstash-*" ],
        "template" : {
          "source" : {
            "size" : 0,
            "query" : {
              "bool" : {
                "must" : {
                  "match" : { "priority" : "{{priority}}"}
                },
                "filter" : [
                  {
                    "range" : {
                      "@timestamp" : {
                        "gte" : "{{ctx.trigger.scheduled_time}}||-30s",
                        "lte" : "{{ctx.trigger.triggered_time}}"
                      }
                    }
                  }
                ]
              }
            },
            "params" : {
              "priority" : "error"
            }
          }
        }
      }
    }
  }
}
		
© . This website operates independently and is not affiliated with or endorsed by Elasticsearch B.V. All brand names, logos, and trademarks are the property of their respective owners.