lang-python.asciidoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. DELETE test
  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. DELETE test
  76. PUT test/doc/1
  77. {
  78. "num": 1.0
  79. }
  80. PUT test/doc/2
  81. {
  82. "num": 2.0
  83. }
  84. POST _scripts/python/my_script <1>
  85. {
  86. "script": "doc[\"num\"].value * factor"
  87. }
  88. GET test/_search
  89. {
  90. "query": {
  91. "function_score": {
  92. "script_score": {
  93. "script": {
  94. "id": "my_script", <2>
  95. "lang": "python",
  96. "params": {
  97. "factor": 2
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. ----
  105. // CONSOLE
  106. <1> We store the script under the id `my_script`.
  107. <2> The function score query retrieves the script with id `my_script`.
  108. [[lang-python-file]]
  109. [float]
  110. === File scripts
  111. You can save your scripts to a file in the `config/scripts/` directory on
  112. every node. The `.py` file suffix identifies the script as containing
  113. Python:
  114. First, save this file as `config/scripts/my_script.py` on every node
  115. in the cluster:
  116. [source,python]
  117. ----
  118. doc["num"].value * factor
  119. ----
  120. then use the script as follows:
  121. [source,js]
  122. ----
  123. DELETE test
  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`.