script.asciidoc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. [[script-processor]]
  2. === Script processor
  3. ++++
  4. <titleabbrev>Script</titleabbrev>
  5. ++++
  6. Allows inline and stored scripts to be executed within ingest pipelines.
  7. See <<modules-scripting-using, How to use scripts>> to learn more about writing scripts. The Script Processor
  8. leverages caching of compiled scripts for improved performance. Since the
  9. script specified within the processor is potentially re-compiled per document, it is important
  10. to understand how script caching works. To learn more about
  11. caching see <<modules-scripting-using-caching, Script Caching>>.
  12. [[script-options]]
  13. .Script Options
  14. [options="header"]
  15. |======
  16. | Name | Required | Default | Description
  17. | `lang` | no | "painless" | The scripting language
  18. | `id` | no | - | The stored script id to refer to
  19. | `source` | no | - | An inline script to be executed
  20. | `params` | no | - | Script Parameters
  21. include::common-options.asciidoc[]
  22. |======
  23. One of `id` or `source` options must be provided in order to properly reference a script to execute.
  24. You can access the current ingest document from within the script context by using the `ctx` variable.
  25. The following example sets a new field called `field_a_plus_b_times_c` to be the sum of two existing
  26. numeric fields `field_a` and `field_b` multiplied by the parameter param_c:
  27. [source,js]
  28. --------------------------------------------------
  29. {
  30. "script": {
  31. "lang": "painless",
  32. "source": "ctx.field_a_plus_b_times_c = (ctx.field_a + ctx.field_b) * params.param_c",
  33. "params": {
  34. "param_c": 10
  35. }
  36. }
  37. }
  38. --------------------------------------------------
  39. // NOTCONSOLE
  40. It is possible to use the Script Processor to manipulate document metadata like `_index` during
  41. ingestion. Here is an example of an Ingest Pipeline that renames the index to `my-index` no matter what
  42. was provided in the original index request:
  43. [source,console]
  44. --------------------------------------------------
  45. PUT _ingest/pipeline/my-index
  46. {
  47. "description": "use index:my-index",
  48. "processors": [
  49. {
  50. "script": {
  51. "source": """
  52. ctx._index = 'my-index';
  53. """
  54. }
  55. }
  56. ]
  57. }
  58. --------------------------------------------------
  59. Using the above pipeline, we can attempt to index a document into the `any-index` index.
  60. [source,console]
  61. --------------------------------------------------
  62. PUT any-index/_doc/1?pipeline=my-index
  63. {
  64. "message": "text"
  65. }
  66. --------------------------------------------------
  67. // TEST[continued]
  68. The response from the above index request:
  69. [source,console-result]
  70. --------------------------------------------------
  71. {
  72. "_index": "my-index",
  73. "_id": "1",
  74. "_version": 1,
  75. "result": "created",
  76. "_shards": {
  77. "total": 2,
  78. "successful": 1,
  79. "failed": 0
  80. },
  81. "_seq_no": 89,
  82. "_primary_term": 1,
  83. }
  84. --------------------------------------------------
  85. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
  86. In the above response, you can see that our document was actually indexed into `my-index` instead of
  87. `any-index`. This type of manipulation is often convenient in pipelines that have various branches of transformation,
  88. and depending on the progress made, indexed into different indices.