rollover-index.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. }
  188. }
  189. ----
  190. // TEST[continued]
  191. // TEST[setup:my_index_huge]
  192. // TEST[s/^/POST _reindex?refresh\n{ "source": { "index": "my-index-000001" }, "dest": { "index": "my-data-stream", "op_type": "create" } }\n/]
  193. The API returns:
  194. [source,console-result]
  195. ----
  196. {
  197. "acknowledged": true,
  198. "shards_acknowledged": true,
  199. "old_index": ".ds-my-data-stream-2099.05.06-000001",
  200. "new_index": ".ds-my-data-stream-2099.05.07-000002",
  201. "rolled_over": true,
  202. "dry_run": false,
  203. "conditions": {
  204. "[max_age: 7d]": false,
  205. "[max_docs: 1000]": true,
  206. "[max_primary_shard_size: 50gb]": false
  207. }
  208. }
  209. ----
  210. // TESTRESPONSE[s/.ds-my-data-stream-2099.05.06-000001/$body.old_index/]
  211. // TESTRESPONSE[s/.ds-my-data-stream-2099.05.07-000002/$body.new_index/]
  212. ////
  213. [source,console]
  214. ----
  215. DELETE _data_stream/*
  216. DELETE _index_template/*
  217. ----
  218. // TEST[continued]
  219. ////
  220. [[roll-over-index-alias-with-write-index]]
  221. ===== Roll over an index alias with a write index
  222. The following request creates `<my-index-{now/d}-000001>` and sets it as the
  223. write index for `my-alias`.
  224. [source,console]
  225. ----
  226. # PUT <my-index-{now/d}-000001>
  227. PUT %3Cmy-index-%7Bnow%2Fd%7D-000001%3E
  228. {
  229. "aliases": {
  230. "my-alias": {
  231. "is_write_index": true
  232. }
  233. }
  234. }
  235. ----
  236. // TEST[s/%3Cmy-index-%7Bnow%2Fd%7D-000001%3E/my-index-2099.05.06-000001/]
  237. :target: alias
  238. include::rollover-index.asciidoc[tag=rollover-conditions-ex]
  239. [source,console]
  240. ----
  241. POST my-alias/_rollover
  242. {
  243. "conditions": {
  244. "max_age": "7d",
  245. "max_docs": 1000,
  246. "max_primary_shard_size": "50gb"
  247. }
  248. }
  249. ----
  250. // TEST[continued]
  251. // TEST[setup:my_index_huge]
  252. // TEST[s/^/POST _reindex?refresh\n{ "source": { "index": "my-index-000001" }, "dest": { "index": "my-alias" } }\n/]
  253. The API returns:
  254. [source,console-result]
  255. ----
  256. {
  257. "acknowledged": true,
  258. "shards_acknowledged": true,
  259. "old_index": "my-index-2099.05.06-000001",
  260. "new_index": "my-index-2099.05.07-000002",
  261. "rolled_over": true,
  262. "dry_run": false,
  263. "conditions": {
  264. "[max_age: 7d]": false,
  265. "[max_docs: 1000]": true,
  266. "[max_primary_shard_size: 50gb]": false
  267. }
  268. }
  269. ----
  270. // TESTRESPONSE[s/my-index-2099.05.07-000002/my-index-2099.05.06-000002/]
  271. If the alias's index names use date math and you roll over indices at a regular
  272. interval, you can use date math to narrow your searches. For example, the
  273. following search targets indices created in the last three days.
  274. [source,console]
  275. ----
  276. # GET /<my-index-{now/d}-*>,<my-index-{now/d-1d}-*>,<my-index-{now/d-2d}-*>/_search
  277. GET /%3Cmy-index-%7Bnow%2Fd%7D-*%3E%2C%3Cmy-index-%7Bnow%2Fd-1d%7D-*%3E%2C%3Cmy-index-%7Bnow%2Fd-2d%7D-*%3E/_search
  278. ----
  279. [[roll-over-index-alias-with-one-index]]
  280. ===== Roll over an index alias with one index
  281. The following request creates `<my-index-{now/d}-000001>` and its alias, `my-write-alias`.
  282. [source,console]
  283. ----
  284. # PUT <my-index-{now/d}-000001>
  285. PUT %3Cmy-index-%7Bnow%2Fd%7D-000001%3E
  286. {
  287. "aliases": {
  288. "my-write-alias": { }
  289. }
  290. }
  291. ----
  292. // TEST[s/%3Cmy-index-%7Bnow%2Fd%7D-000001%3E/my-index-2099.05.06-000001/]
  293. :index: index
  294. include::rollover-index.asciidoc[tag=rollover-conditions-ex]
  295. :target!:
  296. :index!:
  297. [source,console]
  298. ----
  299. POST my-write-alias/_rollover
  300. {
  301. "conditions": {
  302. "max_age": "7d",
  303. "max_docs": 1000,
  304. "max_primary_shard_size": "50gb"
  305. }
  306. }
  307. ----
  308. // TEST[continued]
  309. // TEST[setup:my_index_huge]
  310. // TEST[s/^/POST _reindex?refresh\n{ "source": { "index": "my-index-000001" }, "dest": { "index": "my-write-alias" } }\n/]
  311. The API returns:
  312. [source,console-result]
  313. ----
  314. {
  315. "acknowledged": true,
  316. "shards_acknowledged": true,
  317. "old_index": "my-index-2099.05.06-000001",
  318. "new_index": "my-index-2099.05.07-000002",
  319. "rolled_over": true,
  320. "dry_run": false,
  321. "conditions": {
  322. "[max_age: 7d]": false,
  323. "[max_docs: 1000]": true,
  324. "[max_primary_shard_size: 50gb]": false
  325. }
  326. }
  327. ----
  328. // TESTRESPONSE[s/my-index-2099.05.07-000002/my-index-2099.05.06-000002/]
  329. [[specify-settings-during-rollover]]
  330. ===== Specify settings during a rollover
  331. Typically, you use an <<index-templates,index template>> to automatically
  332. configure indices created during a rollover. If you roll over an index alias,
  333. you use the rollover API to add additional index settings or overwrite settings
  334. in the template. Data streams do not support the `settings` parameter.
  335. [source,console]
  336. ----
  337. POST my-alias/_rollover
  338. {
  339. "settings": {
  340. "index.number_of_shards": 2
  341. }
  342. }
  343. ----
  344. // TEST[continued]
  345. // TEST[s/my-alias/my-write-alias/]