gpt4 book ai didi

elasticsearch - ElasticSearch的年和每周汇总

转载 作者:行者123 更新时间:2023-12-02 23:38:39 25 4
gpt4 key购买 nike

我每年进行一次嵌套聚合,然后每年在Elasticsearch中进行每周一次嵌套聚合。 years年有53周,但是ElasticSearch的结果给出的是year年的最后一周key =“1”而不是“53”。如何让ElasticSearch在上周返回53而不是1?

这是我的查询:

GET _search
{
"size": 0,
"aggs": {
"activities_per_year": {
"date_histogram": {
"field": "start",
"interval": "1y",
"format": "yyyy"
},
"aggs": {
"activities_per_week": {
"date_histogram": {
"field": "start",
"interval": "week",
"format": "w"
}
}
}
}
}
}

结果(中间删除的数据):
"key_as_string": "2008",
"key": 1199145600000,
"doc_count": 872,
"activities_per_week": {
"buckets": [
{
"key_as_string": "1",
"key": 1199059200000,
"doc_count": 6
},
{
"key_as_string": "2",
"key": 1199664000000,
"doc_count": 5
},
{
"key_as_string": "3",
"key": 1200268800000,
"doc_count": 15
}, {
"key_as_string": "51",
"key": 1229299200000,
"doc_count": 18
},
{
"key_as_string": "52",
"key": 1229904000000,
"doc_count": 7
},
{
"key_as_string": "1",
"key": 1230508800000,
"doc_count": 1
}
]

2008年是a年,最后一个星期有“key_as_string”:“1”。我希望它是53,因此可以将其添加到字典中:)我该怎么做?

另外,elasticsearch会返回两周的“key_as_string”:“1”代表2013年,我不认为2013年是year年吗?

最佳答案

这有一些微妙的陷阱,您需要意识到。首先,Elasticsearch将Joda Time API用于与日期时间相关的内容。

其次,看看this关于什么实际上是“周”的解释:

A week based year is one where dates are expressed as a day of week, week number and year (week based). The following description is of the ISO8601 standard used by implementations of this method in this library.

Weeks run from 1 to 52-53 in a week based year. The first day of the week is defined as Monday and given the value 1.

The first week of a year is defined as the first week that has at least four days in the year. As a result of this definition, week 1 may extend into the previous year, and week 52/53 may extend into the following year. Hence the need for the year of weekyear field.

For example, 2003-01-01 was a Wednesday. This means that five days, Wednesday to Sunday, of that week are in 2003. Thus the whole week is considered to be the first week of 2003. Since all weeks start on Monday, the first week of 2003 started on 2002-12-30, ie. in 2002.

The week based year has a specific text format. 2002-12-30 (Monday 30th December 2002) would be represented as 2003-W01-1. 2003-01-01 (Wednesday 1st January 2003) would be represented as 2003-W01-3.



因此,在您的情况下,您会看到2008年12月29日属于第一周,因为2008年12月29日所在的一周是2008年的三天和2009年的四天。根据上述规则,这是从2009年。这与leap年无关。举个例子,尝试索引31-12-2009和31-12-2015。两者都将给您第53周的时间,而这并不是leap年。

为了更好地查看这些内容,我建议您使用以下格式进行汇总: "format": "x-w---yyyy-MM-dd":
{
"size": 0,
"aggs": {
"activities_per_year": {
"date_histogram": {
"field": "start",
"interval": "1y",
"format": "yyyy"
},
"aggs": {
"activities_per_week": {
"date_histogram": {
"field": "start",
"interval": "week",
"format": "x-w---yyyy-MM-dd"
}
}
}
}
}
}

关于elasticsearch - ElasticSearch的年和每周汇总,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26671300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com