set-up-lifecycle-policy.asciidoc 3.1 KB

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