lang-python.asciidoc 3.6 KB

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