博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ES5.x 中long类型不支持time zone
阅读量:3687 次
发布时间:2019-05-21

本文共 1824 字,大约阅读时间需要 6 分钟。

问题

为了在ES上根据时间进行统计,我在ES5.6.4上做了date_histogram聚合,查询语句如下所示

{  "aggregations": {    "dateVspAggs": {      "date_histogram": {        "field": "timestamp",        "interval": "day",        "time_zone": "+08:00",        "order": {          "_key": "asc"        },        "keyed": false,        "min_doc_count": 0,        "extended_bounds": {          "min": 1551369600000,          "max": 1554047999999        }      }    }  }}

因为是interval是day,所以我配置了time_zone 参数,从而避免查询结果的bucket统计范围有时差。

然而这个查询语句返回报错如下:

{"error": {"root_cause": [{"type": "illegal_argument_exception","reason": "Field [timestamp_] of type [long] does not support custom time zones"}],"type": "search_phase_execution_exception","reason": "all shards failed","phase": "query","grouped": true,"failed_shards": [{"shard": 0,"index": "test","node": "ytXqsnmdSqmbKwEyw22fXg","reason": {"type": "illegal_argument_exception","reason": "Field [timestamp_] of type [long] does not support custom time zones"}}]},"status": 400}

报错信息显示time_zones的配置不支持long类型的字段。

不过值得一提的是相同的查询在ES2.x环境是能够成功执行的。

分析

为此我特意查看了ES5.x的源码,发现ES5.x对number类型的字段校验过程中,如果发现它配置了time_zone 就会报异常,从ES5.x开始time_zone 只支持date类型。

相关代码如下所示
LegacyNumberFieldMapper::NumberFieldType::docValueFormat

public DocValueFormat docValueFormat(@Nullable String format, DateTimeZone timeZone) {            if (timeZone != null) {                throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom time zones");            }            if (format == null) {                return DocValueFormat.RAW;            } else {                return new DocValueFormat.Decimal(format);            }        }

为了进一步求证,我去github上提了问题,得到如下回答

在这里插入图片描述

结论

从ES的项目成员的回答中可以得出结论,time_zone的特性本来就是为date类型提供的,至于ES2.x中支持long类型只是因为这个版本对错误的配置更加容忍罢了,在ES5.x之后time_zone只支持date类型了。所以在ES中时间字段最好还是用date类型来存储。

转载地址:http://wwydn.baihongyu.com/

你可能感兴趣的文章
字符串合并
查看>>
贪心算法
查看>>
斐波那契数列
查看>>
java蓝桥杯2017年A组
查看>>
编写代码与初步运行
查看>>
汇编语言之debug篇
查看>>
随机行走
查看>>
树与二叉树
查看>>
荷兰国旗问题
查看>>
Java实现 第十一届 蓝桥杯 (本科组)省内模拟赛(1)
查看>>
螺旋矩阵/正整数摆动
查看>>
小明植树
查看>>
8.2 可编程并行接口芯片8255A
查看>>
户户通---Java蓝桥杯
查看>>
历年数学建模大赛优秀论文解读
查看>>
最优化模型
查看>>
范式题
查看>>
80X86指令系统(2)---数据传送指令
查看>>
图的应用-----关键路径
查看>>
BP神经网络算法
查看>>