Querying scripted field is resulting an error


(Kabali12345) #1

Hi all....
Here I have created a scripted field for calculating Alarm duration, where i have conditions to calculate duration between two dates.

here is my script .

if(doc['CLEARTIME'].date.year  !=  1970) {
return (doc['CLEARTIME'].value.getMillis() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000;
}
if((doc['CLEARTIME'].date.year  ==  1970) && (doc['DELETEDAT'].empty)) {
return (new Date().getTime() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000;
}
if(doc['CLEARTIME'].date.year  ==  1970) {
return (doc['DELETEDAT'].value.getMillis() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000;
}

with this script i am getting result,but when i have tried to query on this scripted field,I am getting following error on kibana devtool

Error: Request to Elasticsearch failed: {"error":{"root_cause":[{"type":"script_exception","reason":"compile error","script_stack":["... rn s.get() == v;}compare(() -> { if(doc['CLEARTIME ...","                             ^---- HERE"],"script":"boolean compare(Supplier s, def v) {return s.get() == v;}compare(() -> { if(doc['CLEARTIME'].date.year  !=  1970) {\nreturn (doc['CLEARTIME'].value.getMillis() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000;\n}\nif((doc['CLEARTIME'].date.year  ==  1970) && (doc['DELETEDAT'].empty)) {\nreturn (new Date().getTime() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000;\n}\nif(doc['CLEARTIME'].date.year  ==  1970) {\nreturn (doc['DELETEDAT'].value.getMillis() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000;\n} }, params.value);","lang":"painless"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"reporter_data-1","node":"tf16RQXDQrCRF5uVTzcEwQ","reason":{"type":"query_shard_exception","reason":"failed to create query: {\n  \"bool\" : {\n    \"must\" : [\n      {\n        \"match_all\" : {\n          \"boost\" : 1.0\n        }\n      },\n      {\n        \"script\" : {\n          \"script\" : {\n            \"source\" : \"boolean compare(Supplier s, def v) {return s.get() == v;}compare(() -> { if(doc['CLEARTIME'].date.year  !=  1970) {\\nreturn (doc['CLEARTIME'].value.getMillis() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000;\\n}\\nif((doc['CLEARTIME'].date.year  ==  1970) && (doc['DELETEDAT'].empty)) {\\nreturn (new Date().getTime() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000;\\n}\\nif(doc['CLEARTIME'].date.year  ==  1970) {\\nreturn (doc['DELETEDAT'].value.getMillis() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000;\\n} }, params.value);\",\n            \"lang\" : \"painless\",\n            \"params\" : {\n              \"value\" : 1\n            }\n          },\n          \"boost\" : 1.0\n        }\n      },\n      {\n        \"range\" : {\n          \"FIRSTOCCURRENCE\" : {\n            \"from\" : 1523697176841,\n            \"to\" : 1526289176841,\n            \"include_lower\" : true,\n            \"include_upper\" : true,\n            \"format\" : \"epoch_millis\",\n            \"boost\" : 1.0\n          }\n        }\n      }\n    ],\n    \"adjust_pure_negative\" : true,\n    \"boost\" : 1.0\n  }\n}","index_uuid":"X56tDel3So2MA0qCraCyxg","index":"reporter_data-1","caused_by":{"type":"script_exception","reason":"compile error","script_stack":["... rn s.get() == v;}compare(() -> { if(doc['CLEARTIME ...","                             ^---- HERE"],"script":"boolean compare(Supplier s, def v) {return s.get() == v;}compare(() -> { if(doc['CLEARTIME'].date.year  !=  1970) {\nreturn (doc['CLEARTIME'].value.getMillis() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000;\n}\nif((doc['CLEARTIME'].date.year  ==  1970) && (doc['DELETEDAT'].empty)) {\nreturn (new Date().getTime() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000;\n}\nif(doc['CLEARTIME'].date.year  ==  1970) {\nreturn (doc['DELETEDAT'].value.getMillis() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000;\n} }, params.value);","lang":"painless","caused_by":{"type":"illegal_argument_exception","reason":"Not all paths provide a return value for method [lambda$0]."}}}}]},"status":400}
    at http://192.168.3.51:5601/bundles/kibana.bundle.js?v=16337:61:655936
    at Function.Promise.try (http://192.168.3.51:5601/bundles/commons.bundle.js?v=16337:50:19289)
    at http://192.168.3.51:5601/bundles/commons.bundle.js?v=16337:50:18677
    at Array.map (<anonymous>)
    at Function.Promise.map (http://192.168.3.51:5601/bundles/commons.bundle.js?v=16337:50:18635)
    at callResponseHandlers (http://192.168.3.51:5601/bundles/kibana.bundle.js?v=16337:61:655514)
    at http://192.168.3.51:5601/bundles/kibana.bundle.js?v=16337:61:644874
    at processQueue (http://192.168.3.51:5601/bundles/commons.bundle.js?v=16337:29:132456)
    at http://192.168.3.51:5601/bundles/commons.bundle.js?v=16337:29:133349
    at Scope.$digest (http://192.168.3.51:5601/bundles/commons.bundle.js?v=16337:29:144239)

Please help me with this error

Thank you


(Ryan Ernst) #2

Your script needs to have a return statement outside of any if condition. If all of the conditions fail, there needs to be something returned.


(Kabali12345) #3

This is how my problem got solved.

if(doc['CLEARTIME'].date.year  !=  1970) {
return (doc['CLEARTIME'].value.getMillis() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000 / 60;
}
else if((doc['CLEARTIME'].date.year  ==  1970) && (doc['DELETEDAT'].empty)) {
return (new Date().getTime() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000 / 60;
}
else if(doc['CLEARTIME'].date.year  ==  1970) {
return (doc['DELETEDAT'].value.getMillis() - doc['OSFIRSTOCCURRENCE'].value.getMillis()) / 1000 /60 ;
}
else {
return "";
}

Thank You...!
Please let me know if you have any suggestion for this code...!


(system) #4

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