# Painless processor add field and set name from variable

**URL:** https://discuss.elastic.co/t/painless-processor-add-field-and-set-name-from-variable/265074
**Category:** Elasticsearch
**Tags:** painless
**Created:** [February 22, 2021, 2:11pm UTC](https://discuss.elastic.co/t/painless-processor-add-field-and-set-name-from-variable/265074 "2021-02-22T14:11:41Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ella.danielsson](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@ella.danielsson](https://discuss.elastic.co/u/ella.danielsson)
#### Post date: [February 22, 2021, 2:11pm UTC](https://discuss.elastic.co/t/painless-processor-add-field-and-set-name-from-variable/265074/1 "2021-02-22T14:11:41Z")

</div>

Hi,

I'm trying to use a painless script processor to split the content of a field and use the result of the split to add new fields to the document. The problem is that I can't figure out how to set a new field, the name and its content, from variables.

I have a field that looks like this:

```auto
{
    "_source": {
        "message": "test1 = 1 | test2 = 2 | test3 = 3"
    }
 }

```

And I want the result to look like this:

```auto
{
    "_source":{
         "message": "test1 = 1 | test2 = 2 | test3 = 3",
         "test1": 1,
         "test2": 2,
         "test3": 3
    }
}

```

This is the script that I have so far and what I am trying to do.

```auto
{
"source": """
    String [] msgFields = ctx.message.splitOnToken('|');

    for (field in msgFields)
    {
      def keyValue=field.splitOnToken('=');

      //This is what i want to do set name of the field to the value of keyValue[0] and set the value of the field to keyValue[1]
      ctx.keyValue[0]=keyValue[1];
   }
"""
}

```

---

<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 22, 2021, 2:12pm UTC](https://discuss.elastic.co/t/painless-processor-add-field-and-set-name-from-variable/265074/2 "2021-03-22T14:12:36Z")

</div>

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