rollover-index.asciidoc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 must point to a write index for
  6. a Rollover request to be valid. There are two ways this can be achieved, and depending on the configuration, the
  7. alias metadata will be updated differently. The two scenarios are as follows:
  8. - The alias only points to a single index with `is_write_index` not configured (defaults to `null`).
  9. In this scenario, the original index will have their rollover alias will be added to the newly created index, and removed
  10. from the original (rolled-over) index.
  11. - The alias points to one or more indices with `is_write_index` set to `true` on the index to be rolled over (the write index).
  12. In this scenario, the write index will have its rollover alias' `is_write_index` set to `false`, while the newly created index
  13. will now have the rollover alias pointing to it as the write index with `is_write_index` as `true`.
  14. [source,js]
  15. --------------------------------------------------
  16. PUT /logs-000001 <1>
  17. {
  18. "aliases": {
  19. "logs_write": {}
  20. }
  21. }
  22. # Add > 1000 documents to logs-000001
  23. POST /logs_write/_rollover <2>
  24. {
  25. "conditions": {
  26. "max_age": "7d",
  27. "max_docs": 1000,
  28. "max_size": "5gb"
  29. }
  30. }
  31. --------------------------------------------------
  32. // CONSOLE
  33. // TEST[setup:huge_twitter]
  34. // TEST[s/# Add > 1000 documents to logs-000001/POST _reindex?refresh\n{"source":{"index":"twitter"},"dest":{"index":"logs-000001"}}/]
  35. <1> Creates an index called `logs-0000001` with the alias `logs_write`.
  36. <2> If the index pointed to by `logs_write` was created 7 or more days ago, or
  37. contains 1,000 or more documents, or has an index size at least around 5GB, then the `logs-000002` index is created
  38. and the `logs_write` alias is updated to point to `logs-000002`.
  39. The above request might return the following response:
  40. [source,js]
  41. --------------------------------------------------
  42. {
  43. "acknowledged": true,
  44. "shards_acknowledged": true,
  45. "old_index": "logs-000001",
  46. "new_index": "logs-000002",
  47. "rolled_over": true, <1>
  48. "dry_run": false, <2>
  49. "conditions": { <3>
  50. "[max_age: 7d]": false,
  51. "[max_docs: 1000]": true,
  52. "[max_size: 5gb]": false,
  53. }
  54. }
  55. --------------------------------------------------
  56. // TESTRESPONSE
  57. <1> Whether the index was rolled over.
  58. <2> Whether the rollover was dry run.
  59. <3> The result of each condition.
  60. [float]
  61. === Naming the new index
  62. If the name of the existing index ends with `-` and a number -- e.g.
  63. `logs-000001` -- then the name of the new index will follow the same pattern,
  64. incrementing the number (`logs-000002`). The number is zero-padded with a length
  65. of 6, regardless of the old index name.
  66. If the old name doesn't match this pattern then you must specify the name for
  67. the new index as follows:
  68. [source,js]
  69. --------------------------------------------------
  70. POST /my_alias/_rollover/my_new_index_name
  71. {
  72. "conditions": {
  73. "max_age": "7d",
  74. "max_docs": 1000,
  75. "max_size": "5gb"
  76. }
  77. }
  78. --------------------------------------------------
  79. // CONSOLE
  80. // TEST[s/^/PUT my_old_index_name\nPUT my_old_index_name\/_alias\/my_alias\n/]
  81. [float]
  82. === Using date math with the rollover API
  83. It can be useful to use <<date-math-index-names,date math>> to name the
  84. rollover index according to the date that the index rolled over, e.g.
  85. `logstash-2016.02.03`. The rollover API supports date math, but requires the
  86. index name to end with a dash followed by a number, e.g.
  87. `logstash-2016.02.03-1` which is incremented every time the index is rolled
  88. over. For instance:
  89. [source,js]
  90. --------------------------------------------------
  91. # PUT /<logs-{now/d}-1> with URI encoding:
  92. PUT /%3Clogs-%7Bnow%2Fd%7D-1%3E <1>
  93. {
  94. "aliases": {
  95. "logs_write": {}
  96. }
  97. }
  98. PUT logs_write/_doc/1
  99. {
  100. "message": "a dummy log"
  101. }
  102. POST logs_write/_refresh
  103. # Wait for a day to pass
  104. POST /logs_write/_rollover <2>
  105. {
  106. "conditions": {
  107. "max_docs": "1"
  108. }
  109. }
  110. --------------------------------------------------
  111. // CONSOLE
  112. // TEST[s/now/2016.10.31||/]
  113. <1> Creates an index named with today's date (e.g.) `logs-2016.10.31-1`
  114. <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
  115. //////////////////////////
  116. [source,js]
  117. --------------------------------------------------
  118. GET _alias
  119. --------------------------------------------------
  120. // CONSOLE
  121. // TEST[continued]
  122. [source,js]
  123. --------------------------------------------------
  124. {
  125. "logs-2016.10.31-000002": {
  126. "aliases": {
  127. "logs_write": {}
  128. }
  129. },
  130. "logs-2016.10.31-1": {
  131. "aliases": {}
  132. }
  133. }
  134. --------------------------------------------------
  135. // TESTRESPONSE
  136. //////////////////////////
  137. These indices can then be referenced as described in the
  138. <<date-math-index-names,date math documentation>>. For example, to search
  139. over indices created in the last three days, you could do the following:
  140. [source,js]
  141. --------------------------------------------------
  142. # GET /<logs-{now/d}-*>,<logs-{now/d-1d}-*>,<logs-{now/d-2d}-*>/_search
  143. GET /%3Clogs-%7Bnow%2Fd%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-1d%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-2d%7D-*%3E/_search
  144. --------------------------------------------------
  145. // CONSOLE
  146. // TEST[continued]
  147. // TEST[s/now/2016.10.31||/]
  148. [float]
  149. === Defining the new index
  150. The settings, mappings, and aliases for the new index are taken from any
  151. matching <<indices-templates,index templates>>. Additionally, you can specify
  152. `settings`, `mappings`, and `aliases` in the body of the request, just like the
  153. <<indices-create-index,create index>> API. Values specified in the request
  154. override any values set in matching index templates. For example, the following
  155. `rollover` request overrides the `index.number_of_shards` setting:
  156. [source,js]
  157. --------------------------------------------------
  158. PUT /logs-000001
  159. {
  160. "aliases": {
  161. "logs_write": {}
  162. }
  163. }
  164. POST /logs_write/_rollover
  165. {
  166. "conditions" : {
  167. "max_age": "7d",
  168. "max_docs": 1000,
  169. "max_size": "5gb"
  170. },
  171. "settings": {
  172. "index.number_of_shards": 2
  173. }
  174. }
  175. --------------------------------------------------
  176. // CONSOLE
  177. [float]
  178. === Dry run
  179. The rollover API supports `dry_run` mode, where request conditions can be
  180. checked without performing the actual rollover:
  181. [source,js]
  182. --------------------------------------------------
  183. PUT /logs-000001
  184. {
  185. "aliases": {
  186. "logs_write": {}
  187. }
  188. }
  189. POST /logs_write/_rollover?dry_run
  190. {
  191. "conditions" : {
  192. "max_age": "7d",
  193. "max_docs": 1000,
  194. "max_size": "5gb"
  195. }
  196. }
  197. --------------------------------------------------
  198. // CONSOLE
  199. [float]
  200. === Wait For Active Shards
  201. Because the rollover operation creates a new index to rollover to, the
  202. <<create-index-wait-for-active-shards,`wait_for_active_shards`>> setting on
  203. index creation applies to the rollover action as well.
  204. [[indices-rollover-is-write-index]]
  205. [float]
  206. === Write Index Alias Behavior
  207. The rollover alias when rolling over a write index that has `is_write_index` explicitly set to `true` is not
  208. swapped during rollover actions. Since having an alias point to multiple indices is ambiguous in distinguishing
  209. which is the correct write index to roll over, it is not valid to rollover an alias that points to multiple indices.
  210. For this reason, the default behavior is to swap which index is being pointed to by the write-oriented alias. This
  211. was `logs_write` in some of the above examples. Since setting `is_write_index` enables an alias to point to multiple indices
  212. while also being explicit as to which is the write index that rollover should target, removing the alias from the rolled over
  213. index is not necessary. This simplifies things by allowing for one alias to behave both as the write and read aliases for
  214. indices that are being managed with Rollover.
  215. Look at the behavior of the aliases in the following example where `is_write_index` is set on the rolled over index.
  216. [source,js]
  217. --------------------------------------------------
  218. PUT my_logs_index-000001
  219. {
  220. "aliases": {
  221. "logs": { "is_write_index": true } <1>
  222. }
  223. }
  224. PUT logs/_doc/1
  225. {
  226. "message": "a dummy log"
  227. }
  228. POST logs/_refresh
  229. POST /logs/_rollover
  230. {
  231. "conditions": {
  232. "max_docs": "1"
  233. }
  234. }
  235. PUT logs/_doc/2 <2>
  236. {
  237. "message": "a newer log"
  238. }
  239. --------------------------------------------------
  240. // CONSOLE
  241. <1> configures `my_logs_index` as the write index for the `logs` alias
  242. <2> newly indexed documents against the `logs` alias will write to the new index
  243. [source,js]
  244. --------------------------------------------------
  245. {
  246. "_index" : "my_logs_index-000002",
  247. "_type" : "_doc",
  248. "_id" : "2",
  249. "_version" : 1,
  250. "result" : "created",
  251. "_shards" : {
  252. "total" : 2,
  253. "successful" : 1,
  254. "failed" : 0
  255. },
  256. "_seq_no" : 0,
  257. "_primary_term" : 1
  258. }
  259. --------------------------------------------------
  260. // TESTRESPONSE
  261. //////////////////////////
  262. [source,js]
  263. --------------------------------------------------
  264. GET _alias
  265. --------------------------------------------------
  266. // CONSOLE
  267. // TEST[continued]
  268. //////////////////////////
  269. After the rollover, the alias metadata for the two indices will have the `is_write_index` setting
  270. reflect each index's role, with the newly created index as the write index.
  271. [source,js]
  272. --------------------------------------------------
  273. {
  274. "my_logs_index-000002": {
  275. "aliases": {
  276. "logs": { "is_write_index": true }
  277. }
  278. },
  279. "my_logs_index-000001": {
  280. "aliases": {
  281. "logs": { "is_write_index" : false }
  282. }
  283. }
  284. }
  285. --------------------------------------------------
  286. // TESTRESPONSE