| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 | [[lang-python]]=== Python Language PluginThe Python language plugin enables the use of Python in Elasticsearchscripts, via the http://www.jython.org/[Jython] Java implementation of Python.[[lang-python-install]][float]==== InstallationThis plugin can be installed using the plugin manager:[source,sh]----------------------------------------------------------------sudo bin/elasticsearch-plugin install lang-python----------------------------------------------------------------The plugin must be installed on every node in the cluster, and each node mustbe restarted after installation.[[lang-python-remove]][float]==== RemovalThe plugin can be removed with the following command:[source,sh]----------------------------------------------------------------sudo bin/elasticsearch-plugin remove lang-python----------------------------------------------------------------The node must be stopped before removing the plugin.[[lang-python-usage]]==== Using Python in ElasticsearchOnce the plugin has been installed, Python can be used at a scriptinglanguage by setting the `lang` parameter to `python`.Scripting is available in many APIs, but we will use an example with the`function_score` for demonstration purposes:[[lang-python-inline]][float]=== Inline scriptsWARNING: Enabling inline scripting on an unprotected Elasticsearch cluster is dangerous.See <<lang-python-file>> for a safer option.If you have enabled {ref}/modules-scripting-security.html#enable-dynamic-scripting[inline scripts],you can use Python as follows:[source,json]----DELETE testPUT test/doc/1{  "num": 1.0}PUT test/doc/2{  "num": 2.0}GET test/_search{  "query": {    "function_score": {      "script_score": {        "script": {          "inline": "doc[\"num\"].value * factor",          "lang": "python",          "params": {            "factor": 2          }        }      }    }  }}----// CONSOLE[[lang-python-stored]][float]=== Stored scriptsWARNING: Enabling stored scripts on an unprotected Elasticsearch cluster is dangerous.See <<lang-python-file>> for a safer option.If you have enabled {ref}/modules-scripting-security.html#enable-dynamic-scripting[stored scripts],you can use Python as follows:[source,json]----DELETE testPUT test/doc/1{  "num": 1.0}PUT test/doc/2{  "num": 2.0}POST _scripts/python/my_script  <1>{  "script": "doc[\"num\"].value * factor"}GET test/_search{  "query": {    "function_score": {      "script_score": {        "script": {          "id": "my_script", <2>          "lang": "python",          "params": {            "factor": 2          }        }      }    }  }}----// CONSOLE<1> We store the script under the id `my_script`.<2> The function score query retrieves the script with id `my_script`.[[lang-python-file]][float]=== File scriptsYou can save your scripts to a file in the `config/scripts/` directory onevery node. The `.py` file suffix identifies the script as containingPython:First, save this file as `config/scripts/my_script.py` on every nodein the cluster:[source,python]----doc["num"].value * factor----then use the script as follows:[source,json]----DELETE testPUT test/doc/1{  "num": 1.0}PUT test/doc/2{  "num": 2.0}GET test/_search{  "query": {    "function_score": {      "script_score": {        "script": {          "file": "my_script", <1>          "lang": "python",          "params": {            "factor": 2          }        }      }    }  }}----// CONSOLE<1> The function score query retrieves the script with filename `my_script.py`.
 |