Foreach processor

The foreach processor is used to iterate over a list of values in an input document and apply a transformation to each value. This can be useful for tasks like processing all the elements in an array consistently, such as converting all elements in a string to lowercase or uppercase.

The following is the syntax for the foreach processor:

  1. {
  2. "foreach": {
  3. "field": "<field_name>",
  4. "processor": {
  5. "<processor_type>": {
  6. "<processor_config>": "<processor_value>"
  7. }
  8. }
  9. }
  10. }

copy

Configuration parameters

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

ParameterRequired/OptionalDescription
fieldRequiredThe array field to iterate over.
processorRequiredThe processor to execute against each field.
ignore_missingOptionalIf true and the specified field does not exist or is null, then the processor will quietly exit without modifying the document.
descriptionOptionalA brief description of the processor.
ifOptionalA condition for running the processor.
ignore_failureOptionalSpecifies whether the processor continues execution even if it encounters an error. If set to true, then failures are ignored. Default is false.
on_failureOptionalA list of processors to run if the processor fails.
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 test-foreach that uses the foreach processor to iterate over each element in the protocols field:

  1. PUT _ingest/pipeline/test-foreach
  2. {
  3. "description": "Lowercase all the elements in an array",
  4. "processors": [
  5. {
  6. "foreach": {
  7. "field": "protocols",
  8. "processor": {
  9. "lowercase": {
  10. "field": "_ingest._value"
  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/test-foreach/_simulate
  2. {
  3. "docs": [
  4. {
  5. "_index": "testindex1",
  6. "_id": "1",
  7. "_source": {
  8. "protocols": ["HTTP","HTTPS","TCP","UDP"]
  9. }
  10. }
  11. ]
  12. }

copy

Response

The following example response confirms that the pipeline is working as expected, showing that the four elements have been lowercased:

  1. {
  2. "docs": [
  3. {
  4. "doc": {
  5. "_index": "testindex1",
  6. "_id": "1",
  7. "_source": {
  8. "protocols": [
  9. "http",
  10. "https",
  11. "tcp",
  12. "udp"
  13. ]
  14. },
  15. "_ingest": {
  16. "_value": null,
  17. "timestamp": "2024-05-23T02:44:10.8201Z"
  18. }
  19. }
  20. }
  21. ]
  22. }

copy

Step 3: Ingest a document

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

  1. POST testindex1/_doc/1?pipeline=test-foreach
  2. {
  3. "protocols": ["HTTP","HTTPS","TCP","UDP"]
  4. }

copy

Response

The request indexes the document into the index testindex1 and applies the pipeline before indexing:

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

copy

Step 4 (Optional): Retrieve the document

To retrieve the document, run the following query:

  1. GET testindex1/_doc/1

copy

Response

The response shows the document with the extracted JSON data from the users field:

  1. {
  2. "_index": "testindex1",
  3. "_id": "1",
  4. "_version": 6,
  5. "_seq_no": 5,
  6. "_primary_term": 67,
  7. "found": true,
  8. "_source": {
  9. "protocols": [
  10. "http",
  11. "https",
  12. "tcp",
  13. "udp"
  14. ]
  15. }
  16. }
  17. {
  18. "docs": [
  19. {
  20. "doc": {
  21. "_index": "testindex1",
  22. "_id": "1",
  23. "_source": {
  24. "protocols": [
  25. "http",
  26. "https",
  27. "tcp",
  28. "udp"
  29. ]
  30. },
  31. "_ingest": {
  32. "_value": null,
  33. "timestamp": "2024-05-23T02:44:10.8201Z"
  34. }
  35. }
  36. }
  37. ]
  38. }

copy