日期纳秒字段类型

date_nanos 字段类型与 date 字段类型类似,都用于存储日期。然而,date 以毫秒精度存储日期,而 date_nanos 则以纳秒精度存储日期。日期以 long 类型值存储,对应于自纪元以来的纳秒数。因此,支持的日期范围大约为 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_timeepoch_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 字段的文档时,您可以使用 fieldsdocvalue_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
          ]
        }
      }
    ]
  }
}