2015-08-15 12:00:55 -04:00
|
|
|
[[lang-python]]
|
|
|
|
=== Python Language Plugin
|
|
|
|
|
2016-09-02 04:54:05 -04:00
|
|
|
deprecated[5.0.0,Python will be replaced by the new scripting language {ref}/modules-scripting-painless.html[`Painless`]]
|
2016-08-30 12:06:18 -04:00
|
|
|
|
2015-08-15 12:00:55 -04:00
|
|
|
The Python language plugin enables the use of Python in Elasticsearch
|
|
|
|
scripts, via the http://www.jython.org/[Jython] Java implementation of Python.
|
|
|
|
|
|
|
|
[[lang-python-install]]
|
|
|
|
[float]
|
|
|
|
==== Installation
|
|
|
|
|
|
|
|
This plugin can be installed using the plugin manager:
|
|
|
|
|
|
|
|
[source,sh]
|
|
|
|
----------------------------------------------------------------
|
2016-02-04 10:00:55 -05:00
|
|
|
sudo bin/elasticsearch-plugin install lang-python
|
2015-08-15 12:00:55 -04:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
The plugin must be installed on every node in the cluster, and each node must
|
|
|
|
be restarted after installation.
|
|
|
|
|
2016-09-19 09:04:29 -04:00
|
|
|
This plugin can be downloaded for <<plugin-management-custom-url,offline install>> from
|
|
|
|
{plugin_url}/lang-python/{version}/lang-python-{version}.zip.
|
2016-09-12 09:34:44 -04:00
|
|
|
|
2015-08-15 12:00:55 -04:00
|
|
|
[[lang-python-remove]]
|
|
|
|
[float]
|
|
|
|
==== Removal
|
|
|
|
|
|
|
|
The plugin can be removed with the following command:
|
|
|
|
|
|
|
|
[source,sh]
|
|
|
|
----------------------------------------------------------------
|
2016-02-04 10:00:55 -05:00
|
|
|
sudo bin/elasticsearch-plugin remove lang-python
|
2015-08-15 12:00:55 -04:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
The node must be stopped before removing the plugin.
|
|
|
|
|
|
|
|
[[lang-python-usage]]
|
|
|
|
==== Using Python in Elasticsearch
|
|
|
|
|
|
|
|
Once the plugin has been installed, Python can be used at a scripting
|
|
|
|
language 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 scripts
|
|
|
|
|
|
|
|
WARNING: Enabling inline scripting on an unprotected Elasticsearch cluster is dangerous.
|
|
|
|
See <<lang-python-file>> for a safer option.
|
|
|
|
|
2016-05-04 16:38:57 -04:00
|
|
|
If you have enabled {ref}/modules-scripting-security.html#enable-dynamic-scripting[inline scripts],
|
2015-08-15 12:00:55 -04:00
|
|
|
you can use Python as follows:
|
|
|
|
|
2016-05-12 12:43:01 -04:00
|
|
|
[source,js]
|
2015-08-15 12:00:55 -04:00
|
|
|
----
|
|
|
|
PUT 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2015-08-15 12:00:55 -04:00
|
|
|
|
2016-05-04 16:38:57 -04:00
|
|
|
[[lang-python-stored]]
|
2015-08-15 12:00:55 -04:00
|
|
|
[float]
|
2016-05-04 16:38:57 -04:00
|
|
|
=== Stored scripts
|
2015-08-15 12:00:55 -04:00
|
|
|
|
2016-05-04 16:38:57 -04:00
|
|
|
WARNING: Enabling stored scripts on an unprotected Elasticsearch cluster is dangerous.
|
2015-08-15 12:00:55 -04:00
|
|
|
See <<lang-python-file>> for a safer option.
|
|
|
|
|
2016-05-04 16:38:57 -04:00
|
|
|
If you have enabled {ref}/modules-scripting-security.html#enable-dynamic-scripting[stored scripts],
|
2015-08-15 12:00:55 -04:00
|
|
|
you can use Python as follows:
|
|
|
|
|
2016-05-12 12:43:01 -04:00
|
|
|
[source,js]
|
2015-08-15 12:00:55 -04:00
|
|
|
----
|
|
|
|
PUT 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
----
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2015-08-15 12:00:55 -04:00
|
|
|
|
2016-05-04 16:38:57 -04:00
|
|
|
<1> We store the script under the id `my_script`.
|
2015-08-15 12:00:55 -04:00
|
|
|
<2> The function score query retrieves the script with id `my_script`.
|
|
|
|
|
|
|
|
|
|
|
|
[[lang-python-file]]
|
|
|
|
[float]
|
|
|
|
=== File scripts
|
|
|
|
|
|
|
|
You can save your scripts to a file in the `config/scripts/` directory on
|
2016-02-28 17:03:43 -05:00
|
|
|
every node. The `.py` file suffix identifies the script as containing
|
2015-08-15 12:00:55 -04:00
|
|
|
Python:
|
|
|
|
|
2016-02-28 17:03:43 -05:00
|
|
|
First, save this file as `config/scripts/my_script.py` on every node
|
2015-08-15 12:00:55 -04:00
|
|
|
in the cluster:
|
|
|
|
|
|
|
|
[source,python]
|
|
|
|
----
|
|
|
|
doc["num"].value * factor
|
|
|
|
----
|
|
|
|
|
|
|
|
then use the script as follows:
|
|
|
|
|
2016-05-12 12:43:01 -04:00
|
|
|
[source,js]
|
2015-08-15 12:00:55 -04:00
|
|
|
----
|
|
|
|
PUT 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2015-08-15 12:00:55 -04:00
|
|
|
|
2016-02-28 17:03:43 -05:00
|
|
|
<1> The function score query retrieves the script with filename `my_script.py`.
|