使用 Painless 进行一年中的一周聚合
Week of the year aggregations using Painless
我在按 "Week of Year" 数量汇总数据时遇到了挑战。
用例是:
一周的第一天在我的电脑上设置为星期日。
创建数据范围:
Start='2018-06-24 Sunday' and End='2018-06-30 Saturday'
预期:我只能看到一周的总和
实际: 我可以看到两周的总和
弹性查询:
{
"size": 0,
"aggs": {
"groupby": {
"terms": {
"script": {
"source": "ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc['CLOSED_DATE'].value.millis), ZoneId.of('UTC')).get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)"
},
"size": 100
}
}
},
"query": {
"bool": {
"must": [
{
"range": {
"CLOSED_DATE": {
"gte": "2018-06-24T00:00:01",
"lte": "2018-06-30T23:59:59",
"time_zone": "UTC"
}
}
}
]
}
}
}
我也试过了,结果一样:
{
"size": 0,
"aggs": {
"groupby": {
"terms": {
"script": {
"source": "doc['CLOSED_DATE'].value.getWeekOfWeekyear()"
},
"size": 100
}
}
},
"query": {
"bool": {
"must": [
{
"range": {
"CLOSED_DATE": {
"gte": "2018-06-24T00:00:01",
"lte": "2018-06-30T23:59:59",
"time_zone": "UTC"
}
}
}
]
}
}
}
公平地说,此查询适用于大多数用例。
它失败的是像上面这个用例这样的场景。
欢迎任何提示!!!
谢谢
这成功了:
def date = ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc[params.dt_field_name].value), ZoneId.of(params.timezone)); DayOfWeek dayOfWeek = DayOfWeek.valueOf(params.first_day_week); date = date.with(TemporalAdjusters.previousOrSame(dayOfWeek)).plusWeeks(1); return date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
我在按 "Week of Year" 数量汇总数据时遇到了挑战。
用例是: 一周的第一天在我的电脑上设置为星期日。 创建数据范围:
Start='2018-06-24 Sunday' and End='2018-06-30 Saturday'
预期:我只能看到一周的总和
实际: 我可以看到两周的总和
弹性查询:
{
"size": 0,
"aggs": {
"groupby": {
"terms": {
"script": {
"source": "ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc['CLOSED_DATE'].value.millis), ZoneId.of('UTC')).get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)"
},
"size": 100
}
}
},
"query": {
"bool": {
"must": [
{
"range": {
"CLOSED_DATE": {
"gte": "2018-06-24T00:00:01",
"lte": "2018-06-30T23:59:59",
"time_zone": "UTC"
}
}
}
]
}
}
}
我也试过了,结果一样:
{
"size": 0,
"aggs": {
"groupby": {
"terms": {
"script": {
"source": "doc['CLOSED_DATE'].value.getWeekOfWeekyear()"
},
"size": 100
}
}
},
"query": {
"bool": {
"must": [
{
"range": {
"CLOSED_DATE": {
"gte": "2018-06-24T00:00:01",
"lte": "2018-06-30T23:59:59",
"time_zone": "UTC"
}
}
}
]
}
}
}
公平地说,此查询适用于大多数用例。 它失败的是像上面这个用例这样的场景。
欢迎任何提示!!! 谢谢
这成功了:
def date = ZonedDateTime.ofInstant(Instant.ofEpochMilli(doc[params.dt_field_name].value), ZoneId.of(params.timezone)); DayOfWeek dayOfWeek = DayOfWeek.valueOf(params.first_day_week); date = date.with(TemporalAdjusters.previousOrSame(dayOfWeek)).plusWeeks(1); return date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);