我已创建此脚本以获取两个字段之间的日期差异:
use(groovy.time.TimeCategory) {
def duration = doc[firstDateField].date - doc[secondDateField].date;
duration.Hours;
}
为了检查它,我正在请求此_search
请求:
#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告诉我:
{
"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
}
所以,问题是,为什么我需要设置一个字段才能执行脚本?最后,我该如何正确发送此脚本请求?
修改
我改变了我的剧本:
new Period(doc[firstDateField].date, doc[secondDateField].date).getHours();
然而,它现在告诉我:
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]: ^
它告诉我Period
存在一些问题。
答案 0 :(得分:1)
您可以使用另一个不使用JODA Period
类的脚本
使用此更新您的脚本文件,并且该工作:
(doc[secondDateField].date.millis - doc[firstDateField].date.millis) / 3600000