rollover-index.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. [[indices-rollover-index]]
  2. === Rollover API
  3. ++++
  4. <titleabbrev>Rollover</titleabbrev>
  5. ++++
  6. Creates a new index for a <<data-streams,data stream>> or <<aliases,index alias>>.
  7. [source,console]
  8. ----
  9. POST my-data-stream/_rollover
  10. ----
  11. // TEST[setup:my_data_stream]
  12. // TEST[teardown:data_stream_cleanup]
  13. [[rollover-index-api-request]]
  14. ==== {api-request-title}
  15. `POST /<rollover-target>/_rollover/`
  16. `POST /<rollover-target>/_rollover/<target-index>`
  17. [[rollover-index-api-prereqs]]
  18. ==== {api-prereq-title}
  19. * If the {es} {security-features} are enabled, you must have the `manage`
  20. <<privileges-list-indices,index privilege>> for the rollover target.
  21. [[rollover-index-api-desc]]
  22. ==== {api-description-title}
  23. TIP: We recommend using {ilm-init}'s <<ilm-rollover,`rollover`>> action to
  24. automate rollovers. See <<ilm-index-lifecycle>>.
  25. The rollover API creates a new index for a data stream or index alias.
  26. The API's behavior depends on the rollover target.
  27. **Roll over a data stream**
  28. If you roll over a data stream, the API creates a new write index for the
  29. stream. The stream's previous write index becomes a regular backing index. A
  30. rollover also increments the data stream's generation. See
  31. <<data-streams-rollover>>.
  32. **Roll over an index alias with a write index**
  33. [TIP]
  34. ====
  35. include::{es-repo-dir}/data-streams/set-up-a-data-stream.asciidoc[tag=time-series-alias-tip]
  36. See <<convert-index-alias-to-data-stream>>.
  37. ====
  38. If an index alias points to multiple indices, one of the indices must be a
  39. <<write-index,write index>>. The rollover API creates a new write index
  40. for the alias with `is_write_index` set to `true`. The API also sets
  41. `is_write_index` to `false` for the previous write index.
  42. **Roll over an index alias with one index**
  43. If you roll over an index alias that points to only one index, the API creates a
  44. new index for the alias and removes the original index from the alias.
  45. [[increment-index-names-for-alias]]
  46. ===== Increment index names for an alias
  47. When you roll over an index alias, you can specify a name for the new index. If
  48. you don't specify a name and the current index ends with `-` and a number, such
  49. as `my-index-000001` or `my-index-3`, the new index name increments that number.
  50. For example, if you roll over an alias with a current index of
  51. `my-index-000001`, the rollover creates a new index named `my-index-000002`.
  52. This number is always 6 characters and zero-padded, regardless of the previous
  53. index's name.
  54. [[_using_date_math_with_the_rollover_api]]
  55. .Use date math with index alias rollovers
  56. ****
  57. If you use an index alias for time series data, you can use
  58. <<date-math-index-names,date math>> in the index name to track the rollover
  59. date. For example, you can create an alias that points to an index named
  60. `<my-index-{now/d}-000001>`. If you create the index on May 6, 2099, the index's
  61. name is `my-index-2099.05.06-000001`. If you roll over the alias on May 7, 2099,
  62. the new index's name is `my-index-2099.05.07-000002`. For an example, see
  63. <<roll-over-index-alias-with-write-index>>.
  64. ****
  65. [[rollover-wait-active-shards]]
  66. ===== Wait for active shards
  67. A rollover creates a new index and is subject to the
  68. <<create-index-wait-for-active-shards,`wait_for_active_shards`>> setting.
  69. [[rollover-index-api-path-params]]
  70. ==== {api-path-parms-title}
  71. `<rollover-target>`::
  72. (Required, string)
  73. Name of the data stream or index alias to roll over.
  74. `<target-index>`::
  75. (Optional, string)
  76. Name of the index to create. Supports <<date-math-index-names,date math>>. Data
  77. streams do not support this parameter.
  78. +
  79. If the name of the alias's current write index does not end with `-` and a
  80. number, such as `my-index-000001` or `my-index-3`, this parameter is required.
  81. +
  82. include::{es-repo-dir}/indices/create-index.asciidoc[tag=index-name-reqs]
  83. [[rollover-index-api-query-params]]
  84. ==== {api-query-parms-title}
  85. `dry_run`::
  86. (Optional, Boolean)
  87. If `true`, checks whether the current index matches one or more specified
  88. `conditions` but does not perform a rollover. Defaults to `false`.
  89. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
  90. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  91. [role="child_attributes"]
  92. [[rollover-index-api-request-body]]
  93. ==== {api-request-body-title}
  94. `aliases`::
  95. (Optional, object of objects) Aliases for the target index. Data streams do not
  96. support this parameter.
  97. +
  98. include::{es-repo-dir}/indices/create-index.asciidoc[tag=aliases-props]
  99. [[rollover-conditions]]
  100. `conditions`::
  101. (Optional, object)
  102. Conditions for the rollover. If specified, {es} only performs the rollover if
  103. the current index meets one or more of these conditions. If this parameter is
  104. not specified, {es} performs the rollover unconditionally.
  105. +
  106. IMPORTANT: To trigger a rollover, the current index must meet these conditions
  107. at the time of the request. {es} does not monitor the index after the API
  108. response. To automate rollover, use {ilm-init}'s <<ilm-rollover,`rollover`>>
  109. instead.
  110. +
  111. .Properties of `conditions`
  112. [%collapsible%open]
  113. ====
  114. include::{es-repo-dir}/ilm/actions/ilm-rollover.asciidoc[tag=rollover-conditions]
  115. ====
  116. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=mappings]
  117. +
  118. Data streams do not support this parameter.
  119. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=settings]
  120. +
  121. Data streams do not support this parameter.
  122. [role="child_attributes"]
  123. [[rollover-index-api-response-body]]
  124. ==== {api-response-body-title}
  125. `acknowledged`::
  126. (Boolean)
  127. If `true`, the request received a response from the master node within the
  128. `timeout` period.
  129. `shards_acknowledged`::
  130. (Boolean)
  131. If `true`, the request received a response from
  132. <<index-wait-for-active-shards,active shards>> within the `master_timeout`
  133. period.
  134. `old_index`::
  135. (string)
  136. Previous index for the data stream or index alias. For data streams and index
  137. aliases with a write index, this is the previous write index.
  138. `new_index`::
  139. (string)
  140. Index created by the rollover. For data streams and index aliases with a write
  141. index, this is the current write index.
  142. `rolled_over`::
  143. (Boolean)
  144. If `true`, the data stream or index alias rolled over.
  145. `dry_run`::
  146. (Boolean)
  147. If `true`, {es} did not perform the rollover.
  148. `condition`::
  149. (object)
  150. Result of each condition specified in the request's `conditions`. If no
  151. conditions were specified, this is an empty object.
  152. +
  153. .Properties of `condition`
  154. [%collapsible%open]
  155. ====
  156. `<condition>`::
  157. (Boolean) The key is each condition. The value is its result. If `true`, the
  158. index met the condition at rollover.
  159. ====
  160. [[rollover-index-api-example]]
  161. ==== {api-examples-title}
  162. [[roll-over-data-stream]]
  163. ===== Roll over a data stream
  164. The following request rolls over a data stream unconditionally.
  165. [source,console]
  166. ----
  167. POST my-data-stream/_rollover
  168. ----
  169. // TEST[setup:my_data_stream]
  170. :target: data stream
  171. :index: write index
  172. // tag::rollover-conditions-ex[]
  173. The following request only rolls over the {target} if the current {index}
  174. meets one or more of the following conditions:
  175. * The index was created 7 or more days ago.
  176. * The index contains 1,000 or more documents.
  177. * The index's largest primary shard is 50GB or larger.
  178. // end::rollover-conditions-ex[]
  179. [source,console]
  180. ----
  181. POST my-data-stream/_rollover
  182. {
  183. "conditions": {
  184. "max_age": "7d",
  185. "max_docs": 1000,
  186. "max_primary_shard_size": "50gb",
  187. "max_primary_shard_docs": "2000"
  188. }
  189. }
  190. ----
  191. // TEST[continued]
  192. // TEST[setup:my_index_huge]
  193. // TEST[s/^/POST _reindex?refresh\n{ "source": { "index": "my-index-000001" }, "dest": { "index": "my-data-stream", "op_type": "create" } }\n/]
  194. The API returns:
  195. [source,console-result]
  196. ----
  197. {
  198. "acknowledged": true,
  199. "shards_acknowledged": true,
  200. "old_index": ".ds-my-data-stream-2099.05.06-000001",
  201. "new_index": ".ds-my-data-stream-2099.05.07-000002",
  202. "rolled_over": true,
  203. "dry_run": false,
  204. "conditions": {
  205. "[max_age: 7d]": false,
  206. "[max_docs: 1000]": true,
  207. "[max_primary_shard_size: 50gb]": false,
  208. "[max_primary_shard_docs: 2000]": false
  209. }
  210. }
  211. ----
  212. // TESTRESPONSE[s/.ds-my-data-stream-2099.05.06-000001/$body.old_index/]
  213. // TESTRESPONSE[s/.ds-my-data-stream-2099.05.07-000002/$body.new_index/]
  214. ////
  215. [source,console]
  216. ----
  217. DELETE _data_stream/*
  218. DELETE _index_template/*
  219. ----
  220. // TEST[continued]
  221. ////
  222. [[roll-over-index-alias-with-write-index]]
  223. ===== Roll over an index alias with a write index
  224. The following request creates `<my-index-{now/d}-000001>` and sets it as the
  225. write index for `my-alias`.
  226. [source,console]
  227. ----
  228. # PUT <my-index-{now/d}-000001>
  229. PUT %3Cmy-index-%7Bnow%2Fd%7D-000001%3E
  230. {
  231. "aliases": {
  232. "my-alias": {
  233. "is_write_index": true
  234. }
  235. }
  236. }
  237. ----
  238. // TEST[s/%3Cmy-index-%7Bnow%2Fd%7D-000001%3E/my-index-2099.05.06-000001/]
  239. :target: alias
  240. include::rollover-index.asciidoc[tag=rollover-conditions-ex]
  241. [source,console]
  242. ----
  243. POST my-alias/_rollover
  244. {
  245. "conditions": {
  246. "max_age": "7d",
  247. "max_docs": 1000,
  248. "max_primary_shard_size": "50gb",
  249. "max_primary_shard_docs": "2000"
  250. }
  251. }
  252. ----
  253. // TEST[continued]
  254. // TEST[setup:my_index_huge]
  255. // TEST[s/^/POST _reindex?refresh\n{ "source": { "index": "my-index-000001" }, "dest": { "index": "my-alias" } }\n/]
  256. The API returns:
  257. [source,console-result]
  258. ----
  259. {
  260. "acknowledged": true,
  261. "shards_acknowledged": true,
  262. "old_index": "my-index-2099.05.06-000001",
  263. "new_index": "my-index-2099.05.07-000002",
  264. "rolled_over": true,
  265. "dry_run": false,
  266. "conditions": {
  267. "[max_age: 7d]": false,
  268. "[max_docs: 1000]": true,
  269. "[max_primary_shard_size: 50gb]": false,
  270. "[max_primary_shard_docs: 2000]": false
  271. }
  272. }
  273. ----
  274. // TESTRESPONSE[s/my-index-2099.05.07-000002/my-index-2099.05.06-000002/]
  275. If the alias's index names use date math and you roll over indices at a regular
  276. interval, you can use date math to narrow your searches. For example, the
  277. following search targets indices created in the last three days.
  278. [source,console]
  279. ----
  280. # GET /<my-index-{now/d}-*>,<my-index-{now/d-1d}-*>,<my-index-{now/d-2d}-*>/_search
  281. GET /%3Cmy-index-%7Bnow%2Fd%7D-*%3E%2C%3Cmy-index-%7Bnow%2Fd-1d%7D-*%3E%2C%3Cmy-index-%7Bnow%2Fd-2d%7D-*%3E/_search
  282. ----
  283. [[roll-over-index-alias-with-one-index]]
  284. ===== Roll over an index alias with one index
  285. The following request creates `<my-index-{now/d}-000001>` and its alias, `my-write-alias`.
  286. [source,console]
  287. ----
  288. # PUT <my-index-{now/d}-000001>
  289. PUT %3Cmy-index-%7Bnow%2Fd%7D-000001%3E
  290. {
  291. "aliases": {
  292. "my-write-alias": { }
  293. }
  294. }
  295. ----
  296. // TEST[s/%3Cmy-index-%7Bnow%2Fd%7D-000001%3E/my-index-2099.05.06-000001/]
  297. :index: index
  298. include::rollover-index.asciidoc[tag=rollover-conditions-ex]
  299. :target!:
  300. :index!:
  301. [source,console]
  302. ----
  303. POST my-write-alias/_rollover
  304. {
  305. "conditions": {
  306. "max_age": "7d",
  307. "max_docs": 1000,
  308. "max_primary_shard_size": "50gb",
  309. "max_primary_shard_docs": "2000"
  310. }
  311. }
  312. ----
  313. // TEST[continued]
  314. // TEST[setup:my_index_huge]
  315. // TEST[s/^/POST _reindex?refresh\n{ "source": { "index": "my-index-000001" }, "dest": { "index": "my-write-alias" } }\n/]
  316. The API returns:
  317. [source,console-result]
  318. ----
  319. {
  320. "acknowledged": true,
  321. "shards_acknowledged": true,
  322. "old_index": "my-index-2099.05.06-000001",
  323. "new_index": "my-index-2099.05.07-000002",
  324. "rolled_over": true,
  325. "dry_run": false,
  326. "conditions": {
  327. "[max_age: 7d]": false,
  328. "[max_docs: 1000]": true,
  329. "[max_primary_shard_size: 50gb]": false,
  330. "[max_primary_shard_docs: 2000]": false
  331. }
  332. }
  333. ----
  334. // TESTRESPONSE[s/my-index-2099.05.07-000002/my-index-2099.05.06-000002/]
  335. [[specify-settings-during-rollover]]
  336. ===== Specify settings during a rollover
  337. Typically, you use an <<index-templates,index template>> to automatically
  338. configure indices created during a rollover. If you roll over an index alias,
  339. you use the rollover API to add additional index settings or overwrite settings
  340. in the template. Data streams do not support the `settings` parameter.
  341. [source,console]
  342. ----
  343. POST my-alias/_rollover
  344. {
  345. "settings": {
  346. "index.number_of_shards": 2
  347. }
  348. }
  349. ----
  350. // TEST[continued]
  351. // TEST[s/my-alias/my-write-alias/]