rollover-index.asciidoc 17 KB

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