script.asciidoc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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` during
  38. ingestion. Here is an example of an Ingest Pipeline that renames the index to `my_index` no matter what
  39. was provided in the original index request:
  40. [source,console]
  41. --------------------------------------------------
  42. PUT _ingest/pipeline/my_index
  43. {
  44. "description": "use index:my_index",
  45. "processors": [
  46. {
  47. "script": {
  48. "source": """
  49. ctx._index = 'my_index';
  50. """
  51. }
  52. }
  53. ]
  54. }
  55. --------------------------------------------------
  56. Using the above pipeline, we can attempt to index a document into the `any_index` index.
  57. [source,console]
  58. --------------------------------------------------
  59. PUT any_index/_doc/1?pipeline=my_index
  60. {
  61. "message": "text"
  62. }
  63. --------------------------------------------------
  64. // TEST[continued]
  65. The response from the above index request:
  66. [source,console-result]
  67. --------------------------------------------------
  68. {
  69. "_index": "my_index",
  70. "_id": "1",
  71. "_version": 1,
  72. "result": "created",
  73. "_shards": {
  74. "total": 2,
  75. "successful": 1,
  76. "failed": 0
  77. },
  78. "_seq_no": 89,
  79. "_primary_term": 1,
  80. }
  81. --------------------------------------------------
  82. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
  83. In the above response, you can see that our document was actually indexed into `my_index` instead of
  84. `any_index`. This type of manipulation is often convenient in pipelines that have various branches of transformation,
  85. and depending on the progress made, indexed into different indices.