无法为观察者提供弹性搜索条件?

时间:2015-06-26 13:35:25

标签: groovy elasticsearch scripting mailing elasticsearch-watcher

我安装了观察者并给出了条件。在给出条件的同时给我错误......

{"error":"WatcherException[failed to put watch [log_error_watch]]; nested: ScriptConditionValidationException[failed to compile script [return ctx.payload.hits.total > 5] with lang [groovy] of type [INLINE]]; nested: ScriptException[dynamic scripting for [groovy] disabled]; ","status":500}

什么是动态脚本?它给我的错误,它被禁用。 我对守望者的条件如下。

curl -XPUT 'http://localhost:9200/_watcher/watch/log_error_watch' -d '{
  "metadata" : { 
    "color" : "red"
  },
  "trigger" : { 
    "schedule" : {
      "interval" : "10s"
    }
  },
  "input" : { 
    "search" : {
      "request" : {
        "search_type" : "count",
        "indices" : "logs",
        "body" : {
          "query" : { "match" : { "status" : "error" } }
        }
      }
    }
  },
  "condition" : { 
    "script" : "return ctx.payload.hits.total > 5"
  },
  "transform" : { 
    "search" : {
        "request" : {
          "indices" : "logs",
          "body" : {
            "query" : { "match" : { "status" : "error" } }
          }
        }
    }
  },
  "actions" : { 
    "my_webhook" : {
      "webhook" : {
        "method" : "GET",
        "host" : "mylisteninghost",
        "port" : 9200,
        "path" : "/{{watch_id}}",
        "body" : "Encountered {{ctx.payload.hits.total}} errors"
      }
    },
    "email_administrator" : {
      "email" : {
        "to" : "xxxxxx.xxx@gmail.com",
        "subject" : "Encountered {{ctx.payload.hits.total}} errors",
        "body" : "Too many error in the system, see attached data",
        "attach_data" : true,
        "priority" : "high"
      }
    }
  }
}'

2 个答案:

答案 0 :(得分:3)

@andrei关于如何在Elasticsearch中启用动态脚本是正确的,我即将粘贴相同的链接。

但是,根据您指定的条件,您实际上根本不需要使用脚本! Watcher有一个compare条件,看起来非常合适:

https://www.elastic.co/guide/en/watcher/current/condition.html#condition-compare

在您的情况下,条件将如下所示:

    {
  ...

  "condition" : {
    "compare" : {
      "ctx.payload.hits.total" : { 
        "gte" : 5 
      }
  }
  ...
}

答案 1 :(得分:0)

您需要在Elasticsearch中启用动态脚本:https://www.elastic.co/guide/en/watcher/current/condition.html#condition-script

  

评估脚本的监视条件。默认的脚本语言是groovy。只要该语言支持将表达式计算为布尔值,就可以使用Elasticsearch支持的任何脚本语言。请注意,胡须和表达式语言太有限,无法被此条件使用。有关更多信息,请参阅Elasticsearch Reference中的脚本。

     

重要

     

您必须在elasticsearch.yml中显式启用动态脚本才能使用内联或索引脚本。

实际启用动态脚本:https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#enable-dynamic-scripting