timestamp-field.asciidoc 2.3 KB

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