在Elasticasearch脚本中无法识别日期时间区域ID'美国' [英] The datetime zone id 'america' is not recognised in Elasticasearch script
问题描述
我有一个针对酒店位置的Elasticsearch索引,其时区详细信息如下
I have an Elasticsearch index for hotel locations with timezone details as below
{
"location":"1",
.
.
.
"timezone": {
"timeZoneName": "Eastern Daylight Time",
"rawOffset": -18000,
"timeZoneId": "America/New_York",
"dstOffset": 3600,
"status": "OK"
}
}
我正在Elasticsearch脚本中使用此时区详细信息来过滤文档.
I'm using this timezone detail in elasticsearch script to filter docs.
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();
当我在Elasticsearch脚本(Groovy)中使用时区详细信息时,它会抛出错误以下消息
While I'm using timezone details in my elasticsearch script (Groovy) it is throwing below error
"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 的只是返回美国,而不是美国/纽约
Looks like doc['timezone.timeZoneId'].value is only returning america not America/New_York
试图将 doc ['timezone.timeZoneId'].value 转换为字符串 doc ['timezone.timeZoneId'].value.toString(),但问题仍然存在保持不变.
Tried to convert doc['timezone.timeZoneId'].value as String doc['timezone.timeZoneId'].value.toString() but problem is still remain same.
我的Elasticsearch版本是2.3
My Elasticsearch version is 2.3
使用America/New_York
以下查询将返回所有结果
Below query is returning all results
{
"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"
}
}
},
.
.
.
推荐答案
已解决.问题在于保存的时区ID.分析了timeZoneId字段,这就是为什么将字符串America/New_York
作为两个单独的关键字America
和New_York
的原因.
Solved it. Problem was with the saved timezone id. timeZoneId field was analysed thats why assuming string America/New_York
as two separate keywords, America
and New_York
.
我做了
- 备份旧索引
- 使用新映射创建新索引,将timeZoneId字段设置为not_analysed(关键字)
- 将所有数据推送到新索引
- 现在
doc['timezone.timeZoneId'].value
在脚本中返回America/New_York
- Backed up old index
- Create a new index with new mapping, Set timeZoneId field as not_analysed (keyword)
- Pushed all data to new index
- Now
doc['timezone.timeZoneId'].value
returnsAmerica/New_York
in script
希望这可以帮助其他人.感谢您的讨论.
Hope this can help others. Thanks for discussion.
这篇关于在Elasticasearch脚本中无法识别日期时间区域ID'美国'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!