The error message that you receive indicates that the dynamic script is disabled, which is the default setting. You need to enable it to make scripts work:
Enable dynamic scripting
We recommend running Elasticsearch behind an application or proxy that protects Elasticsearch from the outside world. If users are allowed to run dynamic scripts (even in a search request), then they have the same access to your mailbox as the user who runs Elasticsearch as. For this reason, dynamic scripting is allowed only for the sandbox by default.
Firstly, you should not run Elasticsearch as the root user, as this will allow the script to access or do anything on your server without restriction. Secondly, you should not expose Elasticsearch directly but instead there is a proxy application between them. If you do, you intend to expose Elasticsearch directly to your users, then you have to decide whether you trust them enough to run scripts on your mailbox or not. If you do this, you can enable dynamic scripting by adding after installation to the config / elasticsearch.yml file on each node:
script.disable_dynamic: false
Although this still allows the execution of named scripts provided in config or native Java scripts registered through plugins, it also allows users to run arbitrary scripts through the API. Instead of sending the file name as a script, you can send the body of the script instead.
There are three possible configuration values โโfor script.disable_dynamic, the default value is sandbox:
true: all dynamic scripts are disabled, scripts must be placed in config / scripts.
false: all dynamic scripts are enabled, scripts can be sent as strings in requests.
sandbox: scripts can be sent as strings for languages โโthat are sandboxed.