script.asciidoc 3.5 KB

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