查看按工作日分类的文档?

问题描述 投票:0回答:1

我的 ES 2.4.0 中有这样的数据:

{"text": "are","created_at": "2017-01-29T05:30:36.000Z"}
{"text": "hi","created_at": "2017-01-28T18:30:36.000Z"}
{"text": "you","created_at": "2017-01-31T09:30:36.000Z"} 
{"text": "how","created_at": "2017-01-28T11:30:36.000Z"}
{"text": "hello","created_at": "2017-01-30T23:30:36.000Z"}

为了查看按工作日分类的文档,我使用了以下常规脚本:

Date date = new Date(doc[date_field].value); date.format(format);

在我的输出中,它显示如下:

 "aggregations": {
    "byDays": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "Sunday",
          "doc_count": 2
        },
        {
          "key": "Tuesday",
          "doc_count": 2
        },
        {
          "key": "Saturday",
          "doc_count": 1
        }
      ]
    }
  }
}

但是我的输入日期将在接下来的几天出现

   {"text": "are","created_at": "2017-01-29T05:30:36.000Z"} --sunday
    {"text": "hi","created_at": "2017-01-28T18:30:36.000Z"}--saturday
    {"text": "you","created_at": "2017-01-31T09:30:36.000Z"} --tuesday
    {"text": "how","created_at": "2017-01-28T11:30:36.000Z"}--sunday
    {"text": "hello","created_at": "2017-01-30T23:30:36.000Z"}--monday

我需要更改时区才能获得准确的日期吗?

elasticsearch groovy
1个回答
0
投票

更改

ZoneId.of
中的参数并发送此类查询

GET /days/_search?filter_path=aggregations
{
    "runtime_mappings": {
        "day_of_week": {
            "type": "keyword",
            "script": {
                "source": """
                    ZonedDateTime dateTime = doc['created_at'].value;
                    ZonedDateTime rezonedDateTime = dateTime.withZoneSameInstant(ZoneId.of('UTC+7'));

                    emit(rezonedDateTime.getDayOfWeek().toString())
                """
            }
        }
    },
    "aggs": {
        "day_of_week": {
            "terms": {
                "field": "day_of_week",
                "size": 10
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.