ingest-attachment.asciidoc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. | `indexed_chars_field` | no | `null` | Field name from which you can overwrite the number of chars being used for extraction. See `indexed_chars`.
  23. | `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`
  24. | `ignore_missing` | no | `false` | If `true` and `field` does not exist, the processor quietly exits without modifying the document
  25. |======
  26. For example, this:
  27. [source,js]
  28. --------------------------------------------------
  29. PUT _ingest/pipeline/attachment
  30. {
  31. "description" : "Extract attachment information",
  32. "processors" : [
  33. {
  34. "attachment" : {
  35. "field" : "data"
  36. }
  37. }
  38. ]
  39. }
  40. PUT my_index/_doc/my_id?pipeline=attachment
  41. {
  42. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
  43. }
  44. GET my_index/_doc/my_id
  45. --------------------------------------------------
  46. // CONSOLE
  47. Returns this:
  48. [source,js]
  49. --------------------------------------------------
  50. {
  51. "found": true,
  52. "_index": "my_index",
  53. "_type": "_doc",
  54. "_id": "my_id",
  55. "_version": 1,
  56. "_source": {
  57. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
  58. "attachment": {
  59. "content_type": "application/rtf",
  60. "language": "ro",
  61. "content": "Lorem ipsum dolor sit amet",
  62. "content_length": 28
  63. }
  64. }
  65. }
  66. --------------------------------------------------
  67. // TESTRESPONSE
  68. To specify only some fields to be extracted:
  69. [source,js]
  70. --------------------------------------------------
  71. PUT _ingest/pipeline/attachment
  72. {
  73. "description" : "Extract attachment information",
  74. "processors" : [
  75. {
  76. "attachment" : {
  77. "field" : "data",
  78. "properties": [ "content", "title" ]
  79. }
  80. }
  81. ]
  82. }
  83. --------------------------------------------------
  84. // CONSOLE
  85. NOTE: Extracting contents from binary data is a resource intensive operation and
  86. consumes a lot of resources. It is highly recommended to run pipelines
  87. using this processor in a dedicated ingest node.
  88. [[ingest-attachment-extracted-chars]]
  89. ==== Limit the number of extracted chars
  90. To prevent extracting too many chars and overload the node memory, the number of chars being used for extraction
  91. is limited by default to `100000`. You can change this value by setting `indexed_chars`. Use `-1` for no limit but
  92. ensure when setting this that your node will have enough HEAP to extract the content of very big documents.
  93. You can also define this limit per document by extracting from a given field the limit to set. If the document
  94. has that field, it will overwrite the `indexed_chars` setting. To set this field, define the `indexed_chars_field`
  95. setting.
  96. For example:
  97. [source,js]
  98. --------------------------------------------------
  99. PUT _ingest/pipeline/attachment
  100. {
  101. "description" : "Extract attachment information",
  102. "processors" : [
  103. {
  104. "attachment" : {
  105. "field" : "data",
  106. "indexed_chars" : 11,
  107. "indexed_chars_field" : "max_size"
  108. }
  109. }
  110. ]
  111. }
  112. PUT my_index/_doc/my_id?pipeline=attachment
  113. {
  114. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
  115. }
  116. GET my_index/_doc/my_id
  117. --------------------------------------------------
  118. // CONSOLE
  119. Returns this:
  120. [source,js]
  121. --------------------------------------------------
  122. {
  123. "found": true,
  124. "_index": "my_index",
  125. "_type": "_doc",
  126. "_id": "my_id",
  127. "_version": 1,
  128. "_source": {
  129. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
  130. "attachment": {
  131. "content_type": "application/rtf",
  132. "language": "sl",
  133. "content": "Lorem ipsum",
  134. "content_length": 11
  135. }
  136. }
  137. }
  138. --------------------------------------------------
  139. // TESTRESPONSE
  140. [source,js]
  141. --------------------------------------------------
  142. PUT _ingest/pipeline/attachment
  143. {
  144. "description" : "Extract attachment information",
  145. "processors" : [
  146. {
  147. "attachment" : {
  148. "field" : "data",
  149. "indexed_chars" : 11,
  150. "indexed_chars_field" : "max_size"
  151. }
  152. }
  153. ]
  154. }
  155. PUT my_index/_doc/my_id_2?pipeline=attachment
  156. {
  157. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
  158. "max_size": 5
  159. }
  160. GET my_index/_doc/my_id_2
  161. --------------------------------------------------
  162. // CONSOLE
  163. Returns this:
  164. [source,js]
  165. --------------------------------------------------
  166. {
  167. "found": true,
  168. "_index": "my_index",
  169. "_type": "_doc",
  170. "_id": "my_id_2",
  171. "_version": 1,
  172. "_source": {
  173. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
  174. "max_size": 5,
  175. "attachment": {
  176. "content_type": "application/rtf",
  177. "language": "ro",
  178. "content": "Lorem",
  179. "content_length": 5
  180. }
  181. }
  182. }
  183. --------------------------------------------------
  184. // TESTRESPONSE
  185. [[ingest-attachment-with-arrays]]
  186. ==== Using the Attachment Processor with arrays
  187. To use the attachment processor within an array of attachments the
  188. {ref}/foreach-processor.html[foreach processor] is required. This
  189. enables the attachment processor to be run on the individual elements
  190. of the array.
  191. For example, given the following source:
  192. [source,js]
  193. --------------------------------------------------
  194. {
  195. "attachments" : [
  196. {
  197. "filename" : "ipsum.txt",
  198. "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
  199. },
  200. {
  201. "filename" : "test.txt",
  202. "data" : "VGhpcyBpcyBhIHRlc3QK"
  203. }
  204. ]
  205. }
  206. --------------------------------------------------
  207. // NOTCONSOLE
  208. In this case, we want to process the data field in each element
  209. of the attachments field and insert
  210. the properties into the document so the following `foreach`
  211. processor is used:
  212. [source,js]
  213. --------------------------------------------------
  214. PUT _ingest/pipeline/attachment
  215. {
  216. "description" : "Extract attachment information from arrays",
  217. "processors" : [
  218. {
  219. "foreach": {
  220. "field": "attachments",
  221. "processor": {
  222. "attachment": {
  223. "target_field": "_ingest._value.attachment",
  224. "field": "_ingest._value.data"
  225. }
  226. }
  227. }
  228. }
  229. ]
  230. }
  231. PUT my_index/_doc/my_id?pipeline=attachment
  232. {
  233. "attachments" : [
  234. {
  235. "filename" : "ipsum.txt",
  236. "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
  237. },
  238. {
  239. "filename" : "test.txt",
  240. "data" : "VGhpcyBpcyBhIHRlc3QK"
  241. }
  242. ]
  243. }
  244. GET my_index/_doc/my_id
  245. --------------------------------------------------
  246. // CONSOLE
  247. Returns this:
  248. [source,js]
  249. --------------------------------------------------
  250. {
  251. "_index" : "my_index",
  252. "_type" : "_doc",
  253. "_id" : "my_id",
  254. "_version" : 1,
  255. "found" : true,
  256. "_source" : {
  257. "attachments" : [
  258. {
  259. "filename" : "ipsum.txt",
  260. "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo=",
  261. "attachment" : {
  262. "content_type" : "text/plain; charset=ISO-8859-1",
  263. "language" : "en",
  264. "content" : "this is\njust some text",
  265. "content_length" : 24
  266. }
  267. },
  268. {
  269. "filename" : "test.txt",
  270. "data" : "VGhpcyBpcyBhIHRlc3QK",
  271. "attachment" : {
  272. "content_type" : "text/plain; charset=ISO-8859-1",
  273. "language" : "en",
  274. "content" : "This is a test",
  275. "content_length" : 16
  276. }
  277. }
  278. ]
  279. }
  280. }
  281. --------------------------------------------------
  282. // TESTRESPONSE
  283. Note that the `target_field` needs to be set, otherwise the
  284. default value is used which is a top level field `attachment`. The
  285. properties on this top level field will contain the value of the
  286. first attachment only. However, by specifying the
  287. `target_field` on to a value on `_ingest._value` it will correctly
  288. associate the properties with the correct attachment.