rollover-index.asciidoc 12 KB

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