# Use conditional if in a Set processor

**URL:** <https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971>\
**Category:** Elasticsearch\
**Created:** [June 10, 2019, 10:56am UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971 "2019-06-10T10:56:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [June 10, 2019, 10:56am UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971/1 "2019-06-10T10:56:55Z")

</div>

Hi there!

Title is kinda explicative. How do I use a set processor in an ingest pipeline only if another field exists?

I mean, I want to copy the value of field\_A into another field\_B only if field\_A does exist.  
Guess it can be accomplished since there's the 'if' parameter among the Set Processor ones, but I do not know the correct syntax to use it.

Also, field\_A is a nested field.

Ideas?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 10, 2019, 1:14pm UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971/2 "2019-06-10T13:14:41Z")

</div>

Hey,

try using the `if` field in the processor

```auto
{
  "set": {
    "if": "ctx.foo == 'someValue'",
    "field": "found",
    "value": true
  }
}

```

See [https://www.elastic.co/guide/en/elasticsearch/reference/7.1/ingest-processors.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.1/ingest-processors.html)

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [June 11, 2019, 8:20am UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971/3 "2019-06-11T08:20:54Z")

</div>

Hi!

Thank you so much. It works apparently even though I cannot access nested fields with the same syntax I use in the conditional if of the script processor.

I mean, what if I had to do something like:

```
`"if": "ctx[outer_obj].nested_field == 'someValue'"`
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 11, 2019, 9:24am UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971/4 "2019-06-11T09:24:01Z")

</div>

try `ctx.outer_obj.nested_field` or `ctx["outer_obj"].nested_field`

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [June 11, 2019, 9:41am UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971/5 "2019-06-11T09:41:49Z")

</div>

I think I tried both with no success.

Anyway, I'll try them again ASAP and let you know. The info might help someone else!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 11, 2019, 12:24pm UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971/6 "2019-06-11T12:24:50Z")

</div>

please provide a full blown example using the simulate endpoint and we can try to help.

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [June 12, 2019, 8:31am UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971/7 "2019-06-12T08:31:17Z")

</div>

Ok I tested it and both your solutions worked like a charm. Maybe I was working with a wrong input (field containing a dot in its name without being a nested object).

Anyway, I'll share my test (where I included several combinations) so it may help somebody with the syntax:

```
POST _ingest/pipeline/_simulate
{
  "docs": [ 
    { "_source" : { "outer": {"inner": "X"} } }, 
    { "_source" : { "outer": {"inner": "Y"} } }, 
    { "_source" : { "outer": {"inner": "Z"} } }, 
    { "_source" : { "outer": {"inner": "T"} } } 
    ],
  "pipeline": {
    "processors": [
      {
        "set": {
          "if": "ctx.containsKey('outer') && ctx['outer'].containsKey('inner') && ctx.outer.inner == 'Y'",
          "field": "added_field",
          "value": "added_value"
        }
      }
    ]
  }
}

```

RESPONSE:

```
{
  "docs" : [
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "outer" : {
            "inner" : "X"
          }
        },
        "_ingest" : {
          "timestamp" : "2019-06-12T08:24:23.360Z"
        }
      }
    },
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "outer" : {
            "inner" : "Y"
          },
          "added_field" : "added_value"
        },
        "_ingest" : {
          "timestamp" : "2019-06-12T08:24:23.360Z"
        }
      }
    },
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "outer" : {
            "inner" : "Z"
          }
        },
        "_ingest" : {
          "timestamp" : "2019-06-12T08:24:23.360Z"
        }
      }
    },
    {
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "outer" : {
            "inner" : "T"
          }
        },
        "_ingest" : {
          "timestamp" : "2019-06-12T08:24:23.360Z"
        }
      }
    }
  ]
}

```

Thank you so much for your precious help!

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [June 12, 2019, 9:38am UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971/8 "2019-06-12T09:38:45Z")

</div>

Just to cover all possibilities, what if I wanted to access a non-nested fields with dots in its name inside the "field" part? Let's suppose I'm in a situation where if a field `inner` nested in a object `outer` is present, I want to add an additional field `fake.nested.field`.

If a try this:

```
POST _ingest/pipeline/_simulate
{
  "docs": [ 
    { "_source" : { "outer": {"inner": "X"} } }, 
    { "_source" : { "outer": {"inner": "Y"} } }, 
    { "_source" : { "outer": {"inner": "Z"} } }, 
    { "_source" : { "outer": {"inner": "T"} } } 
    ],
  "pipeline": {
    "processors": [
      {
        "set": {
          "if": "ctx.containsKey('outer') && ctx['outer'].containsKey('inner')",
          "field": "fake.nested.field",
          "value": "fake_nested_value"
        }
      }
    ]
  }
}

```

I'd end up with documents like the following:

```
{
      "doc" : {
        "_index" : "_index",
        "_type" : "_type",
        "_id" : "_id",
        "_source" : {
          "outer" : {
            "inner" : "X"
          },
          "fake" : {
            "nested" : {
              "field" : "fake_nested_value"
            }
          }
        },
        "_ingest" : {
          "timestamp" : "2019-06-12T09:35:05.424Z"
        }
      }
    }

```

As you can see, the fake.nested.field is not so fake.

How can I add "fake.nested.field": "fake\_nested\_value"?

Thanks!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 12, 2019, 12:39pm UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971/9 "2019-06-12T12:39:34Z")

</div>

One last nit. No need to hassle with `containsKey`, just use

```auto
"if": "ctx?.outer?.inner == 'Y'"

```

I would generally not recommend using dots in field names, cause of its ambiguity when specifying inner fields. I think scripting like this could work though

```auto
POST _ingest/pipeline/_simulate
{
  "docs": [ 
    { "_source" : { "outer": {"inner": "X"} } }, 
    { "_source" : { "outer": {"inner": "Y"} } }, 
    { "_source" : { "outer": {"inner": "Z"} } }, 
    { "_source" : { "outer": {"inner": "T"} } } 
    ],
  "pipeline": {
    "processors": [
      {
        "script": {
           "source": "ctx['i.am.sooooo.nested'] = true"
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [June 12, 2019, 12:53pm UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971/10 "2019-06-12T12:53:37Z")

</div>

Thanks for the first tip!

Regardind the fields with dots in the name, I always try not to use them but sometimes they're added by external applications or somebody who does something wrong adds them, so I'd like to learn how to handle them.

I knew I could handle them via script but what if I wanted to remove that field rather than add it?  
In that case I'd need a `remove` processor so I'd fall back on the previous syntax.

Or is there another way to remove a fake nested field (maybe via script as well)?

Thanks!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 12, 2019, 1:31pm UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971/11 "2019-06-12T13:31:22Z")

</div>

you can also remove fields in a script. `ctx.remove("field.with.dots")` should do the trick

---

<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 10, 2019, 1:31pm UTC](https://discuss.elastic.co/t/use-conditional-if-in-a-set-processor/184971/12 "2019-07-10T13:31:23Z")

</div>

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