set.asciidoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. [[set-processor]]
  2. === Set processor
  3. ++++
  4. <titleabbrev>Set</titleabbrev>
  5. ++++
  6. Sets one field and associates it with the specified value. If the field already exists,
  7. its value will be replaced with the provided one.
  8. [[set-options]]
  9. .Set Options
  10. [options="header"]
  11. |======
  12. | Name | Required | Default | Description
  13. | `field` | yes | - | The field to insert, upsert, or update. Supports <<template-snippets,template snippets>>.
  14. | `value` | yes* | - | The value to be set for the field. Supports <<template-snippets,template snippets>>. May specify only one of `value` or `copy_from`.
  15. | `copy_from` | no | - | The origin field which will be copied to `field`, cannot set `value` simultaneously. Supported data types are `boolean`, `number`, `array`, `object`, `string`, `date`, etc.
  16. | `override` | no | `true` | If processor will update fields with pre-existing non-null-valued field. When set to `false`, such fields will not be touched.
  17. | `ignore_empty_value` | no | `false` | If `true` and `value` is a <<template-snippets,template snippet>> that evaluates to `null` or the empty string, the processor quietly exits without modifying the document
  18. | `media_type` | no | `application/json` | The media type for encoding `value`. Applies only when `value` is a <<template-snippets,template snippet>>. Must be one of `application/json`, `text/plain`, or `application/x-www-form-urlencoded`.
  19. include::common-options.asciidoc[]
  20. |======
  21. [source,js]
  22. --------------------------------------------------
  23. {
  24. "description" : "sets the value of count to 1",
  25. "set": {
  26. "field": "count",
  27. "value": 1
  28. }
  29. }
  30. --------------------------------------------------
  31. // NOTCONSOLE
  32. This processor can also be used to copy data from one field to another. For example:
  33. [source,console]
  34. --------------------------------------------------
  35. PUT _ingest/pipeline/set_os
  36. {
  37. "description": "sets the value of host.os.name from the field os",
  38. "processors": [
  39. {
  40. "set": {
  41. "field": "host.os.name",
  42. "value": "{{{os}}}"
  43. }
  44. }
  45. ]
  46. }
  47. POST _ingest/pipeline/set_os/_simulate
  48. {
  49. "docs": [
  50. {
  51. "_source": {
  52. "os": "Ubuntu"
  53. }
  54. }
  55. ]
  56. }
  57. --------------------------------------------------
  58. Result:
  59. [source,console-result]
  60. --------------------------------------------------
  61. {
  62. "docs" : [
  63. {
  64. "doc" : {
  65. "_index" : "_index",
  66. "_id" : "_id",
  67. "_source" : {
  68. "host" : {
  69. "os" : {
  70. "name" : "Ubuntu"
  71. }
  72. },
  73. "os" : "Ubuntu"
  74. },
  75. "_ingest" : {
  76. "timestamp" : "2019-03-11T21:54:37.909224Z"
  77. }
  78. }
  79. }
  80. ]
  81. }
  82. --------------------------------------------------
  83. // TESTRESPONSE[s/2019-03-11T21:54:37.909224Z/$body.docs.0.doc._ingest.timestamp/]
  84. The contents of a field including complex values such as arrays and objects can be copied to another field using `copy_from`:
  85. [source,console]
  86. --------------------------------------------------
  87. PUT _ingest/pipeline/set_bar
  88. {
  89. "description": "sets the value of bar from the field foo",
  90. "processors": [
  91. {
  92. "set": {
  93. "field": "bar",
  94. "copy_from": "foo"
  95. }
  96. }
  97. ]
  98. }
  99. POST _ingest/pipeline/set_bar/_simulate
  100. {
  101. "docs": [
  102. {
  103. "_source": {
  104. "foo": ["foo1", "foo2"]
  105. }
  106. }
  107. ]
  108. }
  109. --------------------------------------------------
  110. Result:
  111. [source,console-result]
  112. --------------------------------------------------
  113. {
  114. "docs" : [
  115. {
  116. "doc" : {
  117. "_index" : "_index",
  118. "_id" : "_id",
  119. "_source" : {
  120. "bar": ["foo1", "foo2"],
  121. "foo": ["foo1", "foo2"]
  122. },
  123. "_ingest" : {
  124. "timestamp" : "2020-09-30T12:55:17.742795Z"
  125. }
  126. }
  127. }
  128. ]
  129. }
  130. --------------------------------------------------
  131. // TESTRESPONSE[s/2020-09-30T12:55:17.742795Z/$body.docs.0.doc._ingest.timestamp/]