rollover-index.asciidoc 16 KB

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