ElasticSearch _update,脚本文件未执行

时间:2016-11-12 01:26:01

标签: elasticsearch

我想使用ElasticSearch's _update api调用外部脚本。但是,似乎脚本实际上从未运行过。等效的内联脚本确实可以进行预期的更新。

关于为什么会出现这种情况的任何想法?

脚本如下所示:

脚本/ update_comments.groovy

"ctx._source.comments+=new_comment"

我的弹性查询如下所示:

POST my_index/blog/1/_update
{
  "script": {
    "script_file": "update_comments",
    "params": {
        "new_comment": {
          "name":    "Jon Snow",
          "comment": "Winter is coming"
        }
    }
  }
}

运行GET /my_index/blog/1会返回原始文档,而不是更新的文档。请注意,_version数字会递增,但没有任何变化。

{
  "_index": "my_index",
  "_type": "blog",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "name": "Guy",
    "body": "This is a post",
    "comments": [
      {
        "name": "Foo bar",
        "comment": "Great article"
      }
    ]
  }
}

为了进行测试,我设置script.groovy.sandbox.enabled: true并运行相同的查询,只需使用内联脚本:

{
  "script": "ctx._source.comments+=new_comment",
  "params": {
    "new_comment": {
      "name":    "Jon Snow",
      "comment": "Winter is coming"
    }
  }
}

得到了预期的结果:

{
  "_index": "my_index",
  "_type": "blog",
  "_id": "1",
  "_version": 3,
  "found": true,
  "_source": {
    "name": "Guy",
    "body": "This is a post",
    "comments": [
      {
        "name": "Foo Bar",
        "comment": "Great article"
      },
      {
        "name": "Jon Snow",
        "comment": "Winter is coming"
      }
    ]
  }
}

2 个答案:

答案 0 :(得分:0)

Your script is simply not saved in the right location.

As explained in the file script documentation, you need to save it into $ES_HOME/config/scripts/ or if you want to save them in a different location you need to change the path.scripts setting in elasticsearch.yml.

答案 1 :(得分:0)

问题是脚本周围的引号。这就是我复制和粘贴的原因。

所以而不是

"ctx._source.comments+=new_comment"

脚本应如下所示:

ctx._source.comments+=new_comment