2013-08-28 19:24:34 -04:00
|
|
|
[[mapping-timestamp-field]]
|
2015-07-19 19:24:29 -04:00
|
|
|
=== `_timestamp` field
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2016-01-26 05:56:36 -05:00
|
|
|
deprecated[2.0.0,The `_timestamp` field is deprecated. Instead, use a normal <<date,`date`>> field and set its value explicitly]
|
|
|
|
|
2015-07-19 19:24:29 -04:00
|
|
|
The `_timestamp` field, when enabled, allows a timestamp to be indexed and
|
|
|
|
stored with a document. The timestamp may be specified manually, generated
|
|
|
|
automatically, or set to a default value:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
2015-07-19 19:24:29 -04:00
|
|
|
------------------------------------
|
|
|
|
PUT my_index
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2015-07-19 19:24:29 -04:00
|
|
|
"mappings": {
|
|
|
|
"my_type": {
|
|
|
|
"_timestamp": { <1>
|
|
|
|
"enabled": true
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2015-07-19 19:24:29 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
|
2015-07-19 19:24:29 -04:00
|
|
|
PUT my_index/my_type/1?timestamp=2015-01-01 <2>
|
|
|
|
{ "text": "Timestamp as a formatted date" }
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2015-07-19 19:24:29 -04:00
|
|
|
PUT my_index/my_type/2?timestamp=1420070400000 <3>
|
|
|
|
{ "text": "Timestamp as milliseconds since the epoch" }
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2015-07-19 19:24:29 -04:00
|
|
|
PUT my_index/my_type/3 <4>
|
|
|
|
{ "text": "Autogenerated timestamp set to now()" }
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2015-07-19 19:24:29 -04:00
|
|
|
------------------------------------
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2014-07-25 09:14:31 -04:00
|
|
|
|
2015-07-19 19:24:29 -04:00
|
|
|
<1> Enable the `_timestamp` field with default settings.
|
|
|
|
<2> Set the timestamp manually with a formatted date.
|
|
|
|
<3> Set the timestamp with milliseconds since the epoch.
|
|
|
|
<4> Auto-generates a timestamp with <<date-math,now()>>.
|
2014-07-25 09:14:31 -04:00
|
|
|
|
2015-07-19 19:24:29 -04:00
|
|
|
The behaviour of the `_timestamp` field can be configured with the following parameters:
|
2014-07-25 09:14:31 -04:00
|
|
|
|
2015-07-19 19:24:29 -04:00
|
|
|
`default`::
|
2014-07-25 09:14:31 -04:00
|
|
|
|
2015-07-19 19:24:29 -04:00
|
|
|
A default value to be used if none is provided. Defaults to <<date-math,now()>>.
|
2014-07-25 09:14:31 -04:00
|
|
|
|
2015-07-19 19:24:29 -04:00
|
|
|
`format`::
|
2014-07-25 09:14:31 -04:00
|
|
|
|
2015-07-19 19:24:29 -04:00
|
|
|
The <<mapping-date-format,date format>> (or formats) to use when parsing timestamps. Defaults to `epoch_millis||strictDateOptionalTime`.
|
|
|
|
|
|
|
|
`ignore_missing`::
|
|
|
|
|
|
|
|
If `true` (default), replace missing timestamps with the `default` value. If `false`, throw an exception.
|
|
|
|
|
|
|
|
|
|
|
|
The value of the `_timestamp` field is accessible in queries, aggregations, scripts,
|
|
|
|
and when sorting:
|
2014-07-25 09:14:31 -04:00
|
|
|
|
|
|
|
[source,js]
|
2015-07-19 19:24:29 -04:00
|
|
|
--------------------------
|
|
|
|
GET my_index/_search
|
2014-07-25 09:14:31 -04:00
|
|
|
{
|
2015-07-19 19:24:29 -04:00
|
|
|
"query": {
|
|
|
|
"range": {
|
|
|
|
"_timestamp": { <1>
|
|
|
|
"gte": "2015-01-01"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"aggs": {
|
|
|
|
"Timestamps": {
|
|
|
|
"terms": {
|
|
|
|
"field": "_timestamp", <2>
|
|
|
|
"size": 10
|
|
|
|
}
|
2014-07-25 09:14:31 -04:00
|
|
|
}
|
2015-07-19 19:24:29 -04:00
|
|
|
},
|
|
|
|
"sort": [
|
|
|
|
{
|
|
|
|
"_timestamp": { <3>
|
|
|
|
"order": "desc"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"script_fields": {
|
|
|
|
"Timestamp": {
|
|
|
|
"script": "doc['_timestamp']" <4>
|
|
|
|
}
|
|
|
|
}
|
2014-07-25 09:14:31 -04:00
|
|
|
}
|
2015-07-19 19:24:29 -04:00
|
|
|
--------------------------
|
2016-05-09 09:42:23 -04:00
|
|
|
// CONSOLE
|
2016-04-29 10:42:03 -04:00
|
|
|
// TEST[continued]
|
2014-12-30 11:32:50 -05:00
|
|
|
|
2015-07-19 19:24:29 -04:00
|
|
|
<1> Querying on the `_timestamp` field
|
|
|
|
<2> Aggregating on the `_timestamp` field
|
|
|
|
<3> Sorting on the `_timestamp` field
|
|
|
|
<4> Accessing the `_timestamp` field in scripts (inline scripts must be <<enable-dynamic-scripting,enabled>> for this example to work)
|