lang-javascript.asciidoc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. [[lang-javascript]]
  2. === JavaScript Language Plugin
  3. deprecated[5.0.0,Javascript will be replaced by the new scripting language <<modules-scripting-painless, `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. // NOTCONSOLE
  16. The plugin must be installed on every node in the cluster, and each node must
  17. be restarted after installation.
  18. [[lang-javascript-remove]]
  19. [float]
  20. ==== Removal
  21. The plugin can be removed with the following command:
  22. [source,sh]
  23. ----------------------------------------------------------------
  24. sudo bin/elasticsearch-plugin remove lang-javascript
  25. ----------------------------------------------------------------
  26. // NOTCONSOLE
  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,js]
  119. ----
  120. doc["num"].value * factor
  121. ----
  122. // NOTCONSOLE
  123. then use the script as follows:
  124. [source,js]
  125. ----
  126. PUT test/doc/1
  127. {
  128. "num": 1.0
  129. }
  130. PUT test/doc/2
  131. {
  132. "num": 2.0
  133. }
  134. GET test/_search
  135. {
  136. "query": {
  137. "function_score": {
  138. "script_score": {
  139. "script": {
  140. "file": "my_script", <1>
  141. "lang": "javascript",
  142. "params": {
  143. "factor": 2
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. ----
  151. // CONSOLE
  152. <1> The function score query retrieves the script with filename `my_script.javascript`.