painless-execute-script.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 compatible with the document being indexed.
  53. *Example*
  54. [source,js]
  55. ----------------------------------------------------------------
  56. PUT /my-index
  57. {
  58. "mappings": {
  59. "properties": {
  60. "field": {
  61. "type": "keyword"
  62. }
  63. }
  64. }
  65. }
  66. POST /_scripts/painless/_execute
  67. {
  68. "script": {
  69. "source": "doc['field'].value.length() <= params.max_length",
  70. "params": {
  71. "max_length": 4
  72. }
  73. },
  74. "context": "filter",
  75. "context_setup": {
  76. "index": "my-index",
  77. "document": {
  78. "field": "four"
  79. }
  80. }
  81. }
  82. ----------------------------------------------------------------
  83. // CONSOLE
  84. Response:
  85. [source,js]
  86. --------------------------------------------------
  87. {
  88. "result": true
  89. }
  90. --------------------------------------------------
  91. // TESTRESPONSE
  92. ===== Score context
  93. The `score` context executes scripts as if they were executed inside a `script_score` function in
  94. `function_score` query.
  95. The following parameters may be specified in `context_setup` for a score context:
  96. document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
  97. index:: The name of an index containing a mapping that is compatible with the document being indexed.
  98. query:: If `_score` is used in the script then a query can specified that will be used to compute a score.
  99. *Example*
  100. [source,js]
  101. ----------------------------------------------------------------
  102. PUT /my-index
  103. {
  104. "mappings": {
  105. "properties": {
  106. "field": {
  107. "type": "keyword"
  108. },
  109. "rank": {
  110. "type": "long"
  111. }
  112. }
  113. }
  114. }
  115. POST /_scripts/painless/_execute
  116. {
  117. "script": {
  118. "source": "doc['rank'].value / params.max_rank",
  119. "params": {
  120. "max_rank": 5.0
  121. }
  122. },
  123. "context": "score",
  124. "context_setup": {
  125. "index": "my-index",
  126. "document": {
  127. "rank": 4
  128. }
  129. }
  130. }
  131. ----------------------------------------------------------------
  132. // CONSOLE
  133. Response:
  134. [source,js]
  135. --------------------------------------------------
  136. {
  137. "result": 0.8
  138. }
  139. --------------------------------------------------
  140. // TESTRESPONSE