# Injest Pipeline for conditional statement used in Scripts

**URL:** <https://discuss.elastic.co/t/injest-pipeline-for-conditional-statement-used-in-scripts/298178>\
**Category:** Kibana\
**Tags:** painless, kql-kibana-query-language, ingest-pipeline\
**Created:** [February 24, 2022, 1:59pm UTC](https://discuss.elastic.co/t/injest-pipeline-for-conditional-statement-used-in-scripts/298178 "2022-02-24T13:59:00Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 28, 2022, 11:10pm UTC](https://discuss.elastic.co/t/injest-pipeline-for-conditional-statement-used-in-scripts/298178/22 "2022-02-28T23:10:52Z")

</div>

> [@shivani\_singh](#):
>
> `if( ctx['responseBody'] != null && ctx.containsKey('results'))`

The latter make no sense here if the former does not raise any error.

---

<div class="post-metadata">

**Author:** ![shivani\_singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shivani_singh/32/102450_2.png) [@shivani\_singh](https://discuss.elastic.co/u/shivani_singh)\
**Post date:** [March 1, 2022, 5:38am UTC](https://discuss.elastic.co/t/injest-pipeline-for-conditional-statement-used-in-scripts/298178/23 "2022-03-01T05:38:26Z")

</div>

> [@Tomo\_M](#):
>
> e what you want to do, it is possible to create an array and add values in each iteration, then finally set the array to `ctx['embedded_error_code']` .

As I said, `responseBody` contains different format of json data within so it might happen there is `responseBody` but not `results`.

---

<div class="post-metadata">

**Author:** ![shivani\_singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shivani_singh/32/102450_2.png) [@shivani\_singh](https://discuss.elastic.co/u/shivani_singh)\
**Post date:** [March 1, 2022, 5:45am UTC](https://discuss.elastic.co/t/injest-pipeline-for-conditional-statement-used-in-scripts/298178/24 "2022-03-01T05:45:18Z")

</div>

Now I'm renaming -

```auto
 {
      "rename": {
        "field": "responseBody.results",
        "target_field": "embedded_original_value_obj",
        "ignore_missing": true
      }
    },

```

So now I'm taking array `results` into my new field `embedded_original_value_obj` which has` type: object` in mapping.  
It is working fine and I'm getting `responseBody.results[0].statusCode` value in `embedded_original_value_obj.statusCode` ✅

🔴 Note : Both `set` and `rename` is working fine for me here, I'm using `set` in below example

---

<div class="post-metadata">

**Author:** ![shivani\_singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shivani_singh/32/102450_2.png) [@shivani\_singh](https://discuss.elastic.co/u/shivani_singh)\
**Post date:** [March 1, 2022, 7:21am UTC](https://discuss.elastic.co/t/injest-pipeline-for-conditional-statement-used-in-scripts/298178/25 "2022-03-01T07:21:56Z")

</div>

Hi @Tomo_M ,

I'm using `set` here.  
`1. set` is working and I'm getting value in `embedded_original_value_obj` but `2. set` isn't working. Any idea how can do this?

```auto
PUT _ingest/pipeline/apiproxy-embedded-copy
{
  "processors": [
    {
1. "set": {
        "field": "embedded_original_value_obj",
        "copy_from": "responseBody.results",
        "ignore_empty_value": true
      }
    },
    {
2. "set": {
        "field": "embedded_error_code",
        "copy_from": "embedded_original_value_obj.statusCode",
        "ignore_empty_value": true
      }
    }
]
}

```

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [March 1, 2022, 4:22pm UTC](https://discuss.elastic.co/t/injest-pipeline-for-conditional-statement-used-in-scripts/298178/26 "2022-03-01T16:22:45Z")

</div>

Rename looks good to achive your goal.

But as for set processors,

```auto
POST _ingest/pipeline/_simulate
{
  "docs": [
    {
      "_source":{
        "responseBody":{
          "results":{
            "statusCode": "200"
          }
        }
      }
    }
  ],
  "pipeline": {
    "processors": [
      {
        "set": {
          "field": "embedded_original_value_obj",
          "copy_from": "responseBody.results",
          "ignore_empty_value": true
        }
      },
      {
        "set": {
          "field": "embedded_error_code",
          "copy_from": "embedded_original_value_obj.statusCode",
          "ignore_empty_value": true
        }
      }
    ]
  }
}

```

```auto
{
  "docs" : [
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_doc",
        "_id" : "_id",
        "_source" : {
          "embedded_original_value_obj" : {
            "statusCode" : "200"
          },
          "responseBody" : {
            "results" : {
              "statusCode" : "200"
            }
          },
          "embedded_error_code" : "200"
        },
        "_ingest" : {
          "timestamp" : "2022-03-01T16:15:52.708791852Z"
        }
      }
    }
  ]
}

```

"2. set" looks also working in my environment. What you want to do?

In addition, you have to remove unnecessary fields (or set [`dynamic` paramter](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic.html) to `false`) to avoid mapping\_parsing\_exception. I suppose `rename` processor is better for you if there is no other reason.

---

<div class="post-metadata">

**Author:** ![shivani\_singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shivani_singh/32/102450_2.png) [@shivani\_singh](https://discuss.elastic.co/u/shivani_singh)\
**Post date:** [March 2, 2022, 5:25am UTC](https://discuss.elastic.co/t/injest-pipeline-for-conditional-statement-used-in-scripts/298178/27 "2022-03-02T05:25:12Z")

</div>

> [@Tomo\_M](#):
>
> ition, you have to remove unn

Hi @Tomo_M ,  
Thank you for your efforts. I resolved this issue by using `for each` processor in the place of `2nd set`, one of the community members helped me through this -  
[Set processor isn't working after another Set processor - Injest Pipeline - Elastic Stack / Elasticsearch - Discuss the Elastic Stack](https://discuss.elastic.co/t/set-processor-isnt-working-after-another-set-processor-injest-pipeline/298478)

Thank you.

---

<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:** [March 30, 2022, 5:25am UTC](https://discuss.elastic.co/t/injest-pipeline-for-conditional-statement-used-in-scripts/298178/28 "2022-03-30T05:25:41Z")

</div>

Closed on March 30, 2022, 5:25am UTC

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

[Previous page](https://discuss.elastic.co/t/injest-pipeline-for-conditional-statement-used-in-scripts/298178.md?page=1)
