Foreach processor

Processes elements in an array of unknown length.

All processors can operate on elements inside an array, but if all elements of an array need to be processed in the same way, defining a processor for each element becomes cumbersome and tricky because it is likely that the number of elements in an array is unknown. For this reason the foreach processor exists. By specifying the field holding array elements and a processor that defines what should happen to each element, array fields can easily be preprocessed.

A processor inside the foreach processor works in the array element context and puts that in the ingest metadata under the _ingest._value key. If the array element is a json object it holds all immediate fields of that json object. and if the nested object is a value is _ingest._value just holds that value. Note that if a processor prior to the foreach processor used _ingest._value key then the specified value will not be available to the processor inside the foreach processor. The foreach processor does restore the original value, so that value is available to processors after the foreach processor.

Note that any other field from the document are accessible and modifiable like with all other processors. This processor just puts the current array element being read into _ingest._value ingest metadata attribute, so that it may be pre-processed.

If the foreach processor fails to process an element inside the array, and no on_failure processor has been specified, then it aborts the execution and leaves the array unmodified.

Table 17. Foreach Options

NameRequiredDefaultDescription

field

yes

-

The array field

processor

yes

-

The processor to execute against each field

ignore_missing

no

false

If true and field does not exist or is null, the processor quietly exits without modifying the document

if

no

-

Conditionally execute this processor.

on_failure

no

-

Handle failures for this processor. See Handling Failures in Pipelines.

ignore_failure

no

false

Ignore failures for this processor. See Handling Failures in Pipelines.

tag

no

-

An identifier for this processor. Useful for debugging and metrics.

Assume the following document:

  1. {
  2. "values" : ["foo", "bar", "baz"]
  3. }

When this foreach processor operates on this sample document:

  1. {
  2. "foreach" : {
  3. "field" : "values",
  4. "processor" : {
  5. "uppercase" : {
  6. "field" : "_ingest._value"
  7. }
  8. }
  9. }
  10. }

Then the document will look like this after preprocessing:

  1. {
  2. "values" : ["FOO", "BAR", "BAZ"]
  3. }

Let’s take a look at another example:

  1. {
  2. "persons" : [
  3. {
  4. "id" : "1",
  5. "name" : "John Doe"
  6. },
  7. {
  8. "id" : "2",
  9. "name" : "Jane Doe"
  10. }
  11. ]
  12. }

In this case, the id field needs to be removed, so the following foreach processor is used:

  1. {
  2. "foreach" : {
  3. "field" : "persons",
  4. "processor" : {
  5. "remove" : {
  6. "field" : "_ingest._value.id"
  7. }
  8. }
  9. }
  10. }

After preprocessing the result is:

  1. {
  2. "persons" : [
  3. {
  4. "name" : "John Doe"
  5. },
  6. {
  7. "name" : "Jane Doe"
  8. }
  9. ]
  10. }

The wrapped processor can have a on_failure definition. For example, the id field may not exist on all person objects. Instead of failing the index request, you can use an on_failure block to send the document to the failure_index index for later inspection:

  1. {
  2. "foreach" : {
  3. "field" : "persons",
  4. "processor" : {
  5. "remove" : {
  6. "field" : "_value.id",
  7. "on_failure" : [
  8. {
  9. "set" : {
  10. "field": "_index",
  11. "value": "failure_index"
  12. }
  13. }
  14. ]
  15. }
  16. }
  17. }
  18. }

In this example, if the remove processor does fail, then the array elements that have been processed thus far will be updated.

Another advanced example can be found in the attachment processor documentation.