初學Elasticsearch,在按照《Elasticsearch服務器開發(第2版)》進行學習的過程中,在P17頁中1.4.5 更新文檔
小節,使用腳本對文檔進行局部更新的時候遇到了如下報錯:
? ~ curl -XPOST http://127.0.0.1:9200/blog/article/1/_update -d '{"script": "ctx._source.content=\"new content\""}'
{
"error":{
"root_cause":[{
"type":"remote_transport_exception",
"reason":"[Lady Mandarin][127.0.0.1:9300][indices:data/write/update[s]]"
}
],
"type":"illegal_argument_exception",
"reason":"failed to execute script",
"caused_by":{
"type":"script_exception",
"reason":"scripts of type [inline], operation [update] and lang [groovy] are disabled"}},
"status":400
}%
我根據提示中的原因"scripts of type [inline], operation [update] and lang [groovy] are disabled"
進行了查詢,查到了官網文檔關于對腳本更新的介紹(介紹鏈接)。
通過粗略查看文檔,我發現要解決這個問題,需要在Elasticsearch的配置文件elasticsearch.yml
中添加如下配置:
script.engine.groovy.inline.update: on
由于Elasticsearch默認使用的是Groovy語言。Groovy語言一個快速且功能豐富的腳本語言,語法類似于Javascript。它在一個沙盒(sandbox)中運行,以防止惡意用戶毀壞Elasticsearch或攻擊服務器。
由于默認Elasticsearch沒有打開Groovy的update權限,因此我們無法通過Groovy進行更新操作,通過上述配置打開權限之后,就可以進行更新操作了。
? ~ curl -XPOST http://127.0.0.1:9200/blog/article/1/_update -d '{"script": "ctx._source.content=\"new content\""}'
{
"_index":"blog",
"_type":"article",
"_id":"1",
"_version":2,
"_shards":{
"total":2,
"successful":1,
"failed":0
}
}%