rollover-index.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. [[indices-rollover-index]]
  2. == Rollover Index
  3. The rollover index API rolls an alias over to a new index when the existing
  4. index is considered to be too large or too old.
  5. The API accepts a single alias name and a list of `conditions`. The alias
  6. must point to a single index only. If the index satisfies the specified
  7. conditions then a new index is created and the alias is switched to point to
  8. the new alias.
  9. [source,js]
  10. --------------------------------------------------
  11. PUT /logs-000001 <1>
  12. {
  13. "aliases": {
  14. "logs_write": {}
  15. }
  16. }
  17. # Add > 1000 documents to logs-000001
  18. POST /logs_write/_rollover <2>
  19. {
  20. "conditions": {
  21. "max_age": "7d",
  22. "max_docs": 1000
  23. }
  24. }
  25. --------------------------------------------------
  26. // CONSOLE
  27. // TEST[setup:huge_twitter]
  28. // TEST[s/# Add > 1000 documents to logs-000001/POST _reindex?refresh\n{"source":{"index":"twitter"},"dest":{"index":"logs-000001"}}/]
  29. <1> Creates an index called `logs-0000001` with the alias `logs_write`.
  30. <2> If the index pointed to by `logs_write` was created 7 or more days ago, or
  31. contains 1,000 or more documents, then the `logs-000002` index is created
  32. and the `logs_write` alias is updated to point to `logs-000002`.
  33. The above request might return the following response:
  34. [source,js]
  35. --------------------------------------------------
  36. {
  37. "acknowledged": true,
  38. "shards_acknowledged": true,
  39. "old_index": "logs-000001",
  40. "new_index": "logs-000002",
  41. "rolled_over": true, <1>
  42. "dry_run": false, <2>
  43. "conditions": { <3>
  44. "[max_age: 7d]": false,
  45. "[max_docs: 1000]": true
  46. }
  47. }
  48. --------------------------------------------------
  49. // TESTRESPONSE
  50. <1> Whether the index was rolled over.
  51. <2> Whether the rollover was dry run.
  52. <3> The result of each condition.
  53. [float]
  54. === Naming the new index
  55. If the name of the existing index ends with `-` and a number -- e.g.
  56. `logs-000001` -- then the name of the new index will follow the same pattern,
  57. incrementing the number (`logs-000002`). The number is zero-padded with a length
  58. of 6, regardless of the old index name.
  59. If the old name doesn't match this pattern then you must specify the name for
  60. the new index as follows:
  61. [source,js]
  62. --------------------------------------------------
  63. POST /my_alias/_rollover/my_new_index_name
  64. {
  65. "conditions": {
  66. "max_age": "7d",
  67. "max_docs": 1000
  68. }
  69. }
  70. --------------------------------------------------
  71. // CONSOLE
  72. // TEST[s/^/PUT my_old_index_name\nPUT my_old_index_name\/_alias\/my_alias\n/]
  73. [float]
  74. === Using date math with the rolllover API
  75. It can be useful to use <<date-math-index-names,date math>> to name the
  76. rollover index according to the date that the index rolled over, e.g.
  77. `logstash-2016.02.03`. The rollover API supports date math, but requires the
  78. index name to end with a dash followed by a number, e.g.
  79. `logstash-2016.02.03-1` which is incremented every time the index is rolled
  80. over. For instance:
  81. [source,js]
  82. --------------------------------------------------
  83. PUT /<logs-{now/d}-1> <1>
  84. {
  85. "aliases": {
  86. "logs_write": {}
  87. }
  88. }
  89. PUT logs_write/log/1
  90. {
  91. "message": "a dummy log"
  92. }
  93. # Wait for a day to pass
  94. POST /logs_write/_rollover <2>
  95. {
  96. "conditions": {
  97. "max_docs": "1"
  98. }
  99. }
  100. --------------------------------------------------
  101. // CONSOLE
  102. // TEST[s/\{now\//{2016.10.31||%2f/]
  103. <1> Creates an index named with today's date (e.g.) `logs-2016.10.31-1`
  104. <2> Rolls over to a new index with today's date, e.g. `logs-2016.10.31-000002` if run immediately, or `logs-2016.11.01-000002` if run after 24 hours
  105. //////////////////////////
  106. [source,js]
  107. --------------------------------------------------
  108. GET _alias
  109. --------------------------------------------------
  110. // CONSOLE
  111. // TEST[continued]
  112. [source,js]
  113. --------------------------------------------------
  114. {
  115. "logs-2016.10.31-000002": {
  116. "aliases": {
  117. "logs_write": {}
  118. }
  119. },
  120. "logs-2016.10.31-1": {
  121. "aliases": {}
  122. }
  123. }
  124. --------------------------------------------------
  125. // TESTRESPONSE
  126. //////////////////////////
  127. These indices can then be referenced as described in the
  128. <<date-math-index-names,date math documentation>>. For example, to search
  129. over indices created in the last three days, you could do the following:
  130. [source,js]
  131. --------------------------------------------------
  132. GET /<logs-{now/d}-*>,<logs-{now/d-1d}-*>,<logs-{now/d-2d}-*>/_search
  133. --------------------------------------------------
  134. // CONSOLE
  135. // TEST[continued]
  136. // TEST[s/now\//now%2f/]
  137. [float]
  138. === Defining the new index
  139. The settings, mappings, and aliases for the new index are taken from any
  140. matching <<indices-templates,index templates>>. Additionally, you can specify
  141. `settings`, `mappings`, and `aliases` in the body of the request, just like the
  142. <<indices-create-index,create index>> API. Values specified in the request
  143. override any values set in matching index templates. For example, the following
  144. `rollover` request overrides the `index.number_of_shards` setting:
  145. [source,js]
  146. --------------------------------------------------
  147. PUT /logs-000001
  148. {
  149. "aliases": {
  150. "logs_write": {}
  151. }
  152. }
  153. POST /logs_write/_rollover
  154. {
  155. "conditions" : {
  156. "max_age": "7d",
  157. "max_docs": 1000
  158. },
  159. "settings": {
  160. "index.number_of_shards": 2
  161. }
  162. }
  163. --------------------------------------------------
  164. // CONSOLE
  165. [float]
  166. === Dry run
  167. The rollover API supports `dry_run` mode, where request conditions can be
  168. checked without performing the actual rollover:
  169. [source,js]
  170. --------------------------------------------------
  171. PUT /logs-000001
  172. {
  173. "aliases": {
  174. "logs_write": {}
  175. }
  176. }
  177. POST /logs_write/_rollover?dry_run
  178. {
  179. "conditions" : {
  180. "max_age": "7d",
  181. "max_docs": 1000
  182. }
  183. }
  184. --------------------------------------------------
  185. // CONSOLE
  186. [float]
  187. === Wait For Active Shards
  188. Because the rollover operation creates a new index to rollover to, the
  189. <<create-index-wait-for-active-shards,`wait_for_active_shards`>> setting on
  190. index creation applies to the rollover action as well.