lang-javascript.asciidoc 3.8 KB

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