Elasticsearch日期类型、常见时区问题

Date datatype

简介

存储格式

JSON 本身没有date类型,Elasticsearch将设置了时区的date转换为UTC时间,将没有设置时区的date直接设置为UTC时间,以long型时间戳存储。

查询统计

date类型查询,将date转换为long型时间戳查询;

date类型聚合,返回结果格式化date字符串;

ElasticSearch查询、存储,将date类型转换为UTC long型时间戳。

相同格式存储、查询(同是时间戳或同是格式化字符串),查询时间一致;查询、存储格式不一致,相差8个小时(GMT+08:00与UTC之间相差8小时)

示例

索引

PUT date_index { "settings": { "number_of_shards": 1, "number_of_replicas": 0 }, "mappings": { "test": { "properties": { "date": { "type": "date" } } } } }

查询

格式化date存储,时间戳查询

格式化date存储

POST /_bulk {"index":{"_index":"date_index","_type":"test", "_id":"1"}} {"date":"2020-01-01T00:00:00Z"} {"index":{"_index":"date_index","_type":"test", "_id":"2"}} {"date":"2020-01-01T08:00:00Z"} {"index":{"_index":"date_index","_type":"test", "_id":"3"}} {"date":"2020-01-01T16:00:00Z"} {"index":{"_index":"date_index","_type":"test", "_id":"4"}} {"date":"2020-01-01T23:00:00Z"} {"index":{"_index":"date_index","_type":"test", "_id":"5"}} {"date":"2020-01-02T00:00:00Z"} {"index":{"_index":"date_index","_type":"test", "_id":"6"}} {"date":"2020-01-02T08:00:00Z"} {"index":{"_index":"date_index","_type":"test", "_id":"7"}} {"date":"2020-01-02T16:00:00Z"} {"index":{"_index":"date_index","_type":"test", "_id":"8"}} {"date":"2020-01-02T23:00:00Z"}

时间戳查询

2020-01-01T16:00:00Z(UTC时间 = 东八区-8)

= 2020-01-02 00:00:00(默认东八区时间)

= 2020-01-02T00:00:00Z+08:00(东八区时间)

= 1577894400000

存储格式date类型转换为long型时间戳,查询格式date为long型时间戳,东八区时间戳和UTC时间戳相差8小时

request

POST date_index/_search { "query": { "range": { "date": { "gte": "1577894400000" } } }, "size": 1 }

response

{ "_index" : "date_index", "_type" : "test", "_id" : "3", "_score" : 1.0, "_source" : { "date" : "2020-01-01T16:00:00Z" } }

时间戳存储、格式化date查询

时间戳存储

POST /_bulk {"index":{"_index":"date_index","_type":"test", "_id":"1"}} {"date":"1577980800000"} {"index":{"_index":"date_index","_type":"test", "_id":"2"}} {"date":"1578009600000"} {"index":{"_index":"date_index","_type":"test", "_id":"3"}} {"date":"1578038400000"} {"index":{"_index":"date_index","_type":"test", "_id":"4"}} {"date":"1578063600000"} {"index":{"_index":"date_index","_type":"test", "_id":"5"}} {"date":"1578067200000"} {"index":{"_index":"date_index","_type":"test", "_id":"6"}} {"date":"1578096000000"} {"index":{"_index":"date_index","_type":"test", "_id":"7"}} {"date":"1578124800000"} {"index":{"_index":"date_index","_type":"test", "_id":"8"}} {"date":"1578150000000"}

格式化查询

request

POST date_index/_search { "query": { "range": { "date": { "gte": "2020-01-04T00:00:00Z" } } } }

response

{ "_index" : "date_index", "_type" : "test", "_id" : "6", "_score" : 1.0, "_source" : { "date" : "1578096000000" } }

1578096000000

= 2020-01-04 08:00:00(默认东八区时间)

= 2020-01-04T08:00:00Z+08:00(默认东八区时间)

=2020-01-04T00:00:00Z(UTC时间)

聚合

Elasticserch内存存储都是long型时间戳、UTC时间,想要获取本地时区的统计信息需要指定时区。

request

POST date_index/_search { "size": 0, "aggs": { "by_day": { "date_histogram": { "field": "date", "interval": "day", "time_zone": "+08:00" } } } }

response

{ "by_day" : { "buckets" : [ { "key_as_string" : "2020-01-01T00:00:00.000+08:00", "key" : 1577808000000, "doc_count" : 2 }, { "key_as_string" : "2020-01-02T00:00:00.000+08:00", "key" : 1577894400000, "doc_count" : 4 }, { "key_as_string" : "2020-01-03T00:00:00.000+08:00", "key" : 1577980800000, "doc_count" : 2 } ] } }

参考资料

https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html

<think>根据用户查询和提供的引用内容,用户想了解Elasticsearch常见问题及解决方法。以下将基于引用的信息进行系统整理:引用[1]和[2]提到Elasticsearch是强大的开源分布式搜索和分析引擎,广泛用于全文搜索、日志分析等场景。以下是常见Elasticsearch问题及解决方法:问题1:集群健康问题(引用[1])集群健康状态为RED或YELLOW表示存在严重问题(如分片未分配、副本不足等)或部分问题(如缺少副本分片)[^1]。解决方法:-检查集群状态:通过`GET_cluster/health`查看具体原因。-节点恢复:确保节点间网络正常,节点配置文件正确。-索引分片调整:重新分配未分配的分片。问题2:日期类型存储问题(引用[4])Elasticsearch内部将日期统一转换为UTC时间戳存储,时间字符串的不同展示格式可能导致查询结果与预期不符[^4]。解决方法:-在索引映射中定义日期格式模板:```json{"mappings":{"properties":{"create_time":{"type":"date","format":"yyyy-MM-ddHH:mm:ss||yyyy-MM-dd||epoch_millis"}}}}```问题3:Root权限运行异常(引用[5])错误信息:`java.lang.RuntimeException:cannotrunElasticsearchasroot`[^5]解决方法:-为Elasticsearch创建专属用户并授权:```bashuseraddelasticsearchchown-Relasticsearch:elasticsearch/path/to/elasticsearchsuelasticsearchbin/elasticsearch```问题4:内存不足启动失败(引用[3])错误日志:`org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91)~[elasticsearch-5.4.1.jar:5.4.1]`解决方法:-调整`jvm.options`配置:```text-Xms4g-Xmx4g```**扩展建议**:-分片策略优化:避免单节点分片过多。-冷热数据分离:降低集群负载。§§相关问题§§1.Elasticsearch集群状态为RED该如何诊断?2.如何正确配置Elasticsearch的JVM堆内存大小?3.Elasticsearch部署时有哪些安全注意事项?</think> ### Elasticsearch 常见问题及解决方法 根据引用内容[1][2][3][4][5],以下是Elasticsearch典型问题及其解决方案: --- #### 1. **集群健康状态异常** [^1][^2] **现象**:`GET /_cluster/health` 返回状态为 **RED**(主分片缺失)或 **YELLOW**(副本分片缺失)。 **解决方法**: - 检查未分配分片: ```GET _cat/shards?h=index,shard,prirep,state,unassigned.reason``` - 强制分片重新分配(需确认数据安全性): ```POST _cluster/reroute?retry_failed``` --- #### 2. **日期类型存储异常** [^4] **现象**:日期查询结果与原始数据时区不符。 **原因**:Elasticsearch 内部将所有日期转为 UTC 时间戳存储(毫秒级精度)。 **解决方法**: 在映射中显式声明时区: ```json PUT my_index/_mapping { "properties": { "log_time": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||epoch_millis", "time_zone": "+08:00" } } } ``` --- #### 3. **Root权限运行报错** [^5] **错误**:`can not run Elasticsearch as root` **解决方法**: ```bash # 创建专用用户 useradd elastic -m chown -R elastic:elastic /path/to/elasticsearch su elastic ./bin/elasticsearch ``` --- #### 4. **堆内存溢出** [^3] **日志特征**:`org.elasticsearch.bootstrap.Elasticsearch.main` 报内存错误。 **优化方案**: 修改 `config/jvm.options`: ```text # 根据服务器内存调整(建议不超过物理内存50%) -Xms4g -Xmx4g ``` --- #### 5. **分片未分配** **常见原因**: - 磁盘空间不足(>85%使用率触发只读模式) **解决方法**: ```PUT _all/_settings {"index.blocks.read_only_allow_delete": null}``` --- #### 6. **写入性能下降** **优化策略**: - 批量写入:使用 `_bulk` API - 刷新间隔调整: ```PUT my_index/_settings {"refresh_interval": "30s"}``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值