ttl-field.asciidoc 3.0 KB

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