lang-javascript.asciidoc 3.9 KB

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