rollover-index.asciidoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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/twitter
  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:huge_twitter]
  143. // TEST[s/# Add > 1000 documents to logs-000001/POST _reindex?refresh\n{"source":{"index":"twitter"},"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. "timestamp_field": "@timestamp"
  177. }
  178. }
  179. -----------------------------------
  180. // TEST
  181. [source,console]
  182. --------------------------------------------------
  183. PUT /_data_stream/my-data-stream <1>
  184. # Add > 1000 documents to my-data-stream
  185. POST /my-data-stream/_rollover <2>
  186. {
  187. "conditions" : {
  188. "max_age": "7d",
  189. "max_docs": 1000,
  190. "max_size": "5gb"
  191. }
  192. }
  193. --------------------------------------------------
  194. // TEST[continued]
  195. // TEST[setup:huge_twitter]
  196. // TEST[s/# Add > 1000 documents to my-data-stream/POST _reindex?refresh\n{"source":{"index":"twitter"},"dest":{"index":"my-data-stream","op_type":"create"}}/]
  197. <1> Creates a data stream called `my-data-stream` with one initial backing index
  198. named `my-data-stream-000001`.
  199. <2> This request creates a new backing index, `my-data-stream-000002`, and adds
  200. it as the write index for the `my-data-stream` data stream if the current
  201. write index meets at least one of the following conditions:
  202. +
  203. --
  204. * The index was created 7 or more days ago.
  205. * The index has an index size of 5GB or greater.
  206. * The index contains 1,000 or more documents.
  207. --
  208. The API returns the following response:
  209. [source,console-result]
  210. --------------------------------------------------
  211. {
  212. "acknowledged": true,
  213. "shards_acknowledged": true,
  214. "old_index": ".ds-my-data-stream-000001", <1>
  215. "new_index": ".ds-my-data-stream-000002", <2>
  216. "rolled_over": true, <3>
  217. "dry_run": false, <4>
  218. "conditions": { <5>
  219. "[max_age: 7d]": false,
  220. "[max_docs: 1000]": true,
  221. "[max_size: 5gb]": false,
  222. }
  223. }
  224. --------------------------------------------------
  225. <1> The previous write index for the data stream.
  226. <2> The new write index for the data stream.
  227. <3> Whether the index was rolled over.
  228. <4> Whether the rollover was dry run.
  229. <5> The result of each condition.
  230. ////
  231. [source,console]
  232. -----------------------------------
  233. DELETE /_data_stream/my-data-stream
  234. DELETE /_index_template/*
  235. -----------------------------------
  236. // TEST[continued]
  237. ////
  238. [[rollover-index-settings-ex]]
  239. ===== Specify settings for the target index
  240. The settings, mappings, and aliases for the new index are taken from any
  241. matching <<indices-templates,index templates>>. If the rollover target is an index
  242. alias, you can specify `settings`, `mappings`, and `aliases` in the body of the request
  243. just like the <<indices-create-index,create index>> API. Values specified in the request
  244. override any values set in matching index templates. For example, the following
  245. `rollover` request overrides the `index.number_of_shards` setting:
  246. [source,console]
  247. --------------------------------------------------
  248. PUT /logs-000001
  249. {
  250. "aliases": {
  251. "logs_write": {}
  252. }
  253. }
  254. POST /logs_write/_rollover
  255. {
  256. "conditions" : {
  257. "max_age": "7d",
  258. "max_docs": 1000,
  259. "max_size": "5gb"
  260. },
  261. "settings": {
  262. "index.number_of_shards": 2
  263. }
  264. }
  265. --------------------------------------------------
  266. [[rollover-index-specify-index-ex]]
  267. ===== Specify a target index name
  268. If the rollover target is a data stream, you cannot specify a target index
  269. name. The new index name will always be in the form `<rollover-target>-000001`
  270. where the zero-padded number of length 6 is the generation of the data stream.
  271. If the rollover target is an index alias and the name of the existing index ends
  272. with `-` and a number -- e.g. `logs-000001` -- then the name of the new index will
  273. follow the same pattern, incrementing the number (`logs-000002`). The number is
  274. zero-padded with a length of 6, regardless of the old index name.
  275. If the old name doesn't match this pattern then you must specify the name for
  276. the new index as follows:
  277. [source,console]
  278. --------------------------------------------------
  279. POST /my_alias/_rollover/my_new_index_name
  280. {
  281. "conditions": {
  282. "max_age": "7d",
  283. "max_docs": 1000,
  284. "max_size": "5gb"
  285. }
  286. }
  287. --------------------------------------------------
  288. // TEST[s/^/PUT my_old_index_name\nPUT my_old_index_name\/_alias\/my_alias\n/]
  289. [[_using_date_math_with_the_rollover_api]]
  290. ===== Use date math with a rollover
  291. If the rollover target is an index alias, it can be useful to use <<date-math-index-names,date math>>
  292. to name the rollover index according to the date that the index rolled over, e.g.
  293. `logstash-2016.02.03`. The rollover API supports date math, but requires the
  294. index name to end with a dash followed by a number, e.g. `logstash-2016.02.03-1`
  295. which is incremented every time the index is rolled over. For instance:
  296. [source,console]
  297. --------------------------------------------------
  298. # PUT /<logs-{now/d}-1> with URI encoding:
  299. PUT /%3Clogs-%7Bnow%2Fd%7D-1%3E <1>
  300. {
  301. "aliases": {
  302. "logs_write": {}
  303. }
  304. }
  305. PUT logs_write/_doc/1
  306. {
  307. "message": "a dummy log"
  308. }
  309. POST logs_write/_refresh
  310. # Wait for a day to pass
  311. POST /logs_write/_rollover <2>
  312. {
  313. "conditions": {
  314. "max_docs": "1"
  315. }
  316. }
  317. --------------------------------------------------
  318. // TEST[s/now/2016.10.31%7C%7C/]
  319. <1> Creates an index named with today's date (e.g.) `logs-2016.10.31-1`
  320. <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
  321. //////////////////////////
  322. [source,console]
  323. --------------------------------------------------
  324. GET _alias
  325. --------------------------------------------------
  326. // TEST[continued]
  327. [source,console-result]
  328. --------------------------------------------------
  329. {
  330. "logs-2016.10.31-000002": {
  331. "aliases": {
  332. "logs_write": {}
  333. }
  334. },
  335. "logs-2016.10.31-1": {
  336. "aliases": {}
  337. }
  338. }
  339. --------------------------------------------------
  340. //////////////////////////
  341. These indices can then be referenced as described in the
  342. <<date-math-index-names,date math documentation>>. For example, to search
  343. over indices created in the last three days, you could do the following:
  344. [source,console]
  345. --------------------------------------------------
  346. # GET /<logs-{now/d}-*>,<logs-{now/d-1d}-*>,<logs-{now/d-2d}-*>/_search
  347. GET /%3Clogs-%7Bnow%2Fd%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-1d%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-2d%7D-*%3E/_search
  348. --------------------------------------------------
  349. // TEST[continued]
  350. // TEST[s/now/2016.10.31%7C%7C/]
  351. [[rollover-index-api-dry-run-ex]]
  352. ===== Dry run
  353. The rollover API supports `dry_run` mode, where request conditions can be
  354. checked without performing the actual rollover.
  355. [source,console]
  356. --------------------------------------------------
  357. POST /logs_write/_rollover?dry_run
  358. {
  359. "conditions" : {
  360. "max_age": "7d",
  361. "max_docs": 1000,
  362. "max_size": "5gb"
  363. }
  364. }
  365. --------------------------------------------------
  366. // TEST[s/^/PUT logs-000001\nPUT logs-000001\/_alias\/logs_write\n/]
  367. [[indices-rollover-is-write-index]]
  368. ===== Roll over a write index
  369. If the rollover target is an index alias for a write index that has `is_write_index` explicitly set to `true`, it is not
  370. swapped during rollover actions. Since having an alias point to multiple indices is ambiguous in distinguishing
  371. which is the correct write index to roll over, it is not valid to rollover an alias that points to multiple indices.
  372. For this reason, the default behavior is to swap which index is being pointed to by the write-oriented alias. This
  373. was `logs_write` in some of the above examples. Since setting `is_write_index` enables an alias to point to multiple indices
  374. while also being explicit as to which is the write index that rollover should target, removing the alias from the rolled over
  375. index is not necessary. This simplifies things by allowing for one alias to behave both as the write and read aliases for
  376. indices that are being managed with Rollover.
  377. Look at the behavior of the aliases in the following example where `is_write_index` is set on the rolled over index.
  378. [source,console]
  379. --------------------------------------------------
  380. PUT my_logs_index-000001
  381. {
  382. "aliases": {
  383. "logs": { "is_write_index": true } <1>
  384. }
  385. }
  386. PUT logs/_doc/1
  387. {
  388. "message": "a dummy log"
  389. }
  390. POST logs/_refresh
  391. POST /logs/_rollover
  392. {
  393. "conditions": {
  394. "max_docs": "1"
  395. }
  396. }
  397. PUT logs/_doc/2 <2>
  398. {
  399. "message": "a newer log"
  400. }
  401. --------------------------------------------------
  402. <1> configures `my_logs_index` as the write index for the `logs` alias
  403. <2> newly indexed documents against the `logs` alias will write to the new index
  404. [source,console-result]
  405. --------------------------------------------------
  406. {
  407. "_index" : "my_logs_index-000002",
  408. "_id" : "2",
  409. "_version" : 1,
  410. "result" : "created",
  411. "_shards" : {
  412. "total" : 2,
  413. "successful" : 1,
  414. "failed" : 0
  415. },
  416. "_seq_no" : 0,
  417. "_primary_term" : 1
  418. }
  419. --------------------------------------------------
  420. //////////////////////////
  421. [source,console]
  422. --------------------------------------------------
  423. GET my_logs_index-000001,my_logs_index-000002/_alias
  424. --------------------------------------------------
  425. // TEST[continued]
  426. //////////////////////////
  427. After the rollover, the alias metadata for the two indices will have the `is_write_index` setting
  428. reflect each index's role, with the newly created index as the write index.
  429. [source,console-result]
  430. --------------------------------------------------
  431. {
  432. "my_logs_index-000002": {
  433. "aliases": {
  434. "logs": { "is_write_index": true }
  435. }
  436. },
  437. "my_logs_index-000001": {
  438. "aliases": {
  439. "logs": { "is_write_index" : false }
  440. }
  441. }
  442. }
  443. --------------------------------------------------