ttl-field.asciidoc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. [[mapping-ttl-field]]
  2. === `_ttl` field
  3. deprecated[2.0.0,The current `_ttl` implementation is deprecated and will be replaced with a different implementation in a future version]
  4. Some types of documents, such as session data or special offers, come with an
  5. expiration date. The `_ttl` field allows you to specify the minimum time a
  6. document should live, after which time the document is deleted automatically.
  7. [TIP]
  8. .Prefer index-per-timeframe to TTL
  9. ======================================================
  10. With TTL , expired documents first have to be marked as deleted then later
  11. purged from the index when segments are merged. For append-only time-based
  12. data such as log events, it is much more efficient to use an index-per-day /
  13. week / month instead of TTLs. Old log data can be removed by simply deleting
  14. old indices.
  15. ======================================================
  16. The `_ttl` field may be enabled as follows:
  17. [source,js]
  18. -------------------------------
  19. PUT my_index
  20. {
  21. "mappings": {
  22. "my_type": {
  23. "_ttl": {
  24. "enabled": true
  25. }
  26. }
  27. }
  28. }
  29. PUT my_index/my_type/1?ttl=10m <1>
  30. {
  31. "text": "Will expire in 10 minutes"
  32. }
  33. PUT my_index/my_type/2 <2>
  34. {
  35. "text": "Will not expire"
  36. }
  37. -------------------------------
  38. // AUTOSENSE
  39. <1> This document will expire 10 minutes after being indexed.
  40. <2> This document has no TTL set and will not expire.
  41. The expiry time is calculated as the value of the
  42. <<mapping-timestamp-field,`_timestamp`>> field (or `now()` if the `_timestamp`
  43. is not enabled) plus the `ttl` specified in the indexing request.
  44. ==== Default TTL
  45. You can provide a default `_ttl`, which will be applied to indexing requests where the `ttl` is not specified:
  46. [source,js]
  47. -------------------------------
  48. PUT my_index
  49. {
  50. "mappings": {
  51. "my_type": {
  52. "_ttl": {
  53. "enabled": true,
  54. "default": "5m"
  55. }
  56. }
  57. }
  58. }
  59. PUT my_index/my_type/1?ttl=10m <1>
  60. {
  61. "text": "Will expire in 10 minutes"
  62. }
  63. PUT my_index/my_type/2 <2>
  64. {
  65. "text": "Will expire in 5 minutes"
  66. }
  67. -------------------------------
  68. // AUTOSENSE
  69. <1> This document will expire 10 minutes after being indexed.
  70. <2> This document has no TTL set and so will expire after the default 5 minutes.
  71. The `default` value can use <<time-units,time units>> like `d` for days, and
  72. will use `ms` as the default unit if no time unit is provided.
  73. You can dynamically update the `default` value using the put mapping
  74. API. It won't change the `_ttl` of already indexed documents but will be
  75. used for future documents.
  76. ==== Note on documents expiration
  77. Expired documents will be automatically deleted periodically. The following
  78. settings control the expiry process:
  79. `indices.ttl.interval`::
  80. How often the purge process should run. Defaults to `60s`. Expired documents
  81. may still be retrieved before they are purged.
  82. `indices.ttl.bulk_size`::
  83. How many deletions are handled by a single <<docs-bulk,`bulk`>> request. The
  84. default value is `10000`.
  85. ==== Note on `detect_noop`
  86. If an update tries to update just the `_ttl` without changing the `_source` of
  87. the document it's expiration time won't be updated if `detect_noop` is `true`.
  88. In 2.1 `detect_noop` defaults to `true`.