通过下面的聚合并使用 ES5,我想根据给定时区获取 dayOfWeek 和 hourOfDay(作为标识符从 TZ 数据库提供)。
如何编辑 "doc['created'].date.dayOfWeek'
以调整偏移量?
aggs: {
dayOfWeek: {
terms: {
script: {
inline: "doc['created'].date.dayOfWeek",
lang: 'painless',
},
},
aggs: {
hourOfDay: {
terms: {
script: {
inline: "doc['created'].date.hourOfDay",
lang: 'painless',
},
},
},
},
},
},
最佳答案
使用无痛找到解决方案。因为他们正在将 elasticsearch 从 Joda 迁移到原生的 java.time,所以对 Joda 的支持在 painless 中不是很好。
{
"size": 0,
"aggregations": {
"dayOfWeek": {
"terms": {
"script": {
"inline": "Instant.ofEpochMilli(doc.created.date.millis).atZone(ZoneId.of(params.tz)).dayOfWeek",
"params": {
"tz": "Europe/London"
}
}
},
"aggs": {
"hourOfDay": {
"terms": {
"script": {
"inline": "Instant.ofEpochMilli(doc.created.date.millis).atZone(ZoneId.of(params.tz)).hour",
"params": {
"tz": "Europe/London"
}
}
}
}
}
}
}
}
关于elasticsearch - 在 Elasticsearch 中,如何将时区应用于脚本化日期操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40476135/