Hi Team,
I'm trying to create a script field based in certain conditions. I'm a newbie to creating scripted fields so I'm facing troubles in creating it.
As of now my script looks like this.
GET terracota_sample/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"status": {
"script": {
"lang": "painless",
"source": """
if(doc['agentId.keyword'].value =~ /PerfWAG1/){
def perfWAG1Name = doc['name.keyword'];
def perfWAG1Size = doc['Size.keyword'].value;
}
else if(doc['agentId.keyword'].value =~ /PerfWAG2/){
def perfWAG2Name = doc['name.keyword'];
def perfWAG2Size = doc['Size.keyword'].value;
}
int status = 0;
if (perfWAG1Name == perfWAG2Name) {
if (perfWAG1Size == perfWAG2Size){
status = 1;
}else{
status = -1;
}
}
return status;
"""
}
}
}
}
When i test this in DevTools console, I'm getting this following error
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"... tId.keyword'].value == ~ /PerfWAG1/){\r\n def perfW ...",
" ^---- HERE"
],
"script": " if(doc['agentId.keyword'].value == ~ /PerfWAG1/){\r\n def perfWAG1Name = doc['name.keyword'];\r\n def perfWAG1Size = doc['Size.keyword'].value;\r\n}\r\nelse if(doc['agentId.keyword'].value == ~ /PerfWAG2/){\r\n def perfWAG2Name = doc['name.keyword'];\r\n def perfWAG2Size = doc['Size.keyword'].value;\r\n}\r\nint status = 0;\r\nif (perfWAG1Name == perfWAG2Name) {\r\n if (perfWAG1Size == perfWAG2Size){\r\n status = 1;\r\n }else{\r\n status = -1;\r\n }\r\n}\r\nreturn status;",
"lang": "painless"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "elkrft_ind_test",
"node": "515B4tODSJqBR6WetvxydA",
"reason": {
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"... tId.keyword'].value == ~ /PerfWAG1/){\r\n def perfW ...",
" ^---- HERE"
],
"script": " if(doc['agentId.keyword'].value == ~ /PerfWAG1/){\r\n def perfWAG1Name = doc['name.keyword'];\r\n def perfWAG1Size = doc['Size.keyword'].value;\r\n}\r\nelse if(doc['agentId.keyword'].value == ~ /PerfWAG2/){\r\n def perfWAG2Name = doc['name.keyword'];\r\n def perfWAG2Size = doc['Size.keyword'].value;\r\n}\r\nint status = 0;\r\nif (perfWAG1Name == perfWAG2Name) {\r\n if (perfWAG1Size == perfWAG2Size){\r\n status = 1;\r\n }else{\r\n status = -1;\r\n }\r\n}\r\nreturn status;",
"lang": "painless",
"caused_by": {
"type": "illegal_state_exception",
"reason": "Regexes are disabled. Set [script.painless.regex.enabled] to [true] in elasticsearch.yaml to allow them. Be careful though, regexes break out of Painless's protection against deep recursion and long loops."
}
}
}
]
},
"status": 500
}
I wanna check if the field 'agentId' has "PerfWAG1" in it. Am I doing it wrong? Kindly Help me out with solving this issue.
Thank you.