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!
leandrojmp
(Leandro Pereira)
March 20, 2025, 9:13pm
2
You need to provide some example of your data, the script that you are trying to use and the error that you are getting.
I have data in below form
This is the code i am using
{
"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);
"""
}
}
}
}