首页 > 解决方案 > Elasticsearch聚合不同时区

问题描述

我正在尝试查看直方图除以一天中的小时数。

问题是时区:如果我在美国早上 8 点和欧洲早上 8 点放一个文件,我有不同的时区,但在图表中我想在同一时间汇总它们。

所以我的结果应该是上午 8 点的2 个文档计数

我的实际数据:

[
  {
    "_index" : "test_index",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
      "my_time" : "2021-09-17T08:00:00.000-04:00",
      "value" : 1
    }
  },
  {
    "_index" : "test_index",
    "_type" : "_doc",
    "_id" : "2",
    "_score" : 1.0,
    "_source" : {
      "my_time" : "2021-09-17T08:00:00.000+02:00",
      "value" : 2
    }
  },
  {
    "_index" : "test_index",
    "_type" : "_doc",
    "_id" : "3",
    "_score" : 1.0,
    "_source" : {
      "my_time" : "2021-09-17T03:00:00.000-04:00",
      "value" : 3
    }
  },
  {
    "_index" : "test_index",
    "_type" : "_doc",
    "_id" : "4",
    "_score" : 1.0,
    "_source" : {
      "my_time" : "2021-09-17T04:00:00.000-08:00",
      "value" : 4
    }
  },
  {
    "_index" : "test_index",
    "_type" : "_doc",
    "_id" : "5",
    "_score" : 1.0,
    "_source" : {
      "my_time" : "2021-09-17T23:00:00.000+04:00",
      "value" : 5
    }
  }
]

在映射中

"my_time": {
    "type": "date"
 }

我的查询:

POST /test_index/_search?size=0
{
  "aggs": {
    "my_results": {
      "date_histogram": {
        "field": "my_time",
        "calendar_interval": "1h"
      }
    }
  }
}

但结果很糟糕!例如:

"buckets" : [
    {
      "key_as_string" : "2021-09-17T06:00:00.000Z",
      "key" : 1631858400000,
      "doc_count" : 1
    },
    {
      "key_as_string" : "2021-09-17T07:00:00.000Z",
      "key" : 1631862000000,
      "doc_count" : 1
    },
    ......
]

我该如何解决?

标签: datetimeelasticsearchtimezoneelasticsearch-aggregationelasticsearch-dsl

解决方案


推荐阅读