foreach.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. [[foreach-processor]]
  2. === Foreach processor
  3. ++++
  4. <titleabbrev>Foreach</titleabbrev>
  5. ++++
  6. Processes elements in an array of unknown length.
  7. All processors can operate on elements inside an array, but if all elements of an array need to
  8. be processed in the same way, defining a processor for each element becomes cumbersome and tricky
  9. because it is likely that the number of elements in an array is unknown. For this reason the `foreach`
  10. processor exists. By specifying the field holding array elements and a processor that
  11. defines what should happen to each element, array fields can easily be preprocessed.
  12. A processor inside the foreach processor works in the array element context and puts that in the ingest metadata
  13. under the `_ingest._value` key. If the array element is a json object it holds all immediate fields of that json object.
  14. and if the nested object is a value is `_ingest._value` just holds that value. Note that if a processor prior to the
  15. `foreach` processor used `_ingest._value` key then the specified value will not be available to the processor inside
  16. the `foreach` processor. The `foreach` processor does restore the original value, so that value is available to processors
  17. after the `foreach` processor.
  18. Note that any other field from the document are accessible and modifiable like with all other processors. This processor
  19. just puts the current array element being read into `_ingest._value` ingest metadata attribute, so that it may be
  20. pre-processed.
  21. If the `foreach` processor fails to process an element inside the array, and no `on_failure` processor has been specified,
  22. then it aborts the execution and leaves the array unmodified.
  23. [[foreach-options]]
  24. .Foreach Options
  25. [options="header"]
  26. |======
  27. | Name | Required | Default | Description
  28. | `field` | yes | - | The array field
  29. | `processor` | yes | - | The processor to execute against each field
  30. | `ignore_missing` | no | false | If `true` and `field` does not exist or is `null`, the processor quietly exits without modifying the document
  31. include::common-options.asciidoc[]
  32. |======
  33. Assume the following document:
  34. [source,js]
  35. --------------------------------------------------
  36. {
  37. "values" : ["foo", "bar", "baz"]
  38. }
  39. --------------------------------------------------
  40. // NOTCONSOLE
  41. When this `foreach` processor operates on this sample document:
  42. [source,js]
  43. --------------------------------------------------
  44. {
  45. "foreach" : {
  46. "field" : "values",
  47. "processor" : {
  48. "uppercase" : {
  49. "field" : "_ingest._value"
  50. }
  51. }
  52. }
  53. }
  54. --------------------------------------------------
  55. // NOTCONSOLE
  56. Then the document will look like this after preprocessing:
  57. [source,js]
  58. --------------------------------------------------
  59. {
  60. "values" : ["FOO", "BAR", "BAZ"]
  61. }
  62. --------------------------------------------------
  63. // NOTCONSOLE
  64. Let's take a look at another example:
  65. [source,js]
  66. --------------------------------------------------
  67. {
  68. "persons" : [
  69. {
  70. "id" : "1",
  71. "name" : "John Doe"
  72. },
  73. {
  74. "id" : "2",
  75. "name" : "Jane Doe"
  76. }
  77. ]
  78. }
  79. --------------------------------------------------
  80. // NOTCONSOLE
  81. In this case, the `id` field needs to be removed,
  82. so the following `foreach` processor is used:
  83. [source,js]
  84. --------------------------------------------------
  85. {
  86. "foreach" : {
  87. "field" : "persons",
  88. "processor" : {
  89. "remove" : {
  90. "field" : "_ingest._value.id"
  91. }
  92. }
  93. }
  94. }
  95. --------------------------------------------------
  96. // NOTCONSOLE
  97. After preprocessing the result is:
  98. [source,js]
  99. --------------------------------------------------
  100. {
  101. "persons" : [
  102. {
  103. "name" : "John Doe"
  104. },
  105. {
  106. "name" : "Jane Doe"
  107. }
  108. ]
  109. }
  110. --------------------------------------------------
  111. // NOTCONSOLE
  112. The wrapped processor can have a `on_failure` definition.
  113. For example, the `id` field may not exist on all person objects.
  114. Instead of failing the index request, you can use an `on_failure`
  115. block to send the document to the 'failure_index' index for later inspection:
  116. [source,js]
  117. --------------------------------------------------
  118. {
  119. "foreach" : {
  120. "field" : "persons",
  121. "processor" : {
  122. "remove" : {
  123. "field" : "_value.id",
  124. "on_failure" : [
  125. {
  126. "set" : {
  127. "field": "_index",
  128. "value": "failure_index"
  129. }
  130. }
  131. ]
  132. }
  133. }
  134. }
  135. }
  136. --------------------------------------------------
  137. // NOTCONSOLE
  138. In this example, if the `remove` processor does fail, then
  139. the array elements that have been processed thus far will
  140. be updated.
  141. Another advanced example can be found in the {plugins}/ingest-attachment-with-arrays.html[attachment processor documentation].