ElasticSearch:获取两个字段之间的持续时间脚本语法错误 [英] ElasticSearch: Get duration between two fields script syntax error

查看:2049
本文介绍了ElasticSearch:获取两个字段之间的持续时间脚本语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

为了检查它,我要求这个 _search 请求:

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是告诉我:

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?

编辑

我改变了我的脚本:

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 期间

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

这篇关于ElasticSearch:获取两个字段之间的持续时间脚本语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆