set-up-lifecycle-policy.asciidoc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[set-up-lifecycle-policy]]
  4. == Set up {ilm} policy
  5. beta[]
  6. In order for an index to use an {ilm} policy to manage its lifecycle we must
  7. first define a lifecycle policy for it to use. The following request creates
  8. a policy called `my_policy` in Elasticsearch which we can later use to manage
  9. our indexes.
  10. [source,js]
  11. ------------------------
  12. PUT _ilm/policy/my_policy
  13. {
  14. "policy": {
  15. "phases": {
  16. "hot": {
  17. "actions": {
  18. "rollover": {
  19. "max_size": "25GB" <1>
  20. }
  21. }
  22. },
  23. "delete": {
  24. "min_age": "30d",
  25. "actions": {
  26. "delete": {} <2>
  27. }
  28. }
  29. }
  30. }
  31. }
  32. ------------------------
  33. // CONSOLE
  34. <1> Rollover the index when it reaches 25GB in size
  35. <2> Delete the index when its 30 days old
  36. {ilm} will manage an index using the policy defined in the
  37. `index.lifecycle.name` index setting. If this setting does not exist in the
  38. settings for a particular index {ilm} will not manage that index.
  39. To set the policy for an index there are two options:
  40. 1. Apply the policy to an index template and bootstrap creating the first index
  41. 2. Apply the policy to a new index in a create index request
  42. [[applying-policy-to-template]]
  43. === Applying a policy to an index template
  44. beta[]
  45. The `index.lifecycle.name` setting can be set in an index template so that it
  46. is automatically applied to indexes matching the templates index pattern:
  47. [source,js]
  48. -----------------------
  49. PUT _template/my_template
  50. {
  51. "index_patterns": ["test-*"], <1>
  52. "settings": {
  53. "number_of_shards": 1,
  54. "number_of_replicas": 1,
  55. "index.lifecycle.name": "my_policy", <2>
  56. "index.lifecycle.rollover_alias": "test-alias"
  57. }
  58. }
  59. -----------------------
  60. // CONSOLE
  61. <1> This template will be applied to all indexes which have a name starting
  62. with `test-`
  63. <2> The template will set the policy to be used to `my_policy`
  64. Now that a policy exists and is used in an index template we can create an
  65. initial index which will be managed by our policy:
  66. [source,js]
  67. -----------------------
  68. PUT test-000001
  69. {
  70. "aliases": {
  71. "test-alias":{
  72. "is_write_index": true <1>
  73. }
  74. }
  75. }
  76. -----------------------
  77. // CONSOLE
  78. <1> Set this initial index to be the write index for this alias.
  79. We can now write data to the `test-alias` alias. Because we have a rollover
  80. action defined in our policy when the index grows larger than 25GB {ilm} will
  81. create a new index and roll the alias over to use the new index automatically.
  82. === Apply a policy to a create index request
  83. beta[]
  84. The `index.lifecycle.name` setting can be set on an individual create index
  85. request so {ilm} immediately starts managing the index:
  86. [source,js]
  87. -----------------------
  88. PUT test-index
  89. {
  90. "settings": {
  91. "number_of_shards": 1,
  92. "number_of_replicas": 1,
  93. "index.lifecycle.name": "my_policy"
  94. }
  95. }
  96. -----------------------
  97. // CONSOLE
  98. IMPORTANT: Its recommended not to use the create index API with a policy that
  99. defines a rollover action. If you do so, the new index as the result of the
  100. rollover will not carry forward the policy. Always use index templates to
  101. define policies with rollover actions.