Elasticsearch-⽇期数据类型和时区详解
前⾔
本⽂基于elasticsearch7.3.0版本
elasticsearch⽇期数据类型
官⽅⽂档:
在elasticsearch内部,⽇期被转换为UTC(如果指定了时区),并存储为⼀个⾃1997-01-01 00:00:00(GMT)⾄当前时刻所经过的毫秒数
对⽇期的查询在内部转换为这种毫秒数表⽰形式上的范围查询
聚合和存储字段的结果将根据与字段关联的⽇期格式转换回字符串(聚合和存储字段的⽇期将始终以字符串形式呈现,即使最初在JSON⽂档中作为Long提供⽇期也是如此)
没有指定⽇期格式
默认使⽤
"strict_date_optional_time||epoch_millis"
官⽅⽂档:
{
"mappings":{
"properties":{
"publicDate":{
"type":"date",
// 不管publicDate是什么格式, 存储字段始终是字符串形式, 默认格式为yyyy-MM-dd'T'HH:mm:ss.SSSZ "store":true
},
"tag":{
"type":"keyword",
"store":true
}
}
}
}
# 索引⽂档
POST my_date/_doc/1
{
"publicDate":"1576022400000",
"tag":"0时区:2019-12-11T00:00:00.000Z"
}
POST my_date/_doc/2
{
"publicDate":"1576051200000",
"tag":"0时区:2019-12-11T08:00:00.000Z"
}
POST my_date/_doc/3
{
"publicDate":"2019-12-11T08:00:00",
"tag":"0时区:2019-12-11T08:00:00.000Z"
}
POST my_date/_doc/4
{
"publicDate":"2019-12-11T08:00:00+08:00",
"tag":"0时区:2019-12-11T00:00:00.000Z"
}
从_source获取存储的⽂档, 不做任何处理,原样返回
# 查询
GET my_date/_search
{
"query":{
"match_all":{}
}
}
# 结果
{
"took":0,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
"relation":"eq"
},
"max_score":1.0,
"hits":[
{
"_index":"my_date",
"_type":"_doc",
"_id":"1",
"_score":1.0,
"_source":{
"publicDate":"1576022400000",
"tag":"0时区:2019-12-11T00:00:00.000Z"
}
},
{
"_index":"my_date",
"_type":"_doc",
"_id":"2",
"_score":1.0,
"_source":{
"publicDate":"1576051200000",
"tag":"0时区:2019-12-11T08:00:00.000Z"
}
},
{
"_index":"my_date",
"_type":"_doc",
"_id":"3",
"_score":1.0,
"_source":{
"publicDate":"2019-12-11T08:00:00",
"tag":"0时区:2019-12-11T08:00:00.000Z"
}
},
{
"_index":"my_date",
"_type":"_doc",
"_id":"4",
"_score":1.0,
"_source":{
"publicDate":"2019-12-11T08:00:00+08:00",
"tag":"0时区:2019-12-11T00:00:00.000Z"
}
}
]
}
}
从存储字段获取⽂档, ⽇期会被格式成字符串形式
# 查询
GET my_date/_search
{
"stored_fields":["publicDate","tag"],
"query":{
"match_all":{}
}
}
# 结果
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":4,
"relation":"eq"
},
"max_score":1.0,
unix时间戳转换日期格式"hits":[
{
"_index":"my_date",
"_type":"_doc",
"_id":"1",
"_score":1.0,
"fields":{
"publicDate":[
"2019-12-11T00:00:00.000Z"
],
"tag":[
"0时区:2019-12-11T00:00:00.000Z"
]
}
},
{
"_index":"my_date",
"_type":"_doc",
"_id":"2",
"_score":1.0,
"fields":{
"publicDate":[
"2019-12-11T08:00:00.000Z"
],
"tag":[
"0时区:2019-12-11T08:00:00.000Z"
]
}
},
{
"_index":"my_date",
"_type":"_doc",
"_id":"3",
"_score":1.0,
"fields":{
"publicDate":[
"2019-12-11T08:00:00.000Z"
],
"tag":[
"0时区:2019-12-11T08:00:00.000Z"
]
}
},
{
"_index":"my_date",
"_type":"_doc",
"_id":"4",
"_score":1.0,
"fields":{
"publicDate":[
"tag":[
"0时区:2019-12-11T00:00:00.000Z"
]
}
}
]
}
}
聚合⽂档, ⽇期会被格式成字符串形式
# 聚合
GET my_date/_search
{
"size":0,
"aggs":{
"date_aggs":{
"date_histogram":{
"field":"publicDate",
"calendar_interval":"1h",
/
/ >=1数量的桶才会被展⽰, 默认是0, 即都展⽰
"min_doc_count":1
},
"aggs":{
"top_aggs":{
// 展⽰每个桶的前10个
"top_hits":{
"size":10
}
}
}
}
}
}
# 结果
{
"took":12,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":4,
"relation":"eq"
},
"max_score": null,
"hits":[]
},
"aggregations":{
"date_aggs":{
"buckets":[
{
"key_as_string":"2019-12-11T00:00:00.000Z",
"key":1576022400000,
"doc_count":2,
"top_aggs":{
"hits":{
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论