lang-python.asciidoc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. [[lang-python]]
  2. === Python Language Plugin
  3. deprecated[5.0.0,Python will be replaced by the new scripting language {ref}/modules-scripting-painless.html[`Painless`]]
  4. The Python language plugin enables the use of Python in Elasticsearch
  5. scripts, via the http://www.jython.org/[Jython] Java implementation of Python.
  6. [[lang-python-install]]
  7. [float]
  8. ==== Installation
  9. This plugin can be installed using the plugin manager:
  10. [source,sh]
  11. ----------------------------------------------------------------
  12. sudo bin/elasticsearch-plugin install lang-python
  13. ----------------------------------------------------------------
  14. The plugin must be installed on every node in the cluster, and each node must
  15. be restarted after installation.
  16. This plugin can be downloaded for <<plugin-management-custom-url,offline install>> from
  17. {plugin_url}/lang-python/{version}/lang-python-{version}.zip.
  18. [[lang-python-remove]]
  19. [float]
  20. ==== Removal
  21. The plugin can be removed with the following command:
  22. [source,sh]
  23. ----------------------------------------------------------------
  24. sudo bin/elasticsearch-plugin remove lang-python
  25. ----------------------------------------------------------------
  26. The node must be stopped before removing the plugin.
  27. [[lang-python-usage]]
  28. ==== Using Python in Elasticsearch
  29. Once the plugin has been installed, Python can be used at a scripting
  30. language by setting the `lang` parameter to `python`.
  31. Scripting is available in many APIs, but we will use an example with the
  32. `function_score` for demonstration purposes:
  33. [[lang-python-inline]]
  34. [float]
  35. === Inline scripts
  36. WARNING: Enabling inline scripting on an unprotected Elasticsearch cluster is dangerous.
  37. See <<lang-python-file>> for a safer option.
  38. If you have enabled {ref}/modules-scripting-security.html#enable-dynamic-scripting[inline scripts],
  39. you can use Python as follows:
  40. [source,js]
  41. ----
  42. PUT test/doc/1
  43. {
  44. "num": 1.0
  45. }
  46. PUT test/doc/2
  47. {
  48. "num": 2.0
  49. }
  50. GET test/_search
  51. {
  52. "query": {
  53. "function_score": {
  54. "script_score": {
  55. "script": {
  56. "inline": "doc[\"num\"].value * factor",
  57. "lang": "python",
  58. "params": {
  59. "factor": 2
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. ----
  67. // CONSOLE
  68. [[lang-python-stored]]
  69. [float]
  70. === Stored scripts
  71. WARNING: Enabling stored scripts on an unprotected Elasticsearch cluster is dangerous.
  72. See <<lang-python-file>> for a safer option.
  73. If you have enabled {ref}/modules-scripting-security.html#enable-dynamic-scripting[stored scripts],
  74. you can use Python as follows:
  75. [source,js]
  76. ----
  77. PUT test/doc/1
  78. {
  79. "num": 1.0
  80. }
  81. PUT test/doc/2
  82. {
  83. "num": 2.0
  84. }
  85. POST _scripts/python/my_script <1>
  86. {
  87. "script": "doc[\"num\"].value * factor"
  88. }
  89. GET test/_search
  90. {
  91. "query": {
  92. "function_score": {
  93. "script_score": {
  94. "script": {
  95. "id": "my_script", <2>
  96. "lang": "python",
  97. "params": {
  98. "factor": 2
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. ----
  106. // CONSOLE
  107. <1> We store the script under the id `my_script`.
  108. <2> The function score query retrieves the script with id `my_script`.
  109. [[lang-python-file]]
  110. [float]
  111. === File scripts
  112. You can save your scripts to a file in the `config/scripts/` directory on
  113. every node. The `.py` file suffix identifies the script as containing
  114. Python:
  115. First, save this file as `config/scripts/my_script.py` on every node
  116. in the cluster:
  117. [source,python]
  118. ----
  119. doc["num"].value * factor
  120. ----
  121. then use the script as follows:
  122. [source,js]
  123. ----
  124. PUT test/doc/1
  125. {
  126. "num": 1.0
  127. }
  128. PUT test/doc/2
  129. {
  130. "num": 2.0
  131. }
  132. GET test/_search
  133. {
  134. "query": {
  135. "function_score": {
  136. "script_score": {
  137. "script": {
  138. "file": "my_script", <1>
  139. "lang": "python",
  140. "params": {
  141. "factor": 2
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. ----
  149. // CONSOLE
  150. <1> The function score query retrieves the script with filename `my_script.py`.