rollover-index.asciidoc 6.1 KB

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