painless-execute-script.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 does 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,console]
  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. Response:
  37. [source,console-result]
  38. --------------------------------------------------
  39. {
  40. "result": "0.1"
  41. }
  42. --------------------------------------------------
  43. ===== Filter context
  44. The `filter` context executes scripts as if they were executed inside a `script` query.
  45. For testing purposes, a document must be provided so that it will be temporarily indexed in-memory and
  46. is accessible from the script. More precisely, the _source, stored fields and doc values of such a
  47. document are available to the script being tested.
  48. The following parameters may be specified in `context_setup` for a filter context:
  49. document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
  50. index:: The name of an index containing a mapping that is compatible with the document being indexed.
  51. *Example*
  52. [source,console]
  53. ----------------------------------------------------------------
  54. PUT /my-index
  55. {
  56. "mappings": {
  57. "properties": {
  58. "field": {
  59. "type": "keyword"
  60. }
  61. }
  62. }
  63. }
  64. POST /_scripts/painless/_execute
  65. {
  66. "script": {
  67. "source": "doc['field'].value.length() <= params.max_length",
  68. "params": {
  69. "max_length": 4
  70. }
  71. },
  72. "context": "filter",
  73. "context_setup": {
  74. "index": "my-index",
  75. "document": {
  76. "field": "four"
  77. }
  78. }
  79. }
  80. ----------------------------------------------------------------
  81. Response:
  82. [source,console-result]
  83. --------------------------------------------------
  84. {
  85. "result": true
  86. }
  87. --------------------------------------------------
  88. ===== Score context
  89. The `score` context executes scripts as if they were executed inside a `script_score` function in
  90. `function_score` query.
  91. The following parameters may be specified in `context_setup` for a score context:
  92. document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
  93. index:: The name of an index containing a mapping that is compatible with the document being indexed.
  94. query:: If `_score` is used in the script then a query can specify that it will be used to compute a score.
  95. *Example*
  96. [source,console]
  97. ----------------------------------------------------------------
  98. PUT /my-index
  99. {
  100. "mappings": {
  101. "properties": {
  102. "field": {
  103. "type": "keyword"
  104. },
  105. "rank": {
  106. "type": "long"
  107. }
  108. }
  109. }
  110. }
  111. POST /_scripts/painless/_execute
  112. {
  113. "script": {
  114. "source": "doc['rank'].value / params.max_rank",
  115. "params": {
  116. "max_rank": 5.0
  117. }
  118. },
  119. "context": "score",
  120. "context_setup": {
  121. "index": "my-index",
  122. "document": {
  123. "rank": 4
  124. }
  125. }
  126. }
  127. ----------------------------------------------------------------
  128. Response:
  129. [source,console-result]
  130. --------------------------------------------------
  131. {
  132. "result": 0.8
  133. }
  134. --------------------------------------------------