バージョン情報
- Elasticsearch 1.4.4
現象
Elasticsearchに以下のようなクエリを投げようとしました.
{
"query" : {
"function_score" : {
"query" : {
"simple_query_string" : {
"query": "アルパカ",
"fields": ["_all"]
}
},
"boost_mode": "replace",
"script_score" : {
"script" : "_score * 100.0"
}
}
}
}
simple_query_string
を使っていずれかのフィールドに"アルパカ"を含むドキュメントを探します.
次に, function_score
のscript_score
によって, 見つかったドキュメントの点数を100倍にします.
これが動かない! どうしても動かない!
原因
Elasticsearchのログをよく見たら, org.elasticsearch.search.SearchParseException
の下に, Caused by: org.elasticsearch.script.ScriptException: dynamic scripting for [groovy] disabled
って書いてありました.
調べていると, 下記のページを発見.
Elasticsearch error: SearchPhaseExecutionException
You need to enable dynamic scripting. Or copy the mvel files to the Es directory.
- Slavik, Aug 26, 2014
どうやら最近のバージョンだとデフォルトでスクリプトが無効にされているらしい. なんてこと!
デフォルトOFFにされた経緯は, 公式のScriptingのページに書かれていました.
Elasticsearch versions 1.3.0-1.3.7 and 1.4.0-1.4.2 have a vulnerability in the Groovy scripting engine. The vulnerability allows an attacker to construct Groovy scripts that escape the sandbox and execute shell commands as the user running the Elasticsearch Java VM.
解決方法
elasticsearch.yml
のどこかに, スクリプトを有効化する設定を追記します.
script.groovy.sandbox.enabled: true
設定変更後, Elasticsearchを再起動したのち先ほどのクエリを投げたところ, 無事にアルパカの点数が100倍になりました.