Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOC] Add script processor documentation #5988

Merged
merged 17 commits into from
Jun 6, 2024
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions _ingest-pipelines/processors/script.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
---
layout: default
title: script
parent: Ingest processors
nav_order: 230
---

# `script` processor

Check failure on line 8 in _ingest-pipelines/processors/script.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _ingest-pipelines/processors/script.md#L8

[OpenSearch.HeadingCapitalization] 'processor' is a heading and should be in sentence case.
Raw output
{"message": "[OpenSearch.HeadingCapitalization] 'processor' is a heading and should be in sentence case.", "location": {"path": "_ingest-pipelines/processors/script.md", "range": {"start": {"line": 8, "column": 12}}}, "severity": "ERROR"}

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, as scripts may be recompiled per document. Refer to [Script APIs](https://opensearch.org/docs/latest/api-reference/script-apis/index/) for information about working with scripts in OpenSearch.
vagimeli marked this conversation as resolved.
Show resolved Hide resolved

The following is the syntax for the `script` processor:

```json
{
"processor": {
"script": {
"tag": "<processor_tag>",
vagimeli marked this conversation as resolved.
Show resolved Hide resolved
"description": "<processor_description>",
"source": "<script_source>",
"lang": "<script_language>",
"params": {
"<param_name>": "<param_value>"
}
}
}
}
```
{% include copy-curl.html %}

## Configuration parameters

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

| Parameter | Required/Optional | Description |
|---|---|---|
`source` | Required | The Painless script to be executed.
vagimeli marked this conversation as resolved.
Show resolved Hide resolved
`lang` | Required | The programming language of the script. Default is `painless`.
`params` | Required | The parameters that can be passed to the script.
`description` | Optional | A description of the processor's purpose or configuration.
`if` | Optional | Specifies to conditionally execute the processor.
`ignore_failure` | Optional | Specifies to ignore failures for the processor. See [Handling pipeline failures]({{site.url}}{{site.baseurl}}/ingest-pipelines/pipeline-failures/).
vagimeli marked this conversation as resolved.
Show resolved Hide resolved
`on_failure` | Optional | Specifies a list of processors to run if the processor fails during execution. These processors are executed in the order they are specified.
`tag` | Optional | An 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 <pipeline name> that uses the script processor to <do what?>:

```json
PUT _ingest/pipeline/my-script-pipeline
vagimeli marked this conversation as resolved.
Show resolved Hide resolved
{
"description": "Example pipeline using the ScriptProcessor",
"processors": [
{
"script": {
"source": "ctx.message = ctx.message.toUpperCase()",
"lang": "painless",
"description": "Convert message field to uppercase"
}
}
]
}
```
{% include copy-curl.html %}

### Step 2 (Optional): Test the pipeline

It is recommended that you test your pipeline before you ingest documents.
{: .tip}

To test the pipeline, run the following query:

```json
POST _ingest/pipeline/my-script-pipeline/_simulate
{
"docs": [
{
"_source": {
"message": "hello, world!"
}
}
]
}
```
{% include copy-curl.html %}

#### Response

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

```json
{
"docs": [
{
"doc": {
"_index": "_index",
"_id": "_id",
"_source": {
"message": "HELLO, WORLD!"
},
"_ingest": {
"timestamp": "2024-05-30T16:24:23.30265405Z"
}
}
}
]
}
```
{% include copy-curl.html %}

### Step 3: Ingest a document

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

```json
POST testindex1/_doc?pipeline=my-script-pipeline
{
"message": "hello, world!"
}
```
{% include copy-curl.html %}

#### Response

This repsonse confirms that the document is indexed into `testindex1` and is indexing all documents with the `message` field transformed to uppercase.

Check failure on line 130 in _ingest-pipelines/processors/script.md

View workflow job for this annotation

GitHub Actions / vale

[vale] _ingest-pipelines/processors/script.md#L130

[OpenSearch.Spelling] Error: repsonse. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.
Raw output
{"message": "[OpenSearch.Spelling] Error: repsonse. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.", "location": {"path": "_ingest-pipelines/processors/script.md", "range": {"start": {"line": 130, "column": 6}}}, "severity": "ERROR"}

```json
{
"_index": "testindex1",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 2
}
```
{% include copy-curl.html %}

### Step 4 (Optional): Retrieve the document

To retrieve the document, run the following query:

```json
GET testindex1/_doc/1
```
{% include copy-curl.html %}

<Provide any other information and code examples relevant to the user or use cases.>
Loading