Script processor

The script processor executes inline and stored scripts that can modify or transform data in an OpenSearch document during the ingestion process. The processor uses script caching for improved performance because scripts may be recompiled per document. Refer to Script APIs for information about working with scripts in OpenSearch.

The following is the syntax for the script processor:

  1. {
  2. "processor": {
  3. "script": {
  4. "source": "<script_source>",
  5. "lang": "<script_language>",
  6. "params": {
  7. "<param_name>": "<param_value>"
  8. }
  9. }
  10. }
  11. }

copy

Configuration parameters

The following table lists the required and optional parameters for the script processor.

ParameterRequired/OptionalDescription
sourceOptionalThe Painless script to be executed. Either id or source must be specified—but not both. If source is specified, then the script is executed using the provided source code.
idOptionalThe ID of a stored script previously created using the Create Stored Script API. Either id or source must be specified, but not both. If id is specified, then the script source is retrieved from the stored script with the specified ID.
langOptionalThe programming language of the script. Default is painless.
paramsOptionalThe parameters that can be passed to the script.
descriptionOptionalA description of the processor’s purpose or configuration.
ifOptionalSpecifies to conditionally execute the processor.
ignore_failureOptionalSpecifies to ignore processor failures. See Handling pipeline failures.
on_failureOptionalSpecifies a list of processors to run if the processor fails during execution. These processors are executed in the order they are specified. See Handling pipeline failures.
tagOptionalAn identifier tag for the processor. Useful for debugging in order to distinguish between processors of the same type.

Using the processor

Follow these steps to use the processor in a pipeline.

Step 1: Create a pipeline

The following query creates a pipeline named my-script-pipeline that uses the script processor to convert the message field to uppercase:

  1. PUT _ingest/pipeline/my-script-pipeline
  2. {
  3. "description": "Example pipeline using the ScriptProcessor",
  4. "processors": [
  5. {
  6. "script": {
  7. "source": "ctx.message = ctx.message.toUpperCase()",
  8. "lang": "painless",
  9. "description": "Convert message field to uppercase"
  10. }
  11. }
  12. ]
  13. }

copy

Step 2 (Optional): Test the pipeline

It is recommended that you test your pipeline before you ingest documents.

To test the pipeline, run the following query:

  1. POST _ingest/pipeline/my-script-pipeline/_simulate
  2. {
  3. "docs": [
  4. {
  5. "_source": {
  6. "message": "hello, world!"
  7. }
  8. }
  9. ]
  10. }

copy

Response

The following example response confirms that the pipeline is working as expected:

  1. {
  2. "docs": [
  3. {
  4. "doc": {
  5. "_index": "_index",
  6. "_id": "_id",
  7. "_source": {
  8. "message": "HELLO, WORLD!"
  9. },
  10. "_ingest": {
  11. "timestamp": "2024-05-30T16:24:23.30265405Z"
  12. }
  13. }
  14. }
  15. ]
  16. }

copy

Step 3: Ingest a document

The following query ingests a document into an index named testindex1:

  1. POST testindex1/_doc?pipeline=my-script-pipeline
  2. {
  3. "message": "hello, world!"
  4. }

copy

Response

The response confirms that the document has been indexed into testindex1 and has indexed all documents with the message field converted to uppercase:

  1. {
  2. "_index": "testindex1",
  3. "_id": "1",
  4. "_version": 1,
  5. "result": "created",
  6. "_shards": {
  7. "total": 2,
  8. "successful": 1,
  9. "failed": 0
  10. },
  11. "_seq_no": 6,
  12. "_primary_term": 2
  13. }

copy

Step 4 (Optional): Retrieve the document

To retrieve the document, run the following query:

  1. GET testindex1/_doc/1

copy