# Creating a New Keyword Field at Runtime from Two Existing Fields

**URL:** <https://discuss.elastic.co/t/creating-a-new-keyword-field-at-runtime-from-two-existing-fields/376193>\
**Category:** Elastic Search\
**Tags:** painless\
**Created:** [March 20, 2025, 5:03pm UTC](https://discuss.elastic.co/t/creating-a-new-keyword-field-at-runtime-from-two-existing-fields/376193 "2025-03-20T17:03:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shubham\_Pant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shubham_pant/32/142026_2.png) [@Shubham\_Pant](https://discuss.elastic.co/u/Shubham_Pant)\
**Post date:** [March 20, 2025, 5:03pm UTC](https://discuss.elastic.co/t/creating-a-new-keyword-field-at-runtime-from-two-existing-fields/376193/1 "2025-03-20T17:03:47Z")

</div>

I need to create a new field dynamically at runtime using two existing fields, and the new field should be of type "keyword".

I attempted this using Painless scripting, but it's not working and throws a type casting error.

The expected output should be:

- If both fields have values: ["Apple", "Orange"]
- If only one field has a value: ["Apple"]

How can I correctly concatenate two fields into a keyword array at runtime? Any suggestions would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [March 20, 2025, 9:13pm UTC](https://discuss.elastic.co/t/creating-a-new-keyword-field-at-runtime-from-two-existing-fields/376193/2 "2025-03-20T21:13:31Z")

</div>

You need to provide some example of your data, the script that you are trying to use and the error that you are getting.

---

<div class="post-metadata">

**Author:** ![Shubham\_Pant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shubham_pant/32/142026_2.png) [@Shubham\_Pant](https://discuss.elastic.co/u/Shubham_Pant)\
**Post date:** [March 20, 2025, 9:33pm UTC](https://discuss.elastic.co/t/creating-a-new-keyword-field-at-runtime-from-two-existing-fields/376193/3 "2025-03-20T21:33:22Z")

</div>

I have data in below form

![image](https://us1.discourse-cdn.com/elastic/original/3X/6/4/64021c2a98e098ce8c915049b1b3014ad1589e6f.png)

This is the code i am using

```auto
{
  "runtime_mappings": {
    "newQ3": {
      "type": "keyword",
      "script": {
        "source": """
          def result = [];
         
          if (doc.containsKey('Q1') && doc['Q1'].size() > 0 && doc['Q1'].value != null) {
            result.add(doc['Q1.Answer'].value.toString());
          }

          if (doc.containsKey('Q2') && doc['Q2'].size() > 0 && doc['Q2'].value != null) {
            result.add(doc['Q2.Answer'].value.toString());
          }   
          emit(result);
        """
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Shubham\_Pant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shubham_pant/32/142026_2.png) [@Shubham\_Pant](https://discuss.elastic.co/u/Shubham_Pant)\
**Post date:** [April 4, 2025, 4:39pm UTC](https://discuss.elastic.co/t/creating-a-new-keyword-field-at-runtime-from-two-existing-fields/376193/4 "2025-04-04T16:39:03Z")

</div>

Can someone help me in this, I am new to elastic

I want to create a new variable with name QC by combining the response of QA.Answer and QB.Answer

**Below is my update code**

```auto
def result = [];

if (doc.containsKey('QA.Answer') && !doc['QA.Answer'].empty) {
  result.add(doc['QA.Answer'].value);
}

if (doc.containsKey('QB.Answer') && !doc['QB.Answer'].empty) {
  result.add(doc['QB.Answer'].value);
}

emit(result.toArray(new String[0]))

```

**Getting an below error,**

Cannot cast [Ljava.lang.String; to java.lang.String

**Data Format**

"QA": {  
"Answer": "Open",  
},  
"QB": {  
"Answer": "Apple",  
},  
"QC": [  
"Open",  
"Apple"  
}

---

<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 2, 2025, 4:39pm UTC](https://discuss.elastic.co/t/creating-a-new-keyword-field-at-runtime-from-two-existing-fields/376193/5 "2025-05-02T16:39:27Z")

</div>

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