ElasticSearch Watcher疑难解答:lawrical_argument_exception,Reson:无关的循环

时间:2018-10-22 07:20:43

标签: elasticsearch

我的观察者运行良好,并且成功发出警报,但是现在我遇到了麻烦。

为了实现一些更复杂的触发条件,我将观察者的条件从比较脚本更改为无痛脚本,并且得到了invalid_argument_exception,并且reson无关循环。 这是我的手表json:

PUT /_xpack/watcher/watch/test11111
{
  "trigger": {
    "schedule": {
      "interval": "1m"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "auto-article-web-prod-*"
        ],
        "types": [],
        "body": {
          "size": 0,
          "query": {
            "bool": {
              "filter": [
                {
                  "range": {
                    "@timestamp": {
                      "from": "now-20m",
                      "to": "now"
                    }
                  }
                }
              ]
            }
          },
          "aggs": {
            "failed_api": {
              "terms": {
                "field": "log_content.apiName"
              }
            }
          }
        }
      }
    }
  },
  "condition" : {
      "script" : """
      for (bucket in ctx.payload.aggregations.failed_api.buckets) {
        String key = bucket.key;
        int value = bucket.doc_count;
        if(key == 'articlenumber'){
            return value >= 5;
        }
        else if(value >= 20){
            return true;
        }
        else{
            return false;
        }
    }
      """
    },
  "actions": {
    "my-logging-action": {
    ...
    }
  }
}

,回复为:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "      for (bucket in ctx.payloa ...",
          "      ^---- HERE"
        ],
        "script": "      for (bucket in ctx.payload.aggregations.failed_api.buckets) {\n        String key = bucket.key;\n        int value = bucket.doc_count;\n        if(key == 'article'){\n            return value >= 5;\n        }\n        else if(value >= 20){\n            return true;\n        }\n        else{\n            return false;\n        }\n    }",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "compile error",
    "script_stack": [
      "      for (bucket in ctx.payloa ...",
      "      ^---- HERE"
    ],
    "script": "      for (bucket in ctx.payload.aggregations.failed_api.buckets) {\n        String key = bucket.key;\n        int value = bucket.doc_count;\n        if(key == 'article'){\n            return value >= 5;\n        }\n        else if(value >= 20){\n            return true;\n        }\n        else{\n            return false;\n        }\n    }",
    "lang": "painless",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Extraneous for loop."
    }
  },
  "status": 500
}

根据我的输入,这是查询结果:

{
  "took": 120,
  "timed_out": false,
  "_shards": {
    "total": 139,
    "successful": 139,
    "skipped": 133,
    "failed": 0
  },
  "hits": {
    "total": 70,
    "max_score": 0,
    "hits": [
      ...
    ]
  },
  "aggregations": {
    "failed_api": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "articlenumber",
          "doc_count": 38
        },
        {
          "key": "authorad",
          "doc_count": 15
        },
        {
          "key": "articlepv",
          "doc_count": 13
        },
        {
          "key": "authorarticlelist",
          "doc_count": 3
        },
        {
          "key": "author",
          "doc_count": 1
        }
      ]
    }
  }
}

我检查了Elastic的文档。但是没有任何有用的信息。如何用无痛脚本编写Corret循环?还有更具体的调试信息吗?

1 个答案:

答案 0 :(得分:0)

最后,我在脚本中发现错误,该错误将在第一次循环时返回,并且在更改脚本后观察程序运行良好。