timestamp-field.asciidoc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. [[mapping-timestamp-field]]
  2. === `_timestamp` field
  3. deprecated[2.0.0,The `_timestamp` field is deprecated. Instead, use a normal <<date,`date`>> field and set its value explicitly]
  4. The `_timestamp` field, when enabled, allows a timestamp to be indexed and
  5. stored with a document. The timestamp may be specified manually, generated
  6. automatically, or set to a default value:
  7. [source,js]
  8. ------------------------------------
  9. PUT my_index
  10. {
  11. "mappings": {
  12. "my_type": {
  13. "_timestamp": { <1>
  14. "enabled": true
  15. }
  16. }
  17. }
  18. }
  19. PUT my_index/my_type/1?timestamp=2015-01-01 <2>
  20. { "text": "Timestamp as a formatted date" }
  21. PUT my_index/my_type/2?timestamp=1420070400000 <3>
  22. { "text": "Timestamp as milliseconds since the epoch" }
  23. PUT my_index/my_type/3 <4>
  24. { "text": "Autogenerated timestamp set to now()" }
  25. ------------------------------------
  26. // CONSOLE
  27. <1> Enable the `_timestamp` field with default settings.
  28. <2> Set the timestamp manually with a formatted date.
  29. <3> Set the timestamp with milliseconds since the epoch.
  30. <4> Auto-generates a timestamp with <<date-math,now()>>.
  31. The behaviour of the `_timestamp` field can be configured with the following parameters:
  32. `default`::
  33. A default value to be used if none is provided. Defaults to <<date-math,now()>>.
  34. `format`::
  35. The <<mapping-date-format,date format>> (or formats) to use when parsing timestamps. Defaults to `epoch_millis||strictDateOptionalTime`.
  36. `ignore_missing`::
  37. If `true` (default), replace missing timestamps with the `default` value. If `false`, throw an exception.
  38. The value of the `_timestamp` field is accessible in queries, aggregations, scripts,
  39. and when sorting:
  40. [source,js]
  41. --------------------------
  42. GET my_index/_search
  43. {
  44. "query": {
  45. "range": {
  46. "_timestamp": { <1>
  47. "gte": "2015-01-01"
  48. }
  49. }
  50. },
  51. "aggs": {
  52. "Timestamps": {
  53. "terms": {
  54. "field": "_timestamp", <2>
  55. "size": 10
  56. }
  57. }
  58. },
  59. "sort": [
  60. {
  61. "_timestamp": { <3>
  62. "order": "desc"
  63. }
  64. }
  65. ],
  66. "script_fields": {
  67. "Timestamp": {
  68. "script": "doc['_timestamp']" <4>
  69. }
  70. }
  71. }
  72. --------------------------
  73. // CONSOLE
  74. // TEST[continued]
  75. <1> Querying on the `_timestamp` field
  76. <2> Aggregating on the `_timestamp` field
  77. <3> Sorting on the `_timestamp` field
  78. <4> Accessing the `_timestamp` field in scripts (inline scripts must be <<enable-dynamic-scripting,enabled>> for this example to work)