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,console]
  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. <1> Rollover the index when it reaches 25GB in size
  33. <2> Delete the index when its 30 days old
  34. {ilm} will manage an index using the policy defined in the
  35. `index.lifecycle.name` index setting. If this setting does not exist in the
  36. settings for a particular index, {ilm} will not manage that index.
  37. To set the policy for an index there are two options:
  38. 1. Apply the policy to an index template and bootstrap creating the first index
  39. 2. Apply the policy to a new index in a create index request
  40. [[applying-policy-to-template]]
  41. === Applying a policy to an index template
  42. The `index.lifecycle.name` setting can be set in an index template so that it
  43. is automatically applied to indexes matching the templates index pattern:
  44. [source,console]
  45. -----------------------
  46. PUT _template/my_template
  47. {
  48. "index_patterns": ["test-*"], <1>
  49. "settings": {
  50. "number_of_shards": 1,
  51. "number_of_replicas": 1,
  52. "index.lifecycle.name": "my_policy", <2>
  53. "index.lifecycle.rollover_alias": "test-alias"
  54. }
  55. }
  56. -----------------------
  57. <1> This template will be applied to all indexes which have a name starting
  58. with `test-`
  59. <2> The template will set the policy to be used to `my_policy`
  60. Now that a policy exists and is used in an index template we can create an
  61. initial index which will be managed by our policy:
  62. [source,console]
  63. -----------------------
  64. PUT test-000001
  65. {
  66. "aliases": {
  67. "test-alias":{
  68. "is_write_index": true <1>
  69. }
  70. }
  71. }
  72. -----------------------
  73. <1> Set this initial index to be the write index for this alias.
  74. We can now write data to the `test-alias` alias. Because we have a rollover
  75. action defined in our policy, when the index grows larger than 25GB {ilm} will
  76. create a new index and roll the alias over to use the new index automatically.
  77. === Apply a policy to a create index request
  78. The `index.lifecycle.name` setting can be set on an individual create index
  79. request so {ilm} immediately starts managing the index:
  80. [source,console]
  81. -----------------------
  82. PUT test-index
  83. {
  84. "settings": {
  85. "number_of_shards": 1,
  86. "number_of_replicas": 1,
  87. "index.lifecycle.name": "my_policy"
  88. }
  89. }
  90. -----------------------
  91. IMPORTANT: Do not to use the create index API with a policy that
  92. defines a rollover action. If you do so, the new index as the result of the
  93. rollover will not carry forward the policy. Always use
  94. <<applying-policy-to-template, index templates>> to define policies with rollover
  95. actions.