scripting.asciidoc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. [[breaking_50_scripting]]
  2. === Script related changes
  3. ==== Removed 1.x script and template syntax
  4. The deprecated 1.x syntax of defining inline scripts / templates and referring to file or index base scripts / templates
  5. have been removed.
  6. The `script` and `params` string parameters can no longer be used and instead the `script` object syntax must be used.
  7. This applies for the update api, script sort, `script_score` function, `script` query, `scripted_metric` aggregation and
  8. `script_heuristic` aggregation.
  9. So this usage of inline scripts is no longer allowed:
  10. [source,js]
  11. -----------------------------------
  12. {
  13. "script_score": {
  14. "lang": "groovy",
  15. "script": "Math.log(_score * 2) + my_modifier",
  16. "params": {
  17. "my_modifier": 8
  18. }
  19. }
  20. }
  21. -----------------------------------
  22. and instead this syntax must be used:
  23. [source,js]
  24. -----------------------------------
  25. {
  26. "script_score": {
  27. "script": {
  28. "lang": "groovy",
  29. "inline": "Math.log(_score * 2) + my_modifier",
  30. "params": {
  31. "my_modifier": 8
  32. }
  33. }
  34. }
  35. }
  36. -----------------------------------
  37. The `script` or `script_file` parameter can no longer be used to refer to file based scripts and templates and instead
  38. `file` must be used.
  39. This usage of referring to file based scripts is no longer valid:
  40. [source,js]
  41. -----------------------------------
  42. {
  43. "script_score": {
  44. "script": "calculate-score",
  45. "params": {
  46. "my_modifier": 8
  47. }
  48. }
  49. }
  50. -----------------------------------
  51. This usage is valid:
  52. [source,js]
  53. -----------------------------------
  54. {
  55. "script_score": {
  56. "script": {
  57. "lang": "groovy",
  58. "file": "calculate-score",
  59. "params": {
  60. "my_modifier": 8
  61. }
  62. }
  63. }
  64. }
  65. -----------------------------------
  66. The `script_id` parameter can no longer be used the refer to indexed based scripts and templates and instead `id` must
  67. be used.
  68. This usage of referring to indexed scripts is no longer valid:
  69. [source,js]
  70. -----------------------------------
  71. {
  72. "script_score": {
  73. "script_id": "indexedCalculateScore",
  74. "params": {
  75. "my_modifier": 8
  76. }
  77. }
  78. }
  79. -----------------------------------
  80. This usage is valid:
  81. [source,js]
  82. -----------------------------------
  83. {
  84. "script_score": {
  85. "script": {
  86. "id": "indexedCalculateScore",
  87. "lang" : "groovy",
  88. "params": {
  89. "my_modifier": 8
  90. }
  91. }
  92. }
  93. }
  94. -----------------------------------
  95. ==== Template query
  96. The `query` field in the `template` query can no longer be used.
  97. This 1.x syntax can no longer be used:
  98. [source,js]
  99. -----------------------------------
  100. {
  101. "query": {
  102. "template": {
  103. "query": {"match_{{template}}": {}},
  104. "params" : {
  105. "template" : "all"
  106. }
  107. }
  108. }
  109. }
  110. -----------------------------------
  111. and instead the following syntax should be used:
  112. [source,js]
  113. -----------------------------------
  114. {
  115. "query": {
  116. "template": {
  117. "inline": {"match_{{template}}": {}},
  118. "params" : {
  119. "template" : "all"
  120. }
  121. }
  122. }
  123. }
  124. -----------------------------------
  125. ==== Search templates
  126. The top level `template` field in the search template api has been replaced with consistent template / script object
  127. syntax. This 1.x syntax can no longer be used:
  128. [source,js]
  129. -----------------------------------
  130. {
  131. "template" : {
  132. "query": { "match" : { "{{my_field}}" : "{{my_value}}" } },
  133. "size" : "{{my_size}}"
  134. },
  135. "params" : {
  136. "my_field" : "foo",
  137. "my_value" : "bar",
  138. "my_size" : 5
  139. }
  140. }
  141. -----------------------------------
  142. and instead the following syntax should be used:
  143. [source,js]
  144. -----------------------------------
  145. {
  146. "inline" : {
  147. "query": { "match" : { "{{my_field}}" : "{{my_value}}" } },
  148. "size" : "{{my_size}}"
  149. },
  150. "params" : {
  151. "my_field" : "foo",
  152. "my_value" : "bar",
  153. "my_size" : 5
  154. }
  155. }
  156. -----------------------------------
  157. ==== Indexed scripts and templates
  158. Indexed scripts and templates have been replaced by <<modules-scripting-stored-scripts,stored scripts>>
  159. which stores the scripts and templates in the cluster state instead of a dedicate `.scripts` index.
  160. For the size of stored scripts there is a soft limit of 65535 bytes. If scripts exceed that size then
  161. the `script.max_size_in_bytes` setting can be added to elasticsearch.yml to change the soft limit to a higher value.
  162. If scripts are really large, other options like native scripts should be considered.
  163. Previously indexed scripts in the `.scripts` index will not be used any more as
  164. Elasticsearch will now try to fetch the scripts from the cluster state. Upon upgrading
  165. to 5.x the `.scripts` index will remain to exist, so it can be used by a script to migrate
  166. the stored scripts from the `.scripts` index into the cluster state. The current format of the scripts
  167. and templates hasn't been changed, only the 1.x format has been removed.
  168. ===== Python migration script
  169. The following Python script can be used to import your indexed scripts into the cluster state
  170. as stored scripts:
  171. [source,python]
  172. -----------------------------------
  173. from elasticsearch import Elasticsearch,helpers
  174. es = Elasticsearch([
  175. {'host': 'localhost'}
  176. ])
  177. for doc in helpers.scan(es, index=".scripts", preserve_order=True):
  178. es.put_script(lang=doc['_type'], id=doc['_id'], body=doc['_source'])
  179. -----------------------------------
  180. This script makes use of the official Elasticsearch Python client and
  181. therefore you need to make sure that your have installed the client in your
  182. environment. For more information on this please see
  183. https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/index.html[`elasticsearch-py`].
  184. ===== Perl migration script
  185. The following Perl script can be used to import your indexed scripts into the cluster state
  186. as stored scripts:
  187. [source,perl]
  188. -----------------------------------
  189. use Search::Elasticsearch;
  190. my $es = Search::Elasticsearch->new( nodes => 'localhost:9200');
  191. my $scroll = $es->scroll_helper( index => '.scripts', sort => '_doc');
  192. while (my $doc = $scroll->next) {
  193. $e->put_script(
  194. lang => $doc->{_type},
  195. id => $doc->{_id},
  196. body => $doc->{_source}
  197. );
  198. }
  199. -----------------------------------
  200. This script makes use of the official Elasticsearch Perl client and
  201. therefore you need to make sure that your have installed the client in your
  202. environment. For more information on this please see
  203. https://metacpan.org/pod/Search::Elasticsearch[`Search::Elasticsearch`].
  204. ===== Verifying script migration
  205. After you have moved the scripts via the provided script or otherwise then you can verify with the following
  206. request if the migration has happened successfully:
  207. [source,js]
  208. -----------------------------------
  209. GET _cluster/state?filter_path=metadata.stored_scripts
  210. -----------------------------------
  211. The response should include all your scripts from the `.scripts` index.
  212. After you have verified that all your scripts have been moved, optionally as a last step,
  213. you can delete the `.scripts` index as Elasticsearch no longer uses it.
  214. ==== Indexed scripts Java APIs
  215. All the methods related to interacting with indexed scripts have been removed.
  216. The Java API methods for interacting with stored scripts have been added under `ClusterAdminClient` class.
  217. The sugar methods that used to exist on the indexed scripts API methods don't exist on the methods for
  218. stored scripts. The only way to provide scripts is by using `BytesReference` implementation, if a string needs to be
  219. provided the `BytesArray` class should be used.
  220. ==== Scripting engines now register only a single language
  221. Prior to 5.0.0, script engines could register multiple languages. The Javascript
  222. script engine in particular registered both `"lang": "js"` and `"lang":
  223. "javascript"`. Script engines can now only register a single language. All
  224. references to `"lang": "js"` should be changed to `"lang": "javascript"` for
  225. existing users of the lang-javascript plugin.
  226. ==== Scripting engines now register only a single extension
  227. Prior to 5.0.0 scripting engines could register multiple extensions. The only
  228. engine doing this was the Javascript engine, which registered "js" and
  229. "javascript". It now only registers the "js" file extension for on-disk scripts.
  230. ==== `.javascript` files are no longer supported (use `.js`)
  231. The Javascript engine previously registered "js" and "javascript". It now only
  232. registers the "js" file extension for on-disk scripts.
  233. ==== Removed scripting query string parameters from update rest api
  234. The `script`, `script_id` and `scripting_upsert` query string parameters have been removed from the update api.
  235. ==== Java transport client
  236. The `TemplateQueryBuilder` has been moved to the `lang-mustache` module.
  237. Therefor when using the `TemplateQueryBuilder` from the Java native client the
  238. lang-mustache module should be on the classpath. Also the transport client
  239. should load the lang-mustache module as plugin:
  240. [source,java]
  241. --------------------------------------------------
  242. TransportClient transportClient = TransportClient.builder()
  243. .settings(Settings.builder().put("node.name", "node"))
  244. .addPlugin(MustachePlugin.class)
  245. .build();
  246. transportClient.addTransportAddress(
  247. new InetSocketTransportAddress(new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 9300))
  248. );
  249. --------------------------------------------------
  250. Also the helper methods in `QueryBuilders` class that create a `TemplateQueryBuilder` instance have been removed,
  251. instead the constructors on `TemplateQueryBuilder` should be used.
  252. ==== Template query
  253. The `template` query has been deprecated in favour of the search template api. The `template` query is scheduled
  254. to be removed in the next major version.