2016-11-12 85 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