通过下面的聚合并使用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',
},
},
},
},
},
},发布于 2016-11-10 12:43:21
像这样的事情应该有效:
{
"size": 0,
"aggregations": {
"dayOfWeek": {
"terms": {
"script": {
"inline": "doc['created'].date.setZone(org.joda.time.DateTimeZone.forID(tz)); doc['created'].date.dayOfWeek",
"lang": "groovy",
"params": {
"tz": "Europe/London"
}
}
},
"aggs": {
"hourOfDay": {
"terms": {
"script": {
"inline": "doc['created'].date.setZone(org.joda.time.DateTimeZone.forID(tz)); doc['created'].date.hourOfDay",
"lang": "groovy",
"params": {
"tz": "Europe/London"
}
}
}
}
}
}
}
}您可能需要将script.engine.groovy.inline.aggs: on添加到elasticsearch.yml文件中,从而为groovy启用内联脚本。见:这一讨论。
请注意。上面的工作不会无痛,因为它是锁定和不允许编辑白名单。。
发布于 2017-10-19 13:20:03
用无痛的方法解决问题。因为他们正在将elasticsearch从Joda迁移到本地java.time,所以对Joda的支持在无痛方面不是很好。
{
"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"
}
}
}
}
}
}
}
}https://stackoverflow.com/questions/40476135
复制相似问题