# Conditionally Apply Attachment Processor

**URL:** https://discuss.elastic.co/t/conditionally-apply-attachment-processor/307867
**Category:** Elasticsearch
**Tags:** painless, ingest-pipeline
**Created:** [June 22, 2022, 10:43am UTC](https://discuss.elastic.co/t/conditionally-apply-attachment-processor/307867 "2022-06-22T10:43:39Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jasmoon](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@jasmoon](https://discuss.elastic.co/u/jasmoon)
#### Post date: [June 22, 2022, 10:43am UTC](https://discuss.elastic.co/t/conditionally-apply-attachment-processor/307867/1 "2022-06-22T10:43:39Z")

</div>

Hi all, first time posting in this forum. I have trouble writing the condition to apply the attachment processor. Below is the wrong configuration of the pipeline:

```auto
PUT _ingest/pipeline/attachment
{
  "description" : "Extract attachment information from arrays",
  "processors" : [
    {
      "foreach": {
        "field": "attachments",
        "processor": {
          "attachment": {
            **"if": "ctx?.attachments.data != null",**
            "target_field": "_ingest._value.attachment",
            "field": "_ingest._value.data"
          }
        }
      }
    },{
      "foreach": {
        "field": "attachments",
        "remove": {
          "attachment": {
            **"if": "ctx?.attachments.data != null",**
            "field": "_ingest._value.data"
          }
        }
      }
    }
  ]
}

```

Have been brute forcing various ways to access the `data` field but to no avail or failed to apply the pipeline if somehow managed to access it.

Below is a sample data to ingest:

```auto
PUT my-index-000001/_doc/my_id?pipeline=attachment
{
  "attachments" : [
    {
      "filename" : "ipsum.txt",
      "data" : "dGhpcyBpcwpqdXN0IHNvbWUgdGV4dAo="
    },
    {
      "filename" : "test.txt"
    }
  ]
}

```

Expected:

```auto
{
  "_index" : "my-index-000001",
  "_id" : "my_id",
  "_version" : 1,
  "_seq_no" : 50,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "attachments" : [
      {
        "filename" : "ipsum.txt",
        "attachment" : {
          "content_type" : "text/plain; charset=ISO-8859-1",
          "language" : "en",
          "content" : "this is\njust some text",
          "content_length" : 24
        }
      },
      {
        "filename" : "test.txt"
      }
    ]
  }
}

```

Thank you all in advance!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 20, 2022, 10:44am UTC](https://discuss.elastic.co/t/conditionally-apply-attachment-processor/307867/2 "2022-07-20T10:44:05Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
