ingest-attachment.asciidoc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. [[ingest-attachment-install]]
  11. [float]
  12. ==== Installation
  13. This plugin can be installed using the plugin manager:
  14. [source,sh]
  15. ----------------------------------------------------------------
  16. sudo bin/elasticsearch-plugin install ingest-attachment
  17. ----------------------------------------------------------------
  18. The plugin must be installed on every node in the cluster, and each node must
  19. be restarted after installation.
  20. This plugin can be downloaded for <<plugin-management-custom-url,offline install>> from
  21. {plugin_url}/ingest-attachment/ingest-attachment-{version}.zip.
  22. [[ingest-attachment-remove]]
  23. [float]
  24. ==== Removal
  25. The plugin can be removed with the following command:
  26. [source,sh]
  27. ----------------------------------------------------------------
  28. sudo bin/elasticsearch-plugin remove ingest-attachment
  29. ----------------------------------------------------------------
  30. The node must be stopped before removing the plugin.
  31. [[using-ingest-attachment]]
  32. ==== Using the Attachment Processor in a Pipeline
  33. [[ingest-attachment-options]]
  34. .Attachment options
  35. [options="header"]
  36. |======
  37. | Name | Required | Default | Description
  38. | `field` | yes | - | The field to get the base64 encoded field from
  39. | `target_field` | no | attachment | The field that will hold the attachment information
  40. | `indexed_chars` | no | 100000 | The number of chars being used for extraction to prevent huge fields. Use `-1` for no limit.
  41. | `properties` | no | all | Properties to select to be stored. Can be `content`, `title`, `name`, `author`, `keywords`, `date`, `content_type`, `content_length`, `language`
  42. | `ignore_missing` | no | `false` | If `true` and `field` does not exist, the processor quietly exits without modifying the document
  43. |======
  44. For example, this:
  45. [source,js]
  46. --------------------------------------------------
  47. PUT _ingest/pipeline/attachment
  48. {
  49. "description" : "Extract attachment information",
  50. "processors" : [
  51. {
  52. "attachment" : {
  53. "field" : "data"
  54. }
  55. }
  56. ]
  57. }
  58. PUT my_index/my_type/my_id?pipeline=attachment
  59. {
  60. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
  61. }
  62. GET my_index/my_type/my_id
  63. --------------------------------------------------
  64. // CONSOLE
  65. Returns this:
  66. [source,js]
  67. --------------------------------------------------
  68. {
  69. "found": true,
  70. "_index": "my_index",
  71. "_type": "my_type",
  72. "_id": "my_id",
  73. "_version": 1,
  74. "_source": {
  75. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
  76. "attachment": {
  77. "content_type": "application/rtf",
  78. "language": "ro",
  79. "content": "Lorem ipsum dolor sit amet",
  80. "content_length": 28
  81. }
  82. }
  83. }
  84. --------------------------------------------------
  85. // TESTRESPONSE
  86. NOTE: Extracting contents from binary data is a resource intensive operation and
  87. consumes a lot of resources. It is highly recommended to run pipelines
  88. using this processor in a dedicated ingest node.
  89. [[ingest-attachment-with-arrays]]
  90. ==== Using the Attachment Processor with arrays
  91. To use the attachment processor within an array of attachments the
  92. {ref}/foreach-processor.html[foreach processor] is required. This
  93. enables the attachment processor to be run on the individual elements
  94. of the array.
  95. For example, given the following source:
  96. [source,js]
  97. --------------------------------------------------
  98. {
  99. "attachments" : [
  100. {
  101. "filename" : "ipsum.txt",
  102. "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
  103. },
  104. {
  105. "filename" : "test.txt",
  106. "data" : "VGhpcyBpcyBhIHRlc3QK"
  107. }
  108. ]
  109. }
  110. --------------------------------------------------
  111. // NOTCONSOLE
  112. In this case, we want to process the data field in each element
  113. of the attachments field and insert
  114. the properties into the document so the following `foreach`
  115. processor is used:
  116. [source,js]
  117. --------------------------------------------------
  118. PUT _ingest/pipeline/attachment
  119. {
  120. "description" : "Extract attachment information from arrays",
  121. "processors" : [
  122. {
  123. "foreach": {
  124. "field": "attachments",
  125. "processor": {
  126. "attachment": {
  127. "target_field": "_ingest._value.attachment",
  128. "field": "_ingest._value.data"
  129. }
  130. }
  131. }
  132. }
  133. ]
  134. }
  135. PUT my_index/my_type/my_id?pipeline=attachment
  136. {
  137. "attachments" : [
  138. {
  139. "filename" : "ipsum.txt",
  140. "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
  141. },
  142. {
  143. "filename" : "test.txt",
  144. "data" : "VGhpcyBpcyBhIHRlc3QK"
  145. }
  146. ]
  147. }
  148. GET my_index/my_type/my_id
  149. --------------------------------------------------
  150. // CONSOLE
  151. Returns this:
  152. [source,js]
  153. --------------------------------------------------
  154. {
  155. "_index" : "my_index",
  156. "_type" : "my_type",
  157. "_id" : "my_id",
  158. "_version" : 1,
  159. "found" : true,
  160. "_source" : {
  161. "attachments" : [
  162. {
  163. "filename" : "ipsum.txt",
  164. "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo=",
  165. "attachment" : {
  166. "content_type" : "text/plain; charset=ISO-8859-1",
  167. "language" : "en",
  168. "content" : "this is\njust some text",
  169. "content_length" : 24
  170. }
  171. },
  172. {
  173. "filename" : "test.txt",
  174. "data" : "VGhpcyBpcyBhIHRlc3QK",
  175. "attachment" : {
  176. "content_type" : "text/plain; charset=ISO-8859-1",
  177. "language" : "en",
  178. "content" : "This is a test",
  179. "content_length" : 16
  180. }
  181. }
  182. ]
  183. }
  184. }
  185. --------------------------------------------------
  186. // TESTRESPONSE
  187. Note that the `target_field` needs to be set, otherwise the
  188. default value is used which is a top level field `attachment`. The
  189. properties on this top level field will contain the value of the
  190. first attachment only. However, by specifying the
  191. `target_field` on to a value on `_ingest._value` it will correctly
  192. associate the properties with the correct attachment.