# Replace value from one field in another field using an Elasticsearch Ingest Pipeline

**URL:** <https://discuss.elastic.co/t/replace-value-from-one-field-in-another-field-using-an-elasticsearch-ingest-pipeline/302963>\
**Category:** Elasticsearch\
**Tags:** painless, ingest-pipeline\
**Created:** [April 21, 2022, 7:52pm UTC](https://discuss.elastic.co/t/replace-value-from-one-field-in-another-field-using-an-elasticsearch-ingest-pipeline/302963 "2022-04-21T19:52:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DougR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dougr/32/48095_2.png) [@DougR](https://discuss.elastic.co/u/DougR)\
**Post date:** [April 21, 2022, 7:52pm UTC](https://discuss.elastic.co/t/replace-value-from-one-field-in-another-field-using-an-elasticsearch-ingest-pipeline/302963/1 "2022-04-21T19:52:56Z")

</div>

I need to remove a substring contained in one field from another field. However, I don't seem to be able to use the `gsub` or grok processors to accomplish this. E.g., I have the following fields:

```json
{
    "foo": "foo",
    "foobar": "foobar"
}

```

Desired outcome would be to have the final document look like the following:

```json
{
  "fields": {
    "foo": "foo",
    "foobar": "bar"
  }
}

```

However, the following throws an error:

```json
[
  "gsub": {
    "field": "fields.foobar",
    "pattern": "{{ fields.foo }}",
    "replacement": ""
  }
]

```

And the following also fails to create the new field `fields.baz: bar`:

```json
[
  "grok": {
    "field": "fields.foobar",
    "pattern": "^{{ fields.foo }}%{DATA:fields.baz}"
  }
]

```

I'm not able to preprocess the value of `fields.foobar` prior to sending it to Elasticsearch, which is why I need to do it this way. It seems that there should be a way to do this, perhaps with painless, but I haven't been able to determine it yet.

---

<div class="post-metadata">

**Author:** ![DougR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dougr/32/48095_2.png) [@DougR](https://discuss.elastic.co/u/DougR)\
**Post date:** [April 22, 2022, 2:13pm UTC](https://discuss.elastic.co/t/replace-value-from-one-field-in-another-field-using-an-elasticsearch-ingest-pipeline/302963/2 "2022-04-22T14:13:13Z")

</div>

I ended up using painless. This is the script I used:

```auto
if (ctx.fields.foo.startsWith(ctx.fields.foobar)) {
  ctx['fields']['foobar'] = ctx.fields.foobar.substring(ctx.fields.foo.length());
}

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [April 22, 2022, 2:59pm UTC](https://discuss.elastic.co/t/replace-value-from-one-field-in-another-field-using-an-elasticsearch-ingest-pipeline/302963/3 "2022-04-22T14:59:56Z")

</div>

@DougR NICE!!!

I worked on it a while and didn't have a solution yet...

Thanks for posting your solution TIL!

---

<div class="post-metadata">

**Author:** ![DougR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dougr/32/48095_2.png) [@DougR](https://discuss.elastic.co/u/DougR)\
**Post date:** [April 27, 2022, 1:27pm UTC](https://discuss.elastic.co/t/replace-value-from-one-field-in-another-field-using-an-elasticsearch-ingest-pipeline/302963/4 "2022-04-27T13:27:03Z")

</div>

> [@DougR](#):
>
> ```auto
> if (ctx.fields.foo.startsWith(ctx.fields.foobar)) {
> ctx['fields']['foobar'] = ctx.fields.foobar.substring(ctx.fields.foo.length());
> }
> 
> ```

There was actually a bug in the original solution I posted, and I can't edit it at this point. The code should've been:

```auto
if (ctx.fields.foobar.startsWith(ctx.fields.foo)) {
  ctx['fields']['foobar'] = ctx.fields.foobar.substring(ctx.fields.foo.length());
}

```

---

<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:** [May 25, 2022, 1:27pm UTC](https://discuss.elastic.co/t/replace-value-from-one-field-in-another-field-using-an-elasticsearch-ingest-pipeline/302963/5 "2022-05-25T13:27:23Z")

</div>

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