The Custom Script stage in Index Pipeline allows for custom processing. You can write a painless script to perform actions such as adding a new field, deleting a field, setting/resetting values to a field, coping fields, etc.
Ensure to Train your app each time you make changes to any index configuration. This builds the index based on the updated configurations.
Configuration
To configure a custom script, follow the below steps:
- Click the Indices tab on the top.
- On the left pane, under the Index Configuration section, click Workbench.
- On the Workbench (Index Configuration) page, on the Stages column, click the + icon.
- On the right column, select Custom Script from the Stage Type drop-down list.
- Enter a name in the Stage Name field.
- Enter a condition in the Condition field. You can add multiple conditions using the AND/OR connectors. Documents that satisfy the condition will be executed as part of the stage. See below for details.
- Under the </>Painless section, enter the custom script. See below for details.
- Click Simulate to verify the configurations. The simulator displays the Source and the number of documents to which the mapping was applied, and the result. You can change the Source (if not mentioned in the condition) and the number of documents.
- Once done, click Save Configuration on the top-right.
Script
Example
To copy a value from “defaultAnswers[*].payload” to “answer[]”, use the below script:
ctx.answer =[]; for (def item : ctx.defaultAnswers) { ctx.answer.add(item.payload) }
Conditions
Condition is of the following format: ctx.fieldtype==value
or ctx.fieldtype!=value
. The field_name can be obtained from the Fields table under Index Configuration.
For example, ctx.contentType=="web"
to restrict the extraction to the content from a web source.