Elasticsearch时区问题
生活随笔
收集整理的這篇文章主要介紹了
Elasticsearch时区问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
發現問題
創建索引,配置字段類型
PUT testdate {"mappings": {"testdate":{"properties": {"tag":{"type": "keyword"},"datetime":{"type": "date"}}}} }批量插入數據
POST /_bulk {"create":{"_index":"testdate","_type":"testdate","_id":1}} {"tag":"帶時區信息","datetime":"2019-07-15T08:00:00+08:00"} {"create":{"_index":"testdate","_type":"testdate","_id":2}} {"tag":"不帶時區信息","datetime":"2019-07-15T08:00:00"} {"create":{"_index":"testdate","_type":"testdate","_id":3}} {"tag":"時間戳","datetime":"1563148800000"}對數據進行聚合操作
不帶時區信息聚合
結果
不帶時區信息時間顯示正常, 時間戳和帶時區信息時間提前8小時
東八區
POST /testdate/testdate/_search {"size": 0, "aggs": {"aggs_datetime": {"date_histogram": {"field": "datetime","interval": "hour","time_zone": "+08:00"},"aggs": {"aggs_tag": {"terms": {"field": "tag","size": 10}}}}} }結果
時間戳和帶時區信息時間顯示正常, 不帶時區信息時間推后8小時
對數據進行結構化查詢
時間戳
三條數據全部返回
{"took": 2,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "testdate","_type": "testdate","_id": "2","_score": 1,"_source": {"tag": "不帶時區信息","datetime": "2019-07-15T08:00:00"}},{"_index": "testdate","_type": "testdate","_id": "1","_score": 1,"_source": {"tag": "帶時區信息","datetime": "2019-07-15T08:00:00+08:00"}},{"_index": "testdate","_type": "testdate","_id": "3","_score": 1,"_source": {"tag": "時間戳","datetime": "1563148800000"}}]} }不帶時區
POST /testdate/testdate/_search {"query": {"range": {"datetime": {"gte": "2019-07-15T08:00:00"}}} }只返回了不帶時區信息的一條數據
{"took": 1,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 1,"max_score": 1,"hits": [{"_index": "testdate","_type": "testdate","_id": "2","_score": 1,"_source": {"tag": "不帶時區信息","datetime": "2019-07-15T08:00:00"}}]} }帶時區
POST /testdate/testdate/_search {"query": {"range": {"datetime": {"gte": "2019-07-15T08:00:00+08:00"}}} }三條數據全部返回
{"took": 0,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 3,"max_score": 1,"hits": [{"_index": "testdate","_type": "testdate","_id": "2","_score": 1,"_source": {"tag": "不帶時區信息","datetime": "2019-07-15T08:00:00"}},{"_index": "testdate","_type": "testdate","_id": "1","_score": 1,"_source": {"tag": "帶時區信息","datetime": "2019-07-15T08:00:00+08:00"}},{"_index": "testdate","_type": "testdate","_id": "3","_score": 1,"_source": {"tag": "時間戳","datetime": "1563148800000"}}]} }原因分析
/*** @desc : 時間戳* @author : cheng* @date : 2019-12-10 23:23*/@Testpublic void testA() throws Exception{String dateStr = "2019-07-15 08:00:00";SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 解析字符串,時區:東八區Date date = dateFormat.parse(dateStr);System.out.println(date.getTime());// 格式化日期,時區:0時區dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));System.out.println(dateFormat.format(date));}// 輸出15631488000002019-07-15 00:00:00 /*** @desc : 帶時區* @author : cheng* @date : 2019-12-10 23:24*/@Testpublic void testB() throws Exception{String dateStr = "2019-07-15 08:00:00+0800";SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");// 解析字符串,時區:東八區Date date = dateFormat.parse(dateStr);// 格式化日期,時區:0時區dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));System.out.println(dateFormat.format(date));}// 輸出2019-07-15 00:00:00+0000 /*** @desc : 不帶時區信息* @author : cheng* @date : 2019-12-10 23:23*/@Testpublic void testC() throws Exception{String dateStr = "2019-07-15 08:00:00";SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 解析字符串,時區:0時區dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));Date date = dateFormat.parse(dateStr);// 格式化日期,時區:0時區dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));System.out.println(dateFormat.format(date));}// 輸出2019-07-15 08:00:00ES日期類型使用UTC
時間戳1563148800000
時間戳本身就是UTC毫秒數, 和時區沒有關系, 實際存儲的就是這個值
不帶時區信息格式2019-07-15T08:00:00
等價于2019-07-15T08:00:00+00:00, 默認就是UTC 0時區時間, 實際存儲的就是這個值
帶時區信息格式2019-07-15T08:00:00+08:00
東八區時間 = UTC0時區 + 8個小時
所以實際存儲的值為UTC0時區 = 東八區 - 8小時 = 2019-07-15T00:00:00+00:00, 實際存儲2019-07-15T00:00:00+00:00
總結
- 存儲:es使用UTC時間存儲date類型
- 查詢:查詢的時間會轉換成UTC時間,然后進行查詢操作
使用建議
- 強制:存入es的日期類型數據,必須全部帶上時區信息或者使用時間戳(可視化效果差,不建議)
- 強制:使用日期聚合時,必須帶上時區信息
- 強制:查詢日期類型數據是,必須帶上時區信息,或者時間戳(可視化效果差,不建議)
索引中的文檔存在跨時區,那么索引,聚合,查詢時都需要帶上時區信息
索引中的文檔不存在跨時區,即全在同一時區,那么索引,聚合,查詢時都不需要帶上時區信息
總結
以上是生活随笔為你收集整理的Elasticsearch时区问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (半)自动批量添加QQ好友
- 下一篇: SI/PI仿真概述:有源高速信号、阻抗、