12
11

More than 5 years have passed since last update.

Elasticsearchでfunction score queryのscript_scoreがどうしても動かなかった話

Last updated at Posted at 2015-03-05

バージョン情報

  • Elasticsearch 1.4.4

現象

Elasticsearchに以下のようなクエリを投げようとしました.

JSON
{
    "query" : {
        "function_score" : {
            "query" : {
                "simple_query_string" : {
                    "query": "アルパカ",
                    "fields": ["_all"]
                }
            },
            "boost_mode": "replace",
            "script_score" : {
                "script" : "_score * 100.0"
            }
        }
    }
}

simple_query_stringを使っていずれかのフィールドに"アルパカ"を含むドキュメントを探します.
次に, function_scorescript_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のどこかに, スクリプトを有効化する設定を追記します.

config/elasticsearch.yml
script.groovy.sandbox.enabled: true

設定変更後, Elasticsearchを再起動したのち先ほどのクエリを投げたところ, 無事にアルパカの点数が100倍になりました.

12
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11