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