elasticsearch - 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"
}
}
},
.
.
.
解决方案
@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')
所以问题在你的数据中
推荐阅读
- amazon-web-services - SNS 中的时间戳消息,因为它通过
- c++ - 声明 RAII 类型 (unique_lock) 的 C++ 推荐方式
- joomla - 在 Joomla 中使用 PHPOffice/PHPSpreadsheet
- javascript - 从外部javascript获取动态变量
- javascript - 使用角度更新多个 html 输入元素
- c++ - Qt - QGridLayout
- javascript - 如何在 javascript 中构建交互式 3D 图形
- android - 无法验证 dex:错误的方法句柄类型 7
- python - 在这个函数中传递 kwargs 的最 Pythonic 方式是什么?
- jmeter - Jmeter 验证响应