在摄取管道中使用搜索模板

Using a search template in an ingest pipeline

ElasticSearch 摄取管道能否使用搜索模板作为其脚本?

具体来说,我想配置一个摄取管道,这样每当有特定类型的数据进来时,我们就可以在 ElasticSearch 中查询一些相关数据,并根据结果,在加载之前。

我看到摄取管道可以使用脚本 (https://www.elastic.co/guide/en/elasticsearch/reference/master/script-processor.html), and that scripts can include search templates (https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-using.html),但我没能找到任何关于可能将两者结合的信息。

所以 ingest script 管道已经是您正在寻找的组合, 你应该使用 if 选项,阅读它 here.

您的管道应如下所示:

{
    "script": {
        "if": "ctx.type == 'thisType'",
        "source": """
                  //calculation here
                  ctx.newField = value;
                  """
    }
}

我建议您在创建管道后使用 simulate 对其进行测试,因为这会让您的生活更轻松。

编辑:

感谢@val 消除了我的一些困惑。 所以你不能真正做你想做的事,但我建议你阅读 enrich 管道。通过一些设置,您也许可以实现它。

实际上,您要求的功能将通过新的 enrich processor 出现在 7.5 中,它提供了索引时 JOIN 功能。

主要思想是建立一个enrich policy that will source data from your related indexes into a new "enrich index" and then you can leverage that "enrich index" in your ingest pipeline using an enrich processor,以丰富您的相关领域的文档。

因此,无需过多介绍细节,以下是它在实践中的工作原理:

  1. 您有一个索引 A,其中包含您想用于丰富的字段(abcd)您收到的文件
  2. 您根据该索引 A 和 "join" 字段定义丰富策略 a
  3. 您使用丰富处理器定义摄取管道,该处理器将尝试将传入文档的字段 z 与丰富索引
  4. 的字段 A.a 进行匹配
  5. 如果找到匹配项,您的传入文档将从索引 A 中获取字段 bcd。请注意,它还将获得匹配字段 a,如果需要,您可以使用 remove 处理器将其删除。

这正是您所期望的。您可以在不久的将来找到完整的示例here. At the beginning, it will work for exact matches (i.e. term query) and geo matches (i.e. geo_shape query), but they will probably add new kind of matches (like range matches)。