Date nanoseconds field type
Date nanoseconds field type
This data type is an addition to the date
data type. However there is an important distinction between the two. The existing date
data type stores dates in millisecond resolution. The date_nanos
data type stores dates in nanosecond resolution, which limits its range of dates from roughly 1970 to 2262, as dates are still stored as a long representing nanoseconds since the epoch.
Queries on nanoseconds are internally converted to range queries on this long representation, and the result of aggregations and stored fields is converted back to a string depending on the date format that is associated with the field.
Date formats can be customised, but if no format
is specified then it uses the default:
"strict_date_optional_time_nanos||epoch_millis"
For instance:
resp = client.indices.create(
index="my-index-000001",
mappings={
"properties": {
"date": {
"type": "date_nanos"
}
}
},
)
print(resp)
resp1 = client.bulk(
index="my-index-000001",
refresh=True,
operations=[
{
"index": {
"_id": "1"
}
},
{
"date": "2015-01-01"
},
{
"index": {
"_id": "2"
}
},
{
"date": "2015-01-01T12:10:30.123456789Z"
},
{
"index": {
"_id": "3"
}
},
{
"date": 1420070400000
}
],
)
print(resp1)
resp2 = client.search(
index="my-index-000001",
sort={
"date": "asc"
},
runtime_mappings={
"date_has_nanos": {
"type": "boolean",
"script": "emit(doc['date'].value.nano != 0)"
}
},
fields=[
{
"field": "date",
"format": "strict_date_optional_time_nanos"
},
{
"field": "date_has_nanos"
}
],
)
print(resp2)
response = client.indices.create(
index: 'my-index-000001',
body: {
mappings: {
properties: {
date: {
type: 'date_nanos'
}
}
}
}
)
puts response
response = client.bulk(
index: 'my-index-000001',
refresh: true,
body: [
{
index: {
_id: '1'
}
},
{
date: '2015-01-01'
},
{
index: {
_id: '2'
}
},
{
date: '2015-01-01T12:10:30.123456789Z'
},
{
index: {
_id: '3'
}
},
{
date: 1_420_070_400_000
}
]
)
puts response
response = client.search(
index: 'my-index-000001',
body: {
sort: {
date: 'asc'
},
runtime_mappings: {
date_has_nanos: {
type: 'boolean',
script: "emit(doc['date'].value.nano != 0)"
}
},
fields: [
{
field: 'date',
format: 'strict_date_optional_time_nanos'
},
{
field: 'date_has_nanos'
}
]
}
)
puts response
const response = await client.indices.create({
index: "my-index-000001",
mappings: {
properties: {
date: {
type: "date_nanos",
},
},
},
});
console.log(response);
const response1 = await client.bulk({
index: "my-index-000001",
refresh: "true",
operations: [
{
index: {
_id: "1",
},
},
{
date: "2015-01-01",
},
{
index: {
_id: "2",
},
},
{
date: "2015-01-01T12:10:30.123456789Z",
},
{
index: {
_id: "3",
},
},
{
date: 1420070400000,
},
],
});
console.log(response1);
const response2 = await client.search({
index: "my-index-000001",
sort: {
date: "asc",
},
runtime_mappings: {
date_has_nanos: {
type: "boolean",
script: "emit(doc['date'].value.nano != 0)",
},
},
fields: [
{
field: "date",
format: "strict_date_optional_time_nanos",
},
{
field: "date_has_nanos",
},
],
});
console.log(response2);
PUT my-index-000001
{
"mappings": {
"properties": {
"date": {
"type": "date_nanos"
}
}
}
}
PUT my-index-000001/_bulk?refresh
{ "index" : { "_id" : "1" } }
{ "date": "2015-01-01" }
{ "index" : { "_id" : "2" } }
{ "date": "2015-01-01T12:10:30.123456789Z" }
{ "index" : { "_id" : "3" } }
{ "date": 1420070400000 }
GET my-index-000001/_search
{
"sort": { "date": "asc"},
"runtime_mappings": {
"date_has_nanos": {
"type": "boolean",
"script": "emit(doc['date'].value.nano != 0)"
}
},
"fields": [
{
"field": "date",
"format": "strict_date_optional_time_nanos"
},
{
"field": "date_has_nanos"
}
]
}
The | |
This document uses a plain date. | |
This document includes a time. | |
This document uses milliseconds-since-the-epoch. | |
Note that the | |
Use | |
You can specify the format when fetching data using the fields parameter. Use strict_date_optional_time_nanos or you’ll get a rounded result. |
You can also specify multiple date formats separated by ||
. The same mapping parameters than with the date
field can be used.
Date nanoseconds will accept numbers with a decimal point like {"date": 1618249875.123456}
but there are some cases (#70085) where we’ll lose precision on those dates so they should be avoided.
Limitations
Aggregations are still on millisecond resolution, even when using a date_nanos
field. This limitation also affects transforms.
Synthetic _source
Synthetic _source
is Generally Available only for TSDB indices (indices that have index.mode
set to time_series
). For other indices synthetic _source
is in technical preview. Features in technical preview may be changed or removed in a future release. Elastic will work to fix any issues, but features in technical preview are not subject to the support SLA of official GA features.
Synthetic source may sort date_nanos
field values. For example:
resp = client.indices.create(
index="idx",
settings={
"index": {
"mapping": {
"source": {
"mode": "synthetic"
}
}
}
},
mappings={
"properties": {
"date": {
"type": "date_nanos"
}
}
},
)
print(resp)
resp1 = client.index(
index="idx",
id="1",
document={
"date": [
"2015-01-01T12:10:30.000Z",
"2014-01-01T12:10:30.000Z"
]
},
)
print(resp1)
const response = await client.indices.create({
index: "idx",
settings: {
index: {
mapping: {
source: {
mode: "synthetic",
},
},
},
},
mappings: {
properties: {
date: {
type: "date_nanos",
},
},
},
});
console.log(response);
const response1 = await client.index({
index: "idx",
id: 1,
document: {
date: ["2015-01-01T12:10:30.000Z", "2014-01-01T12:10:30.000Z"],
},
});
console.log(response1);
PUT idx
{
"settings": {
"index": {
"mapping": {
"source": {
"mode": "synthetic"
}
}
}
},
"mappings": {
"properties": {
"date": { "type": "date_nanos" }
}
}
}
PUT idx/_doc/1
{
"date": ["2015-01-01T12:10:30.000Z", "2014-01-01T12:10:30.000Z"]
}
Will become:
{
"date": ["2014-01-01T12:10:30.000Z", "2015-01-01T12:10:30.000Z"]
}