painless-execute-script.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. [[painless-execute-api]]
  2. === Painless execute API
  3. experimental[The painless execute api is new and the request / response format may change in a breaking way in the future]
  4. The Painless execute API allows an arbitrary script to be executed and a result to be returned.
  5. [[painless-execute-api-parameters]]
  6. .Parameters
  7. [options="header"]
  8. |======
  9. | Name | Required | Default | Description
  10. | `script` | yes | - | The script to execute
  11. | `context` | no | `painless_test` | The context the script should be executed in.
  12. | `context_setup` | no | - | Additional parameters to the context.
  13. |======
  14. ==== Contexts
  15. Contexts control how scripts are executed, what variables are available at runtime and what the return type is.
  16. ===== Painless test context
  17. The `painless_test` context executes scripts as is and do not add any special parameters.
  18. The only variable that is available is `params`, which can be used to access user defined values.
  19. The result of the script is always converted to a string.
  20. If no context is specified then this context is used by default.
  21. *Example*
  22. Request:
  23. [source,js]
  24. ----------------------------------------------------------------
  25. POST /_scripts/painless/_execute
  26. {
  27. "script": {
  28. "source": "params.count / params.total",
  29. "params": {
  30. "count": 100.0,
  31. "total": 1000.0
  32. }
  33. }
  34. }
  35. ----------------------------------------------------------------
  36. // CONSOLE
  37. Response:
  38. [source,js]
  39. --------------------------------------------------
  40. {
  41. "result": "0.1"
  42. }
  43. --------------------------------------------------
  44. // TESTRESPONSE
  45. ===== Filter context
  46. The `filter` context executes scripts as if they were executed inside a `script` query.
  47. For testing purposes a document must be provided that will be indexed temporarily in-memory and
  48. is accessible to the script being tested. Because of this the _source, stored fields and doc values
  49. are available in the script being tested.
  50. The following parameters may be specified in `context_setup` for a filter context:
  51. document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
  52. index:: The name of an index containing a mapping that is compatable with the document being indexed.
  53. *Example*
  54. [source,js]
  55. ----------------------------------------------------------------
  56. PUT /my-index
  57. {
  58. "mappings": {
  59. "_doc": {
  60. "properties": {
  61. "field": {
  62. "type": "keyword"
  63. }
  64. }
  65. }
  66. }
  67. }
  68. POST /_scripts/painless/_execute
  69. {
  70. "script": {
  71. "source": "doc['field'].value.length() <= params.max_length",
  72. "params": {
  73. "max_length": 4
  74. }
  75. },
  76. "context": "filter",
  77. "context_setup": {
  78. "index": "my-index",
  79. "document": {
  80. "field": "four"
  81. }
  82. }
  83. }
  84. ----------------------------------------------------------------
  85. // CONSOLE
  86. Response:
  87. [source,js]
  88. --------------------------------------------------
  89. {
  90. "result": true
  91. }
  92. --------------------------------------------------
  93. // TESTRESPONSE
  94. ===== Score context
  95. The `score` context executes scripts as if they were executed inside a `script_score` function in
  96. `function_score` query.
  97. The following parameters may be specified in `context_setup` for a score context:
  98. document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
  99. index:: The name of an index containing a mapping that is compatable with the document being indexed.
  100. query:: If `_score` is used in the script then a query can specified that will be used to compute a score.
  101. *Example*
  102. [source,js]
  103. ----------------------------------------------------------------
  104. PUT /my-index
  105. {
  106. "mappings": {
  107. "_doc": {
  108. "properties": {
  109. "field": {
  110. "type": "keyword"
  111. },
  112. "rank": {
  113. "type": "long"
  114. }
  115. }
  116. }
  117. }
  118. }
  119. POST /_scripts/painless/_execute
  120. {
  121. "script": {
  122. "source": "doc['rank'].value / params.max_rank",
  123. "params": {
  124. "max_rank": 5.0
  125. }
  126. },
  127. "context": "score",
  128. "context_setup": {
  129. "index": "my-index",
  130. "document": {
  131. "rank": 4
  132. }
  133. }
  134. }
  135. ----------------------------------------------------------------
  136. // CONSOLE
  137. Response:
  138. [source,js]
  139. --------------------------------------------------
  140. {
  141. "result": 0.8
  142. }
  143. --------------------------------------------------
  144. // TESTRESPONSE