ElasticSearch: Get duration between two fields script syntax error

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

I've created this script in order to get a date difference between two fields:

use(groovy.time.TimeCategory) {     def duration = doc[firstDateField].date - doc[secondDateField].date;     duration.Hours; } 

In order to check it I'm requesting this _search request:

#docker exec -it es curl -XGET 'http://localhost:9200/living_team/fuas/_search?pretty' -d ' {   "script_fields": {     "my_script": {       "script": {         "file": "dayDateDiff",         "params": {           "firstDateField": "timestamp",           "secondDateField": "startTimestamp"         }       }     }   } } ' 

ElasticSearch is telling me:

{   "error" : {     "root_cause" : [ {       "type" : "script_exception",       "reason" : "failed to run file script [dayDateDiff] using lang [groovy]"     } ],     "type" : "search_phase_execution_exception",     "reason" : "all shards failed",     "phase" : "query_fetch",     "grouped" : true,     "failed_shards" : [ {       "shard" : 3,       "index" : "living_v1",       "node" : "SSgkS5Y9QV-EmzaeP_7hKQ",       "reason" : {         "type" : "script_exception",         "reason" : "failed to run file script [dayDateDiff] using lang [groovy]",         "caused_by" : {           "type" : "missing_property_exception",           "reason" : "No such property: groovy for class: 94b4e4baddb8e121bd26f2098185e84d368e4162"         }       }     } ]   },   "status" : 500 } 

So, the question would be, why do I need to set a field in order perform an script? and lastly, how can I send this script request correctly?

EDIT

I've changed my script:

new Period(doc[firstDateField].date, doc[secondDateField].date).getHours(); 

However, it's telling me now:

bbd8b73ce0b0dd070d07e63f11dcdad4fa12121d: 1: unable to resolve class Period Nov 04 10:27:21 core-01 docker[3876]:  @ line 1, column 1. Nov 04 10:27:21 core-01 docker[3876]:    new Period(doc[firstDateField].date, doc[secondDateField].date).getHours(); Nov 04 10:27:21 core-01 docker[3876]:    ^ 

It's telling me there's some problem with Period.

回答1:

You can use another script that doesn't make use of the JODA Period class

Update your script file with this and that'll work:

(doc[secondDateField].date.millis - doc[firstDateField].date.millis) / 3600000 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!