rollover-index.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. [[indices-rollover-index]]
  2. === Rollover index API
  3. ++++
  4. <titleabbrev>Rollover index</titleabbrev>
  5. ++++
  6. Assigns an <<indices-aliases, index alias>> to a new index
  7. when the alias's existing index meets a condition you provide.
  8. [source,console]
  9. ----
  10. POST /alias1/_rollover/twitter
  11. {
  12. "conditions": {
  13. "max_age": "7d",
  14. "max_docs": 1000,
  15. "max_size": "5gb"
  16. }
  17. }
  18. ----
  19. // TEST[s/^/PUT my_old_index_name\nPUT my_old_index_name\/_alias\/alias1\n/]
  20. [[rollover-index-api-request]]
  21. ==== {api-request-title}
  22. `POST /<alias>/_rollover/<target-index>`
  23. `POST /<alias>/_rollover/`
  24. [[rollover-index-api-desc]]
  25. ==== {api-description-title}
  26. The rollover index API rolls an <<indices-aliases, alias>> to a new index when
  27. the existing index meets a condition you provide. You can use this API to retire
  28. an index that becomes too large or too old.
  29. NOTE: To roll over an index, a condition must be met *when you call the API*.
  30. {es} does not monitor the index after you receive an API response. To
  31. automatically roll over indices when a condition is met, you can use {es}'s
  32. <<index-lifecycle-management, index lifecycle management (ILM) policies>>.
  33. The rollover index API accepts a single alias name
  34. and a list of `conditions`.
  35. If the specified alias points to a single index,
  36. the rollover request:
  37. . Creates a new index
  38. . Adds the alias to the new index
  39. . Removes the alias from the original index
  40. If the specified alias points to multiple indices,
  41. one of these indices must have `is_write_index` set to `true`.
  42. In this case,
  43. the rollover request:
  44. . Creates a new index
  45. . Sets `is_write_index` to `true` for the new index
  46. . Sets `is_write_index` to `false` for the original index
  47. [[rollover-wait-active-shards]]
  48. ===== Wait for active shards
  49. Because the rollover operation creates a new index to rollover to, the
  50. <<create-index-wait-for-active-shards,`wait_for_active_shards`>> setting on
  51. index creation applies to the rollover action.
  52. [[rollover-index-api-path-params]]
  53. ==== {api-path-parms-title}
  54. `<alias>`::
  55. (Required, string)
  56. Name of the existing index alias
  57. to assign to the target index.
  58. `<target-index>`::
  59. +
  60. --
  61. (Optional*, string)
  62. Name of the target index to create and assign the index alias.
  63. include::{docdir}/indices/create-index.asciidoc[tag=index-name-reqs]
  64. *This parameter is not required
  65. if the alias is assigned to an index name that ends with `-` and a number,
  66. such as `logs-000001`.
  67. In this case,
  68. the name of the new index follows the same pattern,
  69. incrementing the number.
  70. For example,
  71. `logs-000001` increments to `logs-000002`.
  72. This number is zero-padded with a length of 6,
  73. regardless of the prior index name.
  74. If the existing index for the alias does not match this pattern,
  75. this parameter is required.
  76. --
  77. [[rollover-index-api-query-params]]
  78. ==== {api-query-parms-title}
  79. `dry_run`::
  80. (Optional, boolean)
  81. If `true`,
  82. the request checks whether the index matches provided conditions
  83. but does not perform a rollover.
  84. Defaults to `false`.
  85. include::{docdir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
  86. include::{docdir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  87. [[rollover-index-api-request-body]]
  88. ==== {api-request-body-title}
  89. include::{docdir}/rest-api/common-parms.asciidoc[tag=aliases]
  90. `conditions`::
  91. +
  92. --
  93. (Required, object)
  94. Set of conditions the index alias's existing index must met to roll over.
  95. Parameters include:
  96. `max_age`::
  97. (Optional, <<time-units, time units>>)
  98. Maximum age of the index.
  99. `max_docs`::
  100. (Optional, integer)
  101. Maximum number of documents in the index.
  102. This number does *not* include documents in replica shards.
  103. `max_size`::
  104. (Optional, <<byte-units, byte units>>)
  105. Maximum estimated size of the primary shard of the index.
  106. --
  107. include::{docdir}/rest-api/common-parms.asciidoc[tag=mappings]
  108. include::{docdir}/rest-api/common-parms.asciidoc[tag=settings]
  109. [[rollover-index-api-example]]
  110. ==== {api-examples-title}
  111. [[rollover-index-basic-ex]]
  112. ===== Basic example
  113. [source,console]
  114. --------------------------------------------------
  115. PUT /logs-000001 <1>
  116. {
  117. "aliases": {
  118. "logs_write": {}
  119. }
  120. }
  121. # Add > 1000 documents to logs-000001
  122. POST /logs_write/_rollover <2>
  123. {
  124. "conditions": {
  125. "max_age": "7d",
  126. "max_docs": 1000,
  127. "max_size": "5gb"
  128. }
  129. }
  130. --------------------------------------------------
  131. // TEST[setup:huge_twitter]
  132. // TEST[s/# Add > 1000 documents to logs-000001/POST _reindex?refresh\n{"source":{"index":"twitter"},"dest":{"index":"logs-000001"}}/]
  133. <1> Creates an index called `logs-0000001` with the alias `logs_write`.
  134. <2> If the index pointed to by `logs_write` was created 7 or more days ago, or
  135. contains 1,000 or more documents, or has an index size at least around 5GB, then the `logs-000002` index is created
  136. and the `logs_write` alias is updated to point to `logs-000002`.
  137. The API returns the following response:
  138. [source,console-result]
  139. --------------------------------------------------
  140. {
  141. "acknowledged": true,
  142. "shards_acknowledged": true,
  143. "old_index": "logs-000001",
  144. "new_index": "logs-000002",
  145. "rolled_over": true, <1>
  146. "dry_run": false, <2>
  147. "conditions": { <3>
  148. "[max_age: 7d]": false,
  149. "[max_docs: 1000]": true,
  150. "[max_size: 5gb]": false,
  151. }
  152. }
  153. --------------------------------------------------
  154. <1> Whether the index was rolled over.
  155. <2> Whether the rollover was dry run.
  156. <3> The result of each condition.
  157. [[rollover-index-settings-ex]]
  158. ===== Specify settings for the target index
  159. The settings, mappings, and aliases for the new index are taken from any
  160. matching <<indices-templates,index templates>>. Additionally, you can specify
  161. `settings`, `mappings`, and `aliases` in the body of the request, just like the
  162. <<indices-create-index,create index>> API. Values specified in the request
  163. override any values set in matching index templates. For example, the following
  164. `rollover` request overrides the `index.number_of_shards` setting:
  165. [source,console]
  166. --------------------------------------------------
  167. PUT /logs-000001
  168. {
  169. "aliases": {
  170. "logs_write": {}
  171. }
  172. }
  173. POST /logs_write/_rollover
  174. {
  175. "conditions" : {
  176. "max_age": "7d",
  177. "max_docs": 1000,
  178. "max_size": "5gb"
  179. },
  180. "settings": {
  181. "index.number_of_shards": 2
  182. }
  183. }
  184. --------------------------------------------------
  185. [[rollover-index-specify-index-ex]]
  186. ===== Specify a target index name
  187. If the name of the existing index ends with `-` and a number -- e.g.
  188. `logs-000001` -- then the name of the new index will follow the same pattern,
  189. incrementing the number (`logs-000002`). The number is zero-padded with a length
  190. of 6, regardless of the old index name.
  191. If the old name doesn't match this pattern then you must specify the name for
  192. the new index as follows:
  193. [source,console]
  194. --------------------------------------------------
  195. POST /my_alias/_rollover/my_new_index_name
  196. {
  197. "conditions": {
  198. "max_age": "7d",
  199. "max_docs": 1000,
  200. "max_size": "5gb"
  201. }
  202. }
  203. --------------------------------------------------
  204. // TEST[s/^/PUT my_old_index_name\nPUT my_old_index_name\/_alias\/my_alias\n/]
  205. [[_using_date_math_with_the_rollover_api]]
  206. ===== Use date math with a rollover
  207. It can be useful to use <<date-math-index-names,date math>> to name the
  208. rollover index according to the date that the index rolled over, e.g.
  209. `logstash-2016.02.03`. The rollover API supports date math, but requires the
  210. index name to end with a dash followed by a number, e.g.
  211. `logstash-2016.02.03-1` which is incremented every time the index is rolled
  212. over. For instance:
  213. [source,console]
  214. --------------------------------------------------
  215. # PUT /<logs-{now/d}-1> with URI encoding:
  216. PUT /%3Clogs-%7Bnow%2Fd%7D-1%3E <1>
  217. {
  218. "aliases": {
  219. "logs_write": {}
  220. }
  221. }
  222. PUT logs_write/_doc/1
  223. {
  224. "message": "a dummy log"
  225. }
  226. POST logs_write/_refresh
  227. # Wait for a day to pass
  228. POST /logs_write/_rollover <2>
  229. {
  230. "conditions": {
  231. "max_docs": "1"
  232. }
  233. }
  234. --------------------------------------------------
  235. // TEST[s/now/2016.10.31||/]
  236. <1> Creates an index named with today's date (e.g.) `logs-2016.10.31-1`
  237. <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
  238. //////////////////////////
  239. [source,console]
  240. --------------------------------------------------
  241. GET _alias
  242. --------------------------------------------------
  243. // TEST[continued]
  244. [source,console-result]
  245. --------------------------------------------------
  246. {
  247. "logs-2016.10.31-000002": {
  248. "aliases": {
  249. "logs_write": {}
  250. }
  251. },
  252. "logs-2016.10.31-1": {
  253. "aliases": {}
  254. }
  255. }
  256. --------------------------------------------------
  257. //////////////////////////
  258. These indices can then be referenced as described in the
  259. <<date-math-index-names,date math documentation>>. For example, to search
  260. over indices created in the last three days, you could do the following:
  261. [source,console]
  262. --------------------------------------------------
  263. # GET /<logs-{now/d}-*>,<logs-{now/d-1d}-*>,<logs-{now/d-2d}-*>/_search
  264. GET /%3Clogs-%7Bnow%2Fd%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-1d%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-2d%7D-*%3E/_search
  265. --------------------------------------------------
  266. // TEST[continued]
  267. // TEST[s/now/2016.10.31||/]
  268. [[rollover-index-api-dry-run-ex]]
  269. ===== Dry run
  270. The rollover API supports `dry_run` mode, where request conditions can be
  271. checked without performing the actual rollover.
  272. [source,console]
  273. --------------------------------------------------
  274. POST /logs_write/_rollover?dry_run
  275. {
  276. "conditions" : {
  277. "max_age": "7d",
  278. "max_docs": 1000,
  279. "max_size": "5gb"
  280. }
  281. }
  282. --------------------------------------------------
  283. // TEST[s/^/PUT logs-000001\nPUT logs-000001\/_alias\/logs_write\n/]
  284. [[indices-rollover-is-write-index]]
  285. ===== Roll over a write index
  286. The rollover alias when rolling over a write index that has `is_write_index` explicitly set to `true` is not
  287. swapped during rollover actions. Since having an alias point to multiple indices is ambiguous in distinguishing
  288. which is the correct write index to roll over, it is not valid to rollover an alias that points to multiple indices.
  289. For this reason, the default behavior is to swap which index is being pointed to by the write-oriented alias. This
  290. was `logs_write` in some of the above examples. Since setting `is_write_index` enables an alias to point to multiple indices
  291. while also being explicit as to which is the write index that rollover should target, removing the alias from the rolled over
  292. index is not necessary. This simplifies things by allowing for one alias to behave both as the write and read aliases for
  293. indices that are being managed with Rollover.
  294. Look at the behavior of the aliases in the following example where `is_write_index` is set on the rolled over index.
  295. [source,console]
  296. --------------------------------------------------
  297. PUT my_logs_index-000001
  298. {
  299. "aliases": {
  300. "logs": { "is_write_index": true } <1>
  301. }
  302. }
  303. PUT logs/_doc/1
  304. {
  305. "message": "a dummy log"
  306. }
  307. POST logs/_refresh
  308. POST /logs/_rollover
  309. {
  310. "conditions": {
  311. "max_docs": "1"
  312. }
  313. }
  314. PUT logs/_doc/2 <2>
  315. {
  316. "message": "a newer log"
  317. }
  318. --------------------------------------------------
  319. <1> configures `my_logs_index` as the write index for the `logs` alias
  320. <2> newly indexed documents against the `logs` alias will write to the new index
  321. [source,console-result]
  322. --------------------------------------------------
  323. {
  324. "_index" : "my_logs_index-000002",
  325. "_id" : "2",
  326. "_version" : 1,
  327. "result" : "created",
  328. "_shards" : {
  329. "total" : 2,
  330. "successful" : 1,
  331. "failed" : 0
  332. },
  333. "_seq_no" : 0,
  334. "_primary_term" : 1
  335. }
  336. --------------------------------------------------
  337. //////////////////////////
  338. [source,console]
  339. --------------------------------------------------
  340. GET my_logs_index-000001,my_logs_index-000002/_alias
  341. --------------------------------------------------
  342. // TEST[continued]
  343. //////////////////////////
  344. After the rollover, the alias metadata for the two indices will have the `is_write_index` setting
  345. reflect each index's role, with the newly created index as the write index.
  346. [source,console-result]
  347. --------------------------------------------------
  348. {
  349. "my_logs_index-000002": {
  350. "aliases": {
  351. "logs": { "is_write_index": true }
  352. }
  353. },
  354. "my_logs_index-000001": {
  355. "aliases": {
  356. "logs": { "is_write_index" : false }
  357. }
  358. }
  359. }
  360. --------------------------------------------------