2016-11-12 86 views
0

我想使用ElasticSearch's _update api调用外部脚本。但是,似乎脚本从未实际运行。一个等效的内联脚本确实会进行预期的更新。ElasticSearch _update脚本文件未执行

任何想法为什么这可能是这种情况?


脚本是这样的:

脚本/ 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" 
     } 
    ] 
    } 
} 

回答

0

问题在于引用脚本。这就是我得到的复制和粘贴。

所以不是

"ctx._source.comments+=new_comment" 

脚本应该是这样的:

ctx._source.comments+=new_comment 
0

你的剧本是根本保存在正确的位置。

正如file script documentation解释,你需要将它保存到$ES_HOME/config/scripts/或者如果你想将它们保存在不同的位置,你需要更改path.scripts设置在elasticsearch.yml

+0

它保存在正确的地方,否则我会得到一个错误说,它不能被发现。我也看到在标准输出脚本加载 – funseiki

+0

然后这是不正确的'config/update_comments.groovy'和误导 – Val

+0

你有'script.file:true'在你的配置? – Val