如果Elasticsearch管道的条件与确切的字符串不匹配,则进行字符串比较

时间:2019-06-27 13:26:05

标签: elasticsearch

我正在尝试在Elasticsearch中创建管道。 在确定要使用的管道之前,我必须进行检查,因此我已经建立了一个进行路由的管道,如下所示:

{
  "description": "A pipeline of pipelines for log files",
  "version": 1,
  "processors": [
    {
      "pipeline": {
        "if": """ctx.applicative_code =~ /AFA[@]959[@]333-SERVICE[@]1.0.0.SIG/ """,
        "name": "ged_pipeline"
      }
    },
    {
      "fail": {
        "message": "This pipeline requires applicative_code to be one of: 'AFA@959@333-SERVICE@1.0.0.SIG'"
      }
    }
  ]
}

要测试它,我使用_simulate API:

POST _ingest/pipeline/logs_pipeline/_simulate
{
  "docs": [
    {
      "_source": {
        "message_log": "127.0.0.1:50613||Download||agent||S0000||PM000||Q||{5C7A4600-C422-4D81-BD02-39072E06F646}",
        "applicative_code": "AFA@959@333-SERVICE@1.0.0.SIG"
      }
    }
  ]
}

响应是这样的:

 "error" : {
        "root_cause" : [
          {
            "type" : "exception",
            "reason" : "java.lang.IllegalArgumentException: org.elasticsearch.ingest.common.FailProcessorException: This pipeline requires applicative_code to be one of: 'AFA@959@333-SERVICE@1.0.0.SIG'",
            "header" : {
              "processor_type" : "fail"
            }
          }
        ],
        "type" : "exception",
        "reason" : "java.lang.IllegalArgumentException: org.elasticsearch.ingest.common.FailProcessorException: This pipeline requires applicative_code to be one of: 'AFA@959@333-SERVICE@1.0.0.SIG'",
        "caused_by" : {
          "type" : "illegal_argument_exception",
          "reason" : "org.elasticsearch.ingest.common.FailProcessorException: This pipeline requires applicative_code to be one of: 'AFA@959@333-SERVICE@1.0.0.SIG'",
          "caused_by" : {
            "type" : "fail_processor_exception",
            "reason" : "This pipeline requires applicative_code to be one of: 'AFA@959@333-SERVICE@1.0.0.SIG'"
          }

所以,看来我的情况不符合要求。 我在这里使用正则表达式的原因是特殊字符“ @”弄乱了字符串比较。到目前为止,我在管道的if条件下尝试过的事情:

"if": """ctx.applicative_code == "AFA@959@333-SERVICE@1.0.0.SIG" """

结果:不起作用,@被解释,我无法使用\

对其进行转义
"if": """ctx.applicative_code.compareTo("AFA@959@333-SERVICE@1.0.0.SIG") """

结果:一样,@被解释

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

嗯,您快到了,但是您误解了管道的工作方式。第一个pipeline处理器已匹配并完成了工作,这一事实并不能阻止第二个fail运行。在上面的管道中,两个步骤始终都在运行,因此为什么会看到错误。

您需要做的是仅在条件不匹配(某种fail条件)的情况下运行else步骤,

{
  "description": "A pipeline of pipelines for log files",
  "version": 1,
  "processors": [
    {
      "pipeline": {
        "if": """ctx.applicative_code == 'AFA@959@333-SERVICE@1.0.0.SIG' """,
        "name": "ged_pipeline"
      }
    },
    {
      "fail": {
        "if": """ctx.applicative_code != 'AFA@959@333-SERVICE@1.0.0.SIG' """,
        "message": "This pipeline requires applicative_code to be one of: 'AFA@959@333-SERVICE@1.0.0.SIG'"
      }
    }
  ]
}