我创建此脚本是为了获得两个字段之间的日期差:
use(groovy.time.TimeCategory) { def duration = doc[firstDateField].date - doc[secondDateField].date; duration.Hours; }
为了对其进行检查,我请求此_search请求:
_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。
Period
您可以使用另一个不使用JODA Period类的脚本
以此来更新您的脚本文件,它将起作用:
(doc[secondDateField].date.millis - doc[firstDateField].date.millis) / 3600000