rollover-index.asciidoc 16 KB

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