lang-python.asciidoc 3.5 KB

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