Script processor
Runs an inline or storedscript on incoming documents. The script runs in theingest context.
The script processor uses thescript cache to avoid recompiling the script for each incoming document. To improve performance, ensure the script cache is properly sized before using a script processor in production.
| Name | Required | Default | Description |
|---|---|---|---|
lang | no | "painless" | Script language. |
id | no | - | ID of astored script. If nosource is specified, this parameter is required. |
source | no | - | Inline script. If noid is specified, this parameter is required. |
params | no | - | Object containing parameters for the script. |
description | no | - | Description of the processor. Useful for describing the purpose of the processor or its configuration. |
if | no | - | Conditionally execute the processor. SeeConditionally run a processor. |
ignore_failure | no | false | Ignore failures for the processor. SeeHandling pipeline failures. |
on_failure | no | - | Handle failures for the processor. SeeHandling pipeline failures. |
tag | no | - | Identifier for the processor. Useful for debugging and metrics. |
The script processor parses each incoming document’s JSON source fields into a set of maps, lists, and primitives. To access these fields with a Painless script, use themap access operator:ctx['my-field']. You can also use the shorthandctx.<my-field> syntax.
The script processor does not support thectx['_source']['my-field'] orctx._source.<my-field> syntaxes.
The following processor uses a Painless script to extract thetags field from theenv source field.
POST _ingest/pipeline/_simulate{ "pipeline": { "processors": [ { "script": { "description": "Extract 'tags' from 'env' field", "lang": "painless", "source": """ String[] envSplit = ctx['env'].splitOnToken(params['delimiter']); ArrayList tags = new ArrayList(); tags.add(envSplit[params['position']].trim()); ctx['tags'] = tags; """, "params": { "delimiter": "-", "position": 1 } } } ] }, "docs": [ { "_source": { "env": "es01-prod" } } ]}
The processor produces:
{ "docs": [ { "doc": { ... "_source": { "env": "es01-prod", "tags": [ "prod" ] } } } ]}You can also use a script processor to access metadata fields. The following processor uses a Painless script to set an incoming document’s_index.
POST _ingest/pipeline/_simulate{ "pipeline": { "processors": [ { "script": { "description": "Set index based on `lang` field and `dataset` param", "lang": "painless", "source": """ ctx['_index'] = ctx['lang'] + '-' + params['dataset']; """, "params": { "dataset": "catalog" } } } ] }, "docs": [ { "_index": "generic-index", "_source": { "lang": "fr" } } ]}
The processor changes the document’s_index tofr-catalog fromgeneric-index.
{ "docs": [ { "doc": { ... "_index": "fr-catalog", "_source": { "lang": "fr" } } } ]}