日期纳秒字段类型
1.0 版引入
date_nanos
字段类型与 date
字段类型类似,都用于存储日期。不同之处在于,date
以毫秒分辨率存储日期,而 date_nanos
以纳秒分辨率存储日期。日期以 long
值形式存储,对应自 epoch 以来的纳秒数。因此,支持的日期范围大约为 1970 年至 2262 年。
对 date_nanos
字段的查询会转换为对其字段值的 long
表示形式的范围查询。然后,存储的字段和聚合结果会使用字段上设置的格式转换为字符串。
date_nanos
字段支持 date
字段支持的所有格式和参数。您可以使用 ||
分隔多个格式。
对于 date_nanos
字段,您可以使用 strict_date_optional_time_nanos
格式来保留纳秒分辨率。如果您在将字段映射为 date_nanos
时未指定格式,则默认格式为 strict_date_optional_time||epoch_millis
,它允许您以 strict_date_optional_time
或 epoch_millis
格式传递值。strict_date_optional_time
格式支持纳秒分辨率的日期,而 epoch_millis
格式仅支持毫秒分辨率的日期。
示例
创建一个映射,其中 date
字段类型为 date_nanos
并具有 strict_date_optional_time_nanos
格式
PUT testindex/_mapping
{
"properties": {
"date": {
"type": "date_nanos",
"format" : "strict_date_optional_time_nanos"
}
}
}
向索引中索引两个文档
PUT testindex/_doc/1
{ "date": "2022-06-15T10:12:52.382719622Z" }
PUT testindex/_doc/2
{ "date": "2022-06-15T10:12:52.382719624Z" }
您可以使用范围查询来搜索日期范围
GET testindex/_search
{
"query": {
"range": {
"date": {
"gte": "2022-06-15T10:12:52.382719621Z",
"lte": "2022-06-15T10:12:52.382719623Z"
}
}
}
}
响应包含日期在指定范围内的文档
{
"took": 43,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "testindex",
"_id": "1",
"_score": 1,
"_source": {
"date": "2022-06-15T10:12:52.382719622Z"
}
}
]
}
}
查询包含 date_nanos
字段的文档时,可以使用 fields
或 docvalue_fields
GET testindex/_search
{
"fields": ["date"]
}
GET testindex/_search
{
"docvalue_fields" : [
{
"field" : "date"
}
]
}
前述任一查询的响应都包含两个已索引的文档
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "testindex",
"_id": "1",
"_score": 1,
"_source": {
"date": "2022-06-15T10:12:52.382719622Z"
},
"fields": {
"date": [
"2022-06-15T10:12:52.382719622Z"
]
}
},
{
"_index": "testindex",
"_id": "2",
"_score": 1,
"_source": {
"date": "2022-06-15T10:12:52.382719624Z"
},
"fields": {
"date": [
"2022-06-15T10:12:52.382719624Z"
]
}
}
]
}
}
您可以按如下方式对 date_nanos
字段进行排序
GET testindex/_search
{
"sort": {
"date": "asc"
}
}
响应包含已排序的文档
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "testindex",
"_id": "1",
"_score": null,
"_source": {
"date": "2022-06-15T10:12:52.382719622Z"
},
"sort": [
1655287972382719700
]
},
{
"_index": "testindex",
"_id": "2",
"_score": null,
"_source": {
"date": "2022-06-15T10:12:52.382719624Z"
},
"sort": [
1655287972382719700
]
}
]
}
}
您还可以使用 Painless 脚本访问字段的纳秒部分
GET testindex/_search
{
"script_fields" : {
"my_field" : {
"script" : {
"lang" : "painless",
"source" : "doc['date'].value.nano"
}
}
}
}
响应仅包含字段的纳秒部分
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "testindex",
"_id": "1",
"_score": 1,
"fields": {
"my_field": [
382719622
]
}
},
{
"_index": "testindex",
"_id": "2",
"_score": 1,
"fields": {
"my_field": [
382719624
]
}
}
]
}
}