ElasticSearch Term Aggregation 脚本时区转换
ElasticSearch Term Aggregation script timezone conversion
我真的很纠结这个。在 Painless 中,我将如何更新以下内容:
"aggs": {
"total_messages_per_day_of_week": {
"terms": {
"script": {
"lang": "painless",
"source": "doc['date'].value.dayOfWeek"
}
},
要在获取星期几之前将 doc.date 中的 UTC 日期时间转换为区域设置(例如 'America/Los_Angeles')?
基本上我想按天数汇总,但是天数代表所需的时区天,而不是 UTC 天。
非常感谢!
您可以这样做,首先通过 Instant.atZone()
将您的 UTC 日期转换为 ZonedDateTime
,然后取星期几:
Instant date = Instant.ofEpochMilli(doc['timestamp'].value);
ZonedDateTime zdt = date.atZone(ZoneId.of('America/Los_Angeles'));
return zdt.getDayOfWeek().getValue();
并且由于 doc.date.value
实际上是 JodaCompatibleZonedDateTime
(即 ZonedDateTime
的代表),在您的聚合中您可以尝试这样做:
{
...,
"aggs": {
"days": {
"terms": {
"script": "doc['timestamp'].value. withZoneSameInstant(ZoneId.of('America/Los_Angeles')).getDayOfWeek().getValue()"
}
}
}
}
我真的很纠结这个。在 Painless 中,我将如何更新以下内容:
"aggs": {
"total_messages_per_day_of_week": {
"terms": {
"script": {
"lang": "painless",
"source": "doc['date'].value.dayOfWeek"
}
},
要在获取星期几之前将 doc.date 中的 UTC 日期时间转换为区域设置(例如 'America/Los_Angeles')?
基本上我想按天数汇总,但是天数代表所需的时区天,而不是 UTC 天。
非常感谢!
您可以这样做,首先通过 Instant.atZone()
将您的 UTC 日期转换为 ZonedDateTime
,然后取星期几:
Instant date = Instant.ofEpochMilli(doc['timestamp'].value);
ZonedDateTime zdt = date.atZone(ZoneId.of('America/Los_Angeles'));
return zdt.getDayOfWeek().getValue();
并且由于 doc.date.value
实际上是 JodaCompatibleZonedDateTime
(即 ZonedDateTime
的代表),在您的聚合中您可以尝试这样做:
{
...,
"aggs": {
"days": {
"terms": {
"script": "doc['timestamp'].value. withZoneSameInstant(ZoneId.of('America/Los_Angeles')).getDayOfWeek().getValue()"
}
}
}
}