Script processor

Allows inline and stored scripts to be executed within ingest pipelines.

See How to use scripts to learn more about writing scripts. The Script Processor leverages caching of compiled scripts for improved performance. Since the script specified within the processor is potentially re-compiled per document, it is important to understand how script caching works. To learn more about caching see Script Caching.

Table 31. Script Options

NameRequiredDefaultDescription

lang

no

“painless”

The scripting language

id

no

-

The stored script id to refer to

source

no

-

An inline script to be executed

params

no

-

Script Parameters

if

no

-

Conditionally execute this processor.

on_failure

no

-

Handle failures for this processor. See Handling Failures in Pipelines.

ignore_failure

no

false

Ignore failures for this processor. See Handling Failures in Pipelines.

tag

no

-

An identifier for this processor. Useful for debugging and metrics.

One of id or source options must be provided in order to properly reference a script to execute.

You can access the current ingest document from within the script context by using the ctx variable.

The following example sets a new field called field_a_plus_b_times_c to be the sum of two existing numeric fields field_a and field_b multiplied by the parameter param_c:

  1. {
  2. "script": {
  3. "lang": "painless",
  4. "source": "ctx.field_a_plus_b_times_c = (ctx.field_a + ctx.field_b) * params.param_c",
  5. "params": {
  6. "param_c": 10
  7. }
  8. }
  9. }

It is possible to use the Script Processor to manipulate document metadata like _index and _type during ingestion. Here is an example of an Ingest Pipeline that renames the index and type to my-index no matter what was provided in the original index request:

  1. PUT _ingest/pipeline/my-index
  2. {
  3. "description": "use index:my-index",
  4. "processors": [
  5. {
  6. "script": {
  7. "source": """
  8. ctx._index = 'my-index';
  9. ctx._type = '_doc';
  10. """
  11. }
  12. }
  13. ]
  14. }

Using the above pipeline, we can attempt to index a document into the any-index index.

  1. PUT any-index/_doc/1?pipeline=my-index
  2. {
  3. "message": "text"
  4. }

The response from the above index request:

  1. {
  2. "_index": "my-index",
  3. "_type": "_doc",
  4. "_id": "1",
  5. "_version": 1,
  6. "result": "created",
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "_seq_no": 89,
  13. "_primary_term": 1,
  14. }

In the above response, you can see that our document was actually indexed into my-index instead of any-index. This type of manipulation is often convenient in pipelines that have various branches of transformation, and depending on the progress made, indexed into different indices.