首页 > 解决方案 > Elasticasearch 脚本中无法识别日期时区 ID“美国”

问题描述

我有一个酒店位置的 Elasticsearch 索引,时区详细信息如下

{
"location":"1",
.
.
. 
"timezone": {
            "timeZoneName": "Eastern Daylight Time",
            "rawOffset": -18000,
            "timeZoneId": "America/New_York",
            "dstOffset": 3600,
            "status": "OK"
          }
}

我在 elasticsearch 脚本中使用这个时区详细信息来过滤文档。

def current_date = new Date(); 
def loc_date = current_date.setZone(org.joda.time.DateTimeZone.forID(doc['timezone.timeZoneId'].value)); 
def day=loc_date.format('EEEE').toString().toLowerCase();

当我在我的弹性搜索脚本(Groovy)中使用时区详细信息时,它会抛出以下错误

"type": "script_exception",
          "reason": "failed to run indexed script [uc-time-test38] using lang [groovy]",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "The datetime zone id 'america' is not recognised"
          }

看起来doc['timezone.timeZoneId'].value只返回美国而不是America/New_York

试图将doc['timezone.timeZoneId'].value转换为 String doc['timezone.timeZoneId'].value.toString()但问题仍然存在。

我的 Elasticsearch 版本是 2.3

搜索时在索引中查询也不给出结果America/New_York

下面的查询返回所有结果

   {
      "query": {
        "bool": {
          "must": {
            "term": {
              "timezone.timeZoneId": "america"
            }
          }
        }
      }
    }

但这给出了 0 结果

{
  "query": {
    "bool": {
      "must": {
        "term": {
          "timezone.timeZoneId": "America/New_York"
        }
      }
    }
  }
}

ES映射

.
.
.
"timezone": {
        "properties": {
          "timeZoneName": {
            "type": "string"
          },
          "rawOffset": {
            "type": "long"
          },
          "timeZoneId": {
            "type": "string"
          },
          "dstOffset": {
            "type": "long"
          },
          "status": {
            "type": "string"
          }
        }
      },
.
.
.

标签: elasticsearchgroovy

解决方案


@Grab(group='joda-time', module='joda-time', version='2.9.9')
import org.joda.time.DateTimeZone

//this line prints America/New_York
println DateTimeZone.forID('America/New_York')

//this one throws exception
//java.lang.IllegalArgumentException: The datetime zone id 'America' is not recognised
println DateTimeZone.forID('America')

所以问题在你的数据中


推荐阅读