2016-02-09 08:57:05 -05:00
[[ingest-attachment]]
2016-02-11 20:40:32 -05:00
=== Ingest Attachment Processor Plugin
2016-02-09 08:57:05 -05:00
2016-03-04 01:49:31 -05:00
The ingest attachment plugin lets Elasticsearch extract file attachments in common formats (such as PPT, XLS, and PDF) by
2016-02-09 08:57:05 -05:00
using the Apache text extraction library http://lucene.apache.org/tika/[Tika].
2016-03-04 01:49:31 -05:00
You can use the ingest attachment plugin as a replacement for the mapper attachment plugin.
2016-02-09 08:57:05 -05:00
2016-04-11 08:14:56 -04:00
The source field must be a base64 encoded binary. If you do not want to incur
the overhead of converting back and forth between base64, you can use the CBOR
format instead of JSON and specify the field as a bytes array instead of a string
representation. The processor will skip the base64 decoding then.
2016-02-09 08:57:05 -05:00
2017-04-20 09:01:37 -04:00
:plugin_name: ingest-attachment
include::install_remove.asciidoc[]
2016-06-08 15:55:59 -04:00
[[using-ingest-attachment]]
==== Using the Attachment Processor in a Pipeline
2016-02-09 08:57:05 -05:00
[[ingest-attachment-options]]
.Attachment options
[options="header"]
|======
| Name | Required | Default | Description
2016-04-20 12:00:11 -04:00
| `field` | yes | - | The field to get the base64 encoded field from
2016-02-09 08:57:05 -05:00
| `target_field` | no | attachment | The field that will hold the attachment information
| `indexed_chars` | no | 100000 | The number of chars being used for extraction to prevent huge fields. Use `-1` for no limit.
2018-03-14 14:07:20 -04:00
| `indexed_chars_field` | no | `null` | Field name from which you can overwrite the number of chars being used for extraction. See `indexed_chars`.
2016-12-23 05:28:12 -05:00
| `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`
2016-12-20 13:53:28 -05:00
| `ignore_missing` | no | `false` | If `true` and `field` does not exist, the processor quietly exits without modifying the document
2016-02-09 08:57:05 -05:00
|======
2016-08-10 11:12:41 -04:00
For example, this:
2019-09-09 13:38:14 -04:00
[source,console]
2016-02-09 08:57:05 -05:00
--------------------------------------------------
2016-08-10 11:12:41 -04:00
PUT _ingest/pipeline/attachment
2016-02-09 08:57:05 -05:00
{
2016-08-10 11:12:41 -04:00
"description" : "Extract attachment information",
2016-02-09 08:57:05 -05:00
"processors" : [
{
"attachment" : {
2016-04-20 12:00:11 -04:00
"field" : "data"
2016-02-09 08:57:05 -05:00
}
}
]
}
2018-03-14 14:07:20 -04:00
PUT my_index/_doc/my_id?pipeline=attachment
2016-08-10 11:12:41 -04:00
{
"data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
}
2018-03-14 14:07:20 -04:00
GET my_index/_doc/my_id
2016-08-10 11:12:41 -04:00
--------------------------------------------------
Returns this:
2019-09-06 16:09:09 -04:00
[source,console-result]
2016-08-10 11:12:41 -04:00
--------------------------------------------------
{
"found": true,
"_index": "my_index",
2018-03-14 14:07:20 -04:00
"_type": "_doc",
2016-08-10 11:12:41 -04:00
"_id": "my_id",
"_version": 1,
2018-12-17 09:22:13 -05:00
"_seq_no": 22,
"_primary_term": 1,
2016-08-10 11:12:41 -04:00
"_source": {
"data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
"attachment": {
"content_type": "application/rtf",
"language": "ro",
"content": "Lorem ipsum dolor sit amet",
2016-08-10 13:07:22 -04:00
"content_length": 28
2016-08-10 11:12:41 -04:00
}
}
}
2016-02-09 08:57:05 -05:00
--------------------------------------------------
2018-12-17 09:22:13 -05:00
// TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
2016-02-09 08:57:05 -05:00
2016-12-21 06:13:16 -05:00
To specify only some fields to be extracted:
2019-09-09 13:38:14 -04:00
[source,console]
2016-12-21 06:13:16 -05:00
--------------------------------------------------
PUT _ingest/pipeline/attachment
{
"description" : "Extract attachment information",
"processors" : [
{
"attachment" : {
"field" : "data",
"properties": [ "content", "title" ]
}
}
]
}
--------------------------------------------------
2016-02-09 08:57:05 -05:00
NOTE: Extracting contents from binary data is a resource intensive operation and
consumes a lot of resources. It is highly recommended to run pipelines
using this processor in a dedicated ingest node.
2017-04-20 09:01:37 -04:00
2018-03-14 14:07:20 -04:00
[[ingest-attachment-extracted-chars]]
==== Limit the number of extracted chars
To prevent extracting too many chars and overload the node memory, the number of chars being used for extraction
is limited by default to `100000`. You can change this value by setting `indexed_chars`. Use `-1` for no limit but
ensure when setting this that your node will have enough HEAP to extract the content of very big documents.
You can also define this limit per document by extracting from a given field the limit to set. If the document
has that field, it will overwrite the `indexed_chars` setting. To set this field, define the `indexed_chars_field`
setting.
For example:
2019-09-09 13:38:14 -04:00
[source,console]
2018-03-14 14:07:20 -04:00
--------------------------------------------------
PUT _ingest/pipeline/attachment
{
"description" : "Extract attachment information",
"processors" : [
{
"attachment" : {
"field" : "data",
"indexed_chars" : 11,
"indexed_chars_field" : "max_size"
}
}
]
}
PUT my_index/_doc/my_id?pipeline=attachment
{
"data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
}
GET my_index/_doc/my_id
--------------------------------------------------
Returns this:
2019-09-06 16:09:09 -04:00
[source,console-result]
2018-03-14 14:07:20 -04:00
--------------------------------------------------
{
"found": true,
"_index": "my_index",
"_type": "_doc",
"_id": "my_id",
"_version": 1,
2018-12-17 09:22:13 -05:00
"_seq_no": 35,
"_primary_term": 1,
2018-03-14 14:07:20 -04:00
"_source": {
"data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
"attachment": {
"content_type": "application/rtf",
"language": "sl",
"content": "Lorem ipsum",
"content_length": 11
}
}
}
--------------------------------------------------
2018-12-17 09:22:13 -05:00
// TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
2018-03-14 14:07:20 -04:00
2019-09-09 13:38:14 -04:00
[source,console]
2018-03-14 14:07:20 -04:00
--------------------------------------------------
PUT _ingest/pipeline/attachment
{
"description" : "Extract attachment information",
"processors" : [
{
"attachment" : {
"field" : "data",
"indexed_chars" : 11,
"indexed_chars_field" : "max_size"
}
}
]
}
PUT my_index/_doc/my_id_2?pipeline=attachment
{
"data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
"max_size": 5
}
GET my_index/_doc/my_id_2
--------------------------------------------------
Returns this:
2019-09-06 16:09:09 -04:00
[source,console-result]
2018-03-14 14:07:20 -04:00
--------------------------------------------------
{
"found": true,
"_index": "my_index",
"_type": "_doc",
"_id": "my_id_2",
"_version": 1,
2018-12-17 09:22:13 -05:00
"_seq_no": 40,
"_primary_term": 1,
2018-03-14 14:07:20 -04:00
"_source": {
"data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0=",
"max_size": 5,
"attachment": {
"content_type": "application/rtf",
"language": "ro",
"content": "Lorem",
"content_length": 5
}
}
}
--------------------------------------------------
2018-12-17 09:22:13 -05:00
// TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
2018-03-14 14:07:20 -04:00
2016-12-21 11:18:33 -05:00
[[ingest-attachment-with-arrays]]
==== Using the Attachment Processor with arrays
To use the attachment processor within an array of attachments the
{ref}/foreach-processor.html[foreach processor] is required. This
enables the attachment processor to be run on the individual elements
of the array.
For example, given the following source:
[source,js]
--------------------------------------------------
{
"attachments" : [
{
"filename" : "ipsum.txt",
"data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
},
{
"filename" : "test.txt",
"data" : "VGhpcyBpcyBhIHRlc3QK"
}
]
}
--------------------------------------------------
2016-12-23 00:48:44 -05:00
// NOTCONSOLE
2016-12-21 11:18:33 -05:00
In this case, we want to process the data field in each element
of the attachments field and insert
the properties into the document so the following `foreach`
processor is used:
2019-09-09 13:38:14 -04:00
[source,console]
2016-12-21 11:18:33 -05:00
--------------------------------------------------
2016-12-23 00:48:44 -05:00
PUT _ingest/pipeline/attachment
2016-12-21 11:18:33 -05:00
{
2016-12-23 00:48:44 -05:00
"description" : "Extract attachment information from arrays",
"processors" : [
{
"foreach": {
"field": "attachments",
"processor": {
"attachment": {
"target_field": "_ingest._value.attachment",
"field": "_ingest._value.data"
}
}
2016-12-21 11:18:33 -05:00
}
}
2016-12-23 00:48:44 -05:00
]
}
2018-03-14 14:07:20 -04:00
PUT my_index/_doc/my_id?pipeline=attachment
2016-12-23 00:48:44 -05:00
{
"attachments" : [
{
"filename" : "ipsum.txt",
"data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
},
{
"filename" : "test.txt",
"data" : "VGhpcyBpcyBhIHRlc3QK"
}
]
}
2018-03-14 14:07:20 -04:00
GET my_index/_doc/my_id
2016-12-23 00:48:44 -05:00
--------------------------------------------------
Returns this:
2019-09-06 16:09:09 -04:00
[source,console-result]
2016-12-23 00:48:44 -05:00
--------------------------------------------------
{
"_index" : "my_index",
2018-03-14 14:07:20 -04:00
"_type" : "_doc",
2016-12-23 00:48:44 -05:00
"_id" : "my_id",
"_version" : 1,
2018-12-17 09:22:13 -05:00
"_seq_no" : 50,
"_primary_term" : 1,
2016-12-23 00:48:44 -05:00
"found" : true,
"_source" : {
"attachments" : [
{
"filename" : "ipsum.txt",
"data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo=",
"attachment" : {
"content_type" : "text/plain; charset=ISO-8859-1",
"language" : "en",
"content" : "this is\njust some text",
"content_length" : 24
}
},
{
"filename" : "test.txt",
"data" : "VGhpcyBpcyBhIHRlc3QK",
"attachment" : {
"content_type" : "text/plain; charset=ISO-8859-1",
"language" : "en",
"content" : "This is a test",
"content_length" : 16
}
}
]
2016-12-21 11:18:33 -05:00
}
}
--------------------------------------------------
2018-12-17 09:22:13 -05:00
// TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
2016-12-23 00:48:44 -05:00
2016-12-21 11:18:33 -05:00
Note that the `target_field` needs to be set, otherwise the
default value is used which is a top level field `attachment`. The
properties on this top level field will contain the value of the
first attachment only. However, by specifying the
`target_field` on to a value on `_ingest._value` it will correctly
associate the properties with the correct attachment.