# Setting a field to either one or another field depending on the existence of one

**URL:** <https://discuss.elastic.co/t/setting-a-field-to-either-one-or-another-field-depending-on-the-existence-of-one/356462>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [March 29, 2024, 11:59am UTC](https://discuss.elastic.co/t/setting-a-field-to-either-one-or-another-field-depending-on-the-existence-of-one/356462 "2024-03-29T11:59:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefws](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stefws/32/6442_2.png) [@stefws](https://discuss.elastic.co/u/stefws)\
**Post date:** [March 29, 2024, 11:59am UTC](https://discuss.elastic.co/t/setting-a-field-to-either-one-or-another-field-depending-on-the-existence-of-one/356462/1 "2024-03-29T11:59:56Z")

</div>

Just dipping my toes into painless, only it isn't simple painless 😉

I'm trying to conditionally set a field to either one or another field depending on the existence of one field like this:

```auto
  {
    "script": {
      "lang": "painless",
      "source": "if (ctx.containsKey('epj.user_login')) {\r\n ctx.epj.user = ctx.epj.user_login;\r\n} else {\r\n ctx.epj.user = ctx.log.file.userdir;\r\n}"
    }
  }
]

```

but seems to always execute the 'else' part, even when documents actually contains an epj.user\_login field, wondering why, any hints appreciated, TIA!

![Screenshot 2024-03-29 at 12.56.08](https://us1.discourse-cdn.com/elastic/original/3X/c/f/cfd5abd20bf6189a323278a46993b4c40eaf1ec1.png)

---

<div class="post-metadata">

**Author:** ![stefws](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stefws/32/6442_2.png) [@stefws](https://discuss.elastic.co/u/stefws)\
**Post date:** [March 29, 2024, 1:58pm UTC](https://discuss.elastic.co/t/setting-a-field-to-either-one-or-another-field-depending-on-the-existence-of-one/356462/2 "2024-03-29T13:58:57Z")

</div>

Replace the painless with two Set processors like this:

```auto
  {
    "set": {
      "field": "epj.user",
      "copy_from": "log.file.userdir",
      "ignore_empty_value": true,
      "ignore_failure": true
    }
  },
  {
    "set": {
      "field": "epj.user",
      "copy_from": "epj.user_login",
      "ignore_empty_value": true,
      "ignore_failure": true
    }
  }

```

This seems to work 🙂

 ![Screenshot 2024-03-29 at 14.56.00](https://us1.discourse-cdn.com/elastic/original/3X/8/6/86f7e90d37d71fff05e8136424bbeebbf3b646ad.png)

---

<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:** [April 26, 2024, 1:59pm UTC](https://discuss.elastic.co/t/setting-a-field-to-either-one-or-another-field-depending-on-the-existence-of-one/356462/3 "2024-04-26T13:59:11Z")

</div>

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