# Querying scripted field is resulting an error

**URL:** <https://discuss.elastic.co/t/querying-scripted-field-is-resulting-an-error/131705>\
**Category:** Elasticsearch\
**Created:** [May 14, 2018, 9:33am UTC](https://discuss.elastic.co/t/querying-scripted-field-is-resulting-an-error/131705 "2018-05-14T09:33:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kabali12345](https://avatars.discourse-cdn.com/v4/letter/k/6a8cbe/32.png) [@kabali12345](https://discuss.elastic.co/u/kabali12345)\
**Post date:** [May 14, 2018, 9:33am UTC](https://discuss.elastic.co/t/querying-scripted-field-is-resulting-an-error/131705/1 "2018-05-14T09:33:59Z")

</div>

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 .

```auto
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

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [May 14, 2018, 5:15pm UTC](https://discuss.elastic.co/t/querying-scripted-field-is-resulting-an-error/131705/2 "2018-05-14T17:15:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kabali12345](https://avatars.discourse-cdn.com/v4/letter/k/6a8cbe/32.png) [@kabali12345](https://discuss.elastic.co/u/kabali12345)\
**Post date:** [May 15, 2018, 9:09am UTC](https://discuss.elastic.co/t/querying-scripted-field-is-resulting-an-error/131705/3 "2018-05-15T09:09:06Z")

</div>

This is how my problem got solved.

```auto
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...!

---

<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:** [June 12, 2018, 9:09am UTC](https://discuss.elastic.co/t/querying-scripted-field-is-resulting-an-error/131705/4 "2018-06-12T09:09:08Z")

</div>

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