script.asciidoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. [[script-processor]]
  2. === Script processor
  3. ++++
  4. <titleabbrev>Script</titleabbrev>
  5. ++++
  6. Runs an inline or stored <<modules-scripting,script>> on incoming documents. The
  7. script runs in the {painless}/painless-ingest-processor-context.html[`ingest`]
  8. context.
  9. The script processor uses the <<scripts-and-search-speed,script cache>> to avoid
  10. recompiling the script for each incoming document. To improve performance,
  11. ensure the script cache is properly sized before using a script processor in
  12. production.
  13. [[script-options]]
  14. .Script options
  15. [options="header"]
  16. |======
  17. | Name | Required | Default | Description
  18. | `lang` | no | "painless" | <<scripting-available-languages,Script language>>.
  19. | `id` | no | - | ID of a <<create-stored-script-api,stored script>>.
  20. If no `source` is specified, this parameter is required.
  21. | `source` | no | - | Inline script.
  22. If no `id` is specified, this parameter is required.
  23. | `params` | no | - | Object containing parameters for the script.
  24. include::common-options.asciidoc[]
  25. |======
  26. [discrete]
  27. [[script-processor-access-source-fields]]
  28. ==== Access source fields
  29. The script processor parses each incoming document's JSON source fields into a
  30. set of maps, lists, and primitives. To access these fields with a Painless
  31. script, use the
  32. {painless}/painless-operators-reference.html#map-access-operator[map access
  33. operator]: `ctx['my-field']`. You can also use the shorthand `ctx.<my-field>`
  34. syntax.
  35. NOTE: The script processor does not support the `ctx['_source']['my-field']` or
  36. `ctx._source.<my-field>` syntaxes.
  37. The following processor uses a Painless script to extract the `tags` field from
  38. the `env` source field.
  39. [source,console]
  40. ----
  41. POST _ingest/pipeline/_simulate
  42. {
  43. "pipeline": {
  44. "processors": [
  45. {
  46. "script": {
  47. "description": "Extract 'tags' from 'env' field",
  48. "lang": "painless",
  49. "source": """
  50. String[] envSplit = ctx['env'].splitOnToken(params['delimiter']);
  51. ArrayList tags = new ArrayList();
  52. tags.add(envSplit[params['position']].trim());
  53. ctx['tags'] = tags;
  54. """,
  55. "params": {
  56. "delimiter": "-",
  57. "position": 1
  58. }
  59. }
  60. }
  61. ]
  62. },
  63. "docs": [
  64. {
  65. "_source": {
  66. "env": "es01-prod"
  67. }
  68. }
  69. ]
  70. }
  71. ----
  72. The processor produces:
  73. [source,console-result]
  74. ----
  75. {
  76. "docs": [
  77. {
  78. "doc": {
  79. ...
  80. "_source": {
  81. "env": "es01-prod",
  82. "tags": [
  83. "prod"
  84. ]
  85. }
  86. }
  87. }
  88. ]
  89. }
  90. ----
  91. // TESTRESPONSE[s/\.\.\./"_index":"_index","_id":"_id","_ingest":{"timestamp":$body.docs.0.doc._ingest.timestamp},/]
  92. [discrete]
  93. [[script-processor-access-metadata-fields]]
  94. ==== Access metadata fields
  95. You can also use a script processor to access metadata fields. The following
  96. processor uses a Painless script to set an incoming document's `_index`.
  97. [source,console]
  98. ----
  99. POST _ingest/pipeline/_simulate
  100. {
  101. "pipeline": {
  102. "processors": [
  103. {
  104. "script": {
  105. "description": "Set index based on `lang` field and `dataset` param",
  106. "lang": "painless",
  107. "source": """
  108. ctx['_index'] = ctx['lang'] + '-' + params['dataset'];
  109. """,
  110. "params": {
  111. "dataset": "catalog"
  112. }
  113. }
  114. }
  115. ]
  116. },
  117. "docs": [
  118. {
  119. "_index": "generic-index",
  120. "_source": {
  121. "lang": "fr"
  122. }
  123. }
  124. ]
  125. }
  126. ----
  127. The processor changes the document's `_index` to `fr-catalog` from
  128. `generic-index`.
  129. [source,console-result]
  130. ----
  131. {
  132. "docs": [
  133. {
  134. "doc": {
  135. ...
  136. "_index": "fr-catalog",
  137. "_source": {
  138. "lang": "fr"
  139. }
  140. }
  141. }
  142. ]
  143. }
  144. ----
  145. // TESTRESPONSE[s/\.\.\./"_id":"_id","_ingest":{"timestamp":$body.docs.0.doc._ingest.timestamp},/]