ingest-attachment.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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-cbor]]
  88. ==== Use the attachment processor with CBOR
  89. To avoid encoding and decoding JSON to base64, you can instead pass CBOR data to
  90. the attachment processor. For example, the following request creates the
  91. `cbor-attachment` pipeline, which uses the attachment processor.
  92. [source,console]
  93. ----
  94. PUT _ingest/pipeline/cbor-attachment
  95. {
  96. "description" : "Extract attachment information",
  97. "processors" : [
  98. {
  99. "attachment" : {
  100. "field" : "data"
  101. }
  102. }
  103. ]
  104. }
  105. ----
  106. The following Python script passes CBOR data to an HTTP indexing request that
  107. includes the `cbor-attachment` pipeline. The HTTP request headers use a
  108. a `content-type` of `application/cbor`.
  109. NOTE: Not all {es} clients support custom HTTP request headers.
  110. [source,python]
  111. ----
  112. import cbor2
  113. import requests
  114. file = 'my-file'
  115. headers = {'content-type': 'application/cbor'}
  116. with open(file, 'rb') as f:
  117. doc = {
  118. 'data': f.read()
  119. }
  120. requests.put(
  121. 'http://localhost:9200/my-index-000001/_doc/my_id?pipeline=cbor-attachment',
  122. data=cbor2.dumps(doc),
  123. headers=headers
  124. )
  125. ----
  126. [[ingest-attachment-extracted-chars]]
  127. ==== Limit the number of extracted chars
  128. To prevent extracting too many chars and overload the node memory, the number of chars being used for extraction
  129. is limited by default to `100000`. You can change this value by setting `indexed_chars`. Use `-1` for no limit but
  130. ensure when setting this that your node will have enough HEAP to extract the content of very big documents.
  131. You can also define this limit per document by extracting from a given field the limit to set. If the document
  132. has that field, it will overwrite the `indexed_chars` setting. To set this field, define the `indexed_chars_field`
  133. setting.
  134. For example:
  135. [source,console]
  136. --------------------------------------------------
  137. PUT _ingest/pipeline/attachment
  138. {
  139. "description" : "Extract attachment information",
  140. "processors" : [
  141. {
  142. "attachment" : {
  143. "field" : "data",
  144. "indexed_chars" : 11,
  145. "indexed_chars_field" : "max_size"
  146. }
  147. }
  148. ]
  149. }
  150. PUT my-index-000001/_doc/my_id?pipeline=attachment
  151. {
  152. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
  153. }
  154. GET my-index-000001/_doc/my_id
  155. --------------------------------------------------
  156. Returns this:
  157. [source,console-result]
  158. --------------------------------------------------
  159. {
  160. "found": true,
  161. "_index": "my-index-000001",
  162. "_id": "my_id",
  163. "_version": 1,
  164. "_seq_no": 35,
  165. "_primary_term": 1,
  166. "_source": {
  167. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
  168. "attachment": {
  169. "content_type": "application/rtf",
  170. "language": "sl",
  171. "content": "Lorem ipsum",
  172. "content_length": 11
  173. }
  174. }
  175. }
  176. --------------------------------------------------
  177. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
  178. [source,console]
  179. --------------------------------------------------
  180. PUT _ingest/pipeline/attachment
  181. {
  182. "description" : "Extract attachment information",
  183. "processors" : [
  184. {
  185. "attachment" : {
  186. "field" : "data",
  187. "indexed_chars" : 11,
  188. "indexed_chars_field" : "max_size"
  189. }
  190. }
  191. ]
  192. }
  193. PUT my-index-000001/_doc/my_id_2?pipeline=attachment
  194. {
  195. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
  196. "max_size": 5
  197. }
  198. GET my-index-000001/_doc/my_id_2
  199. --------------------------------------------------
  200. Returns this:
  201. [source,console-result]
  202. --------------------------------------------------
  203. {
  204. "found": true,
  205. "_index": "my-index-000001",
  206. "_id": "my_id_2",
  207. "_version": 1,
  208. "_seq_no": 40,
  209. "_primary_term": 1,
  210. "_source": {
  211. "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
  212. "max_size": 5,
  213. "attachment": {
  214. "content_type": "application/rtf",
  215. "language": "ro",
  216. "content": "Lorem",
  217. "content_length": 5
  218. }
  219. }
  220. }
  221. --------------------------------------------------
  222. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
  223. [[ingest-attachment-with-arrays]]
  224. ==== Using the Attachment Processor with arrays
  225. To use the attachment processor within an array of attachments the
  226. {ref}/foreach-processor.html[foreach processor] is required. This
  227. enables the attachment processor to be run on the individual elements
  228. of the array.
  229. For example, given the following source:
  230. [source,js]
  231. --------------------------------------------------
  232. {
  233. "attachments" : [
  234. {
  235. "filename" : "ipsum.txt",
  236. "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
  237. },
  238. {
  239. "filename" : "test.txt",
  240. "data" : "VGhpcyBpcyBhIHRlc3QK"
  241. }
  242. ]
  243. }
  244. --------------------------------------------------
  245. // NOTCONSOLE
  246. In this case, we want to process the data field in each element
  247. of the attachments field and insert
  248. the properties into the document so the following `foreach`
  249. processor is used:
  250. [source,console]
  251. --------------------------------------------------
  252. PUT _ingest/pipeline/attachment
  253. {
  254. "description" : "Extract attachment information from arrays",
  255. "processors" : [
  256. {
  257. "foreach": {
  258. "field": "attachments",
  259. "processor": {
  260. "attachment": {
  261. "target_field": "_ingest._value.attachment",
  262. "field": "_ingest._value.data"
  263. }
  264. }
  265. }
  266. }
  267. ]
  268. }
  269. PUT my-index-000001/_doc/my_id?pipeline=attachment
  270. {
  271. "attachments" : [
  272. {
  273. "filename" : "ipsum.txt",
  274. "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
  275. },
  276. {
  277. "filename" : "test.txt",
  278. "data" : "VGhpcyBpcyBhIHRlc3QK"
  279. }
  280. ]
  281. }
  282. GET my-index-000001/_doc/my_id
  283. --------------------------------------------------
  284. Returns this:
  285. [source,console-result]
  286. --------------------------------------------------
  287. {
  288. "_index" : "my-index-000001",
  289. "_id" : "my_id",
  290. "_version" : 1,
  291. "_seq_no" : 50,
  292. "_primary_term" : 1,
  293. "found" : true,
  294. "_source" : {
  295. "attachments" : [
  296. {
  297. "filename" : "ipsum.txt",
  298. "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo=",
  299. "attachment" : {
  300. "content_type" : "text/plain; charset=ISO-8859-1",
  301. "language" : "en",
  302. "content" : "this is\njust some text",
  303. "content_length" : 24
  304. }
  305. },
  306. {
  307. "filename" : "test.txt",
  308. "data" : "VGhpcyBpcyBhIHRlc3QK",
  309. "attachment" : {
  310. "content_type" : "text/plain; charset=ISO-8859-1",
  311. "language" : "en",
  312. "content" : "This is a test",
  313. "content_length" : 16
  314. }
  315. }
  316. ]
  317. }
  318. }
  319. --------------------------------------------------
  320. // TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
  321. Note that the `target_field` needs to be set, otherwise the
  322. default value is used which is a top level field `attachment`. The
  323. properties on this top level field will contain the value of the
  324. first attachment only. However, by specifying the
  325. `target_field` on to a value on `_ingest._value` it will correctly
  326. associate the properties with the correct attachment.