ElasticSearch:获取两个字段之间的持续时间脚本语法错误



我创建了这个脚本,以便获得两个字段之间的日期差异:

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有问题

您可以使用另一个不使用JODA Period类的脚本

更新你的脚本文件,这样就可以了:

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

最新更新