lang-javascript.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. [[lang-javascript]]
  2. === JavaScript Language Plugin
  3. The JavaScript language plugin enables the use of JavaScript in Elasticsearch
  4. scripts, via Mozilla's
  5. https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino[Rhino JavaScript] engine.
  6. [[lang-javascript-install]]
  7. [float]
  8. ==== Installation
  9. This plugin can be installed using the plugin manager:
  10. [source,sh]
  11. ----------------------------------------------------------------
  12. sudo bin/elasticsearch-plugin install lang-javascript
  13. ----------------------------------------------------------------
  14. The plugin must be installed on every node in the cluster, and each node must
  15. be restarted after installation.
  16. [[lang-javascript-remove]]
  17. [float]
  18. ==== Removal
  19. The plugin can be removed with the following command:
  20. [source,sh]
  21. ----------------------------------------------------------------
  22. sudo bin/elasticsearch-plugin remove lang-javascript
  23. ----------------------------------------------------------------
  24. The node must be stopped before removing the plugin.
  25. [[lang-javascript-usage]]
  26. ==== Using JavaScript in Elasticsearch
  27. Once the plugin has been installed, JavaScript can be used at a scripting
  28. language by setting the `lang` parameter to `javascript`.
  29. Scripting is available in many APIs, but we will use an example with the
  30. `function_score` for demonstration purposes:
  31. [[lang-javascript-inline]]
  32. [float]
  33. === Inline scripts
  34. WARNING: Enabling inline scripting on an unprotected Elasticsearch cluster is dangerous.
  35. See <<lang-javascript-file>> for a safer option.
  36. If you have enabled {ref}/modules-scripting-security.html#enable-dynamic-scripting[inline scripts],
  37. you can use JavaScript as follows:
  38. [source,js]
  39. ----
  40. DELETE test
  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. DELETE test
  77. PUT test/doc/1
  78. {
  79. "num": 1.0
  80. }
  81. PUT test/doc/2
  82. {
  83. "num": 2.0
  84. }
  85. POST _scripts/javascript/my_script <1>
  86. {
  87. "script": "doc[\"num\"].value * factor"
  88. }
  89. GET test/_search
  90. {
  91. "query": {
  92. "function_score": {
  93. "script_score": {
  94. "script": {
  95. "id": "my_script", <2>
  96. "lang": "javascript",
  97. "params": {
  98. "factor": 2
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. ----
  106. // CONSOLE
  107. <1> We store the script under the id `my_script`.
  108. <2> The function score query retrieves the script with id `my_script`.
  109. [[lang-javascript-file]]
  110. [float]
  111. === File scripts
  112. You can save your scripts to a file in the `config/scripts/` directory on
  113. every node. The `.javascript` file suffix identifies the script as containing
  114. JavaScript:
  115. First, save this file as `config/scripts/my_script.javascript` on every node
  116. in the cluster:
  117. [source,js]
  118. ----
  119. doc["num"].value * factor
  120. ----
  121. then use the script as follows:
  122. [source,js]
  123. ----
  124. DELETE test
  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`.