Foreach
Synopsis
Runs a processor on each element of an array or object when the number of elements is unknown.
Schema
foreach:
- field: <ident>
- processor: <processor>
- description: <text>
- if: <script>
- ignore_failure: <boolean>
- ignore_missing: <boolean>
- on_failure: <processor[]>
- on_success: <processor[]>
- tag: <string>
Configuration
Field | Required | Default | Description |
---|---|---|---|
field | Y | - | Field containing the array or object to iterate over |
processor | Y | - | The processor configuration to run on each element |
description | N | - | Explanatory note for documentation |
if | N | - | Conditional expression determining if the processor should run |
ignore_failure | N | false | If true , individual element processing failures are ignored |
ignore_missing | N | false | If true and field does not exist, exits quietly without modifications |
on_failure | N | - | Processors to run if an error occurs during iteration |
on_success | N | - | Processors to run after successful iteration completion |
tag | N | - | Identifier for logging and debugging |
Details
The processor supports both arrays and maps (objects) as input, and can execute any valid processor on each element. It uses the following procedure:
-
it first locates the specified field in the document, and determines if the field contains an array or a map
-
if an array of a map exists, it goes through each element storing the current value in
_ingest._value
, the current key/index in_ingest._key
, executing the specified processor, and capturing any errors for failure handling. -
finally, it cleans up any temporary
_ingest
fields, and runs the success/failure processors as appropriate.
Be careful when modifying the _ingest
fields directly as they are used internally by the processor.
The processor provides several error handling mechanisms:
ignore_failure | Skip individual element failures |
ignore_missing | Skip missing input fields |
on_failure | Execute recovery processors on error |
on_success | Execute follow-up processors on success |
If an error occurs—and neither ignore_failure
nor on_failure
was specified—the processor leaves the already processed elements modified, stops processing the remaining elements, and returns the error.
Examples
Arrays
Process each array element... |
|
and mark each as read: |
|
Maps
Process each key-value pair... |
|
converting string values to integers: |
|
Nested
Chain multiple processors for each element... |
|
applying multiple transformations: |
|
Error Handling
Handle processing failures gracefully... |
|
preserving data and adding error info: |
|