ingest-attachment.asciidoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. [[ingest-attachment]]
  2. === Ingest Attachment Processor Plugin
  3. The ingest attachment plugin lets Elasticsearch extract file attachments in common formats (such as PPT, XLS, and PDF) by
  4. using the Apache text extraction library http://lucene.apache.org/tika/[Tika].
  5. You can use the ingest attachment plugin as a replacement for the mapper attachment plugin.
  6. The source field must be a base64 encoded binary. If you do not want to incur
  7. the overhead of converting back and forth between base64, you can use the CBOR
  8. format instead of JSON and specify the field as a bytes array instead of a string
  9. representation. The processor will skip the base64 decoding then.
  10. :plugin_name: ingest-attachment
  11. include::install_remove.asciidoc[]
  12. [[using-ingest-attachment]]
  13. ==== Using the Attachment Processor in a Pipeline
  14. [[ingest-attachment-options]]
  15. .Attachment options
  16. [options="header"]
  17. |======
  18. | Name | Required | Default | Description
  19. | `field` | yes | - | The field to get the base64 encoded field from
  20. | `target_field` | no | attachment | The field that will hold the attachment information
  21. | `indexed_chars` | no | 100000 | The number of chars being used for extraction to prevent huge fields. Use `-1` for no limit.
  22. | `properties` | no | all properties | Array of properties to select to be stored. Can be `content`, `title`, `name`, `author`, `keywords`, `date`, `content_type`, `content_length`, `language`
  23. | `ignore_missing` | no | `false` | If `true` and `field` does not exist, the processor quietly exits without modifying the document
  24. |======
  25. For example, this:
  26. [source,js]
  27. --------------------------------------------------
  28. PUT _ingest/pipeline/attachment
  29. {
  30. "description" : "Extract attachment information",
  31. "processors" : [
  32. {
  33. "attachment" : {
  34. "field" : "data"
  35. }
  36. }
  37. ]
  38. }
  39. PUT my_index/my_type/my_id?pipeline=attachment
  40. {
  41. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
  42. }
  43. GET my_index/my_type/my_id
  44. --------------------------------------------------
  45. // CONSOLE
  46. Returns this:
  47. [source,js]
  48. --------------------------------------------------
  49. {
  50. "found": true,
  51. "_index": "my_index",
  52. "_type": "my_type",
  53. "_id": "my_id",
  54. "_version": 1,
  55. "_source": {
  56. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
  57. "attachment": {
  58. "content_type": "application/rtf",
  59. "language": "ro",
  60. "content": "Lorem ipsum dolor sit amet",
  61. "content_length": 28
  62. }
  63. }
  64. }
  65. --------------------------------------------------
  66. // TESTRESPONSE
  67. To specify only some fields to be extracted:
  68. [source,js]
  69. --------------------------------------------------
  70. PUT _ingest/pipeline/attachment
  71. {
  72. "description" : "Extract attachment information",
  73. "processors" : [
  74. {
  75. "attachment" : {
  76. "field" : "data",
  77. "properties": [ "content", "title" ]
  78. }
  79. }
  80. ]
  81. }
  82. --------------------------------------------------
  83. // CONSOLE
  84. NOTE: Extracting contents from binary data is a resource intensive operation and
  85. consumes a lot of resources. It is highly recommended to run pipelines
  86. using this processor in a dedicated ingest node.
  87. [[ingest-attachment-with-arrays]]
  88. ==== Using the Attachment Processor with arrays
  89. To use the attachment processor within an array of attachments the
  90. {ref}/foreach-processor.html[foreach processor] is required. This
  91. enables the attachment processor to be run on the individual elements
  92. of the array.
  93. For example, given the following source:
  94. [source,js]
  95. --------------------------------------------------
  96. {
  97. "attachments" : [
  98. {
  99. "filename" : "ipsum.txt",
  100. "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
  101. },
  102. {
  103. "filename" : "test.txt",
  104. "data" : "VGhpcyBpcyBhIHRlc3QK"
  105. }
  106. ]
  107. }
  108. --------------------------------------------------
  109. // NOTCONSOLE
  110. In this case, we want to process the data field in each element
  111. of the attachments field and insert
  112. the properties into the document so the following `foreach`
  113. processor is used:
  114. [source,js]
  115. --------------------------------------------------
  116. PUT _ingest/pipeline/attachment
  117. {
  118. "description" : "Extract attachment information from arrays",
  119. "processors" : [
  120. {
  121. "foreach": {
  122. "field": "attachments",
  123. "processor": {
  124. "attachment": {
  125. "target_field": "_ingest._value.attachment",
  126. "field": "_ingest._value.data"
  127. }
  128. }
  129. }
  130. }
  131. ]
  132. }
  133. PUT my_index/my_type/my_id?pipeline=attachment
  134. {
  135. "attachments" : [
  136. {
  137. "filename" : "ipsum.txt",
  138. "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
  139. },
  140. {
  141. "filename" : "test.txt",
  142. "data" : "VGhpcyBpcyBhIHRlc3QK"
  143. }
  144. ]
  145. }
  146. GET my_index/my_type/my_id
  147. --------------------------------------------------
  148. // CONSOLE
  149. Returns this:
  150. [source,js]
  151. --------------------------------------------------
  152. {
  153. "_index" : "my_index",
  154. "_type" : "my_type",
  155. "_id" : "my_id",
  156. "_version" : 1,
  157. "found" : true,
  158. "_source" : {
  159. "attachments" : [
  160. {
  161. "filename" : "ipsum.txt",
  162. "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo=",
  163. "attachment" : {
  164. "content_type" : "text/plain; charset=ISO-8859-1",
  165. "language" : "en",
  166. "content" : "this is\njust some text",
  167. "content_length" : 24
  168. }
  169. },
  170. {
  171. "filename" : "test.txt",
  172. "data" : "VGhpcyBpcyBhIHRlc3QK",
  173. "attachment" : {
  174. "content_type" : "text/plain; charset=ISO-8859-1",
  175. "language" : "en",
  176. "content" : "This is a test",
  177. "content_length" : 16
  178. }
  179. }
  180. ]
  181. }
  182. }
  183. --------------------------------------------------
  184. // TESTRESPONSE
  185. Note that the `target_field` needs to be set, otherwise the
  186. default value is used which is a top level field `attachment`. The
  187. properties on this top level field will contain the value of the
  188. first attachment only. However, by specifying the
  189. `target_field` on to a value on `_ingest._value` it will correctly
  190. associate the properties with the correct attachment.