rollover-index.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. [[indices-rollover-index]]
  2. == Rollover Index
  3. The rollover index API rolls an <<indices-aliases, alias>> to a new index when
  4. the existing index meets a condition you provide. You can use this API to retire
  5. an index that becomes too large or too old.
  6. NOTE: To roll over an index, a condition must be met *when you call the API*.
  7. {es} does not monitor the index after you receive an API response. To
  8. automatically roll over indices when a condition is met, you can use {es}'s
  9. <<index-lifecycle-management, index lifecycle management (ILM) policies>>.
  10. The API accepts a single alias name and a list of `conditions`. The alias must point to a write index for
  11. a Rollover request to be valid. There are two ways this can be achieved, and depending on the configuration, the
  12. alias metadata will be updated differently. The two scenarios are as follows:
  13. - The alias only points to a single index with `is_write_index` not configured (defaults to `null`).
  14. In this scenario, the original index will have their rollover alias will be added to the newly created index, and removed
  15. from the original (rolled-over) index.
  16. - The alias points to one or more indices with `is_write_index` set to `true` on the index to be rolled over (the write index).
  17. In this scenario, the write index will have its rollover alias' `is_write_index` set to `false`, while the newly created index
  18. will now have the rollover alias pointing to it as the write index with `is_write_index` as `true`.
  19. The available conditions are:
  20. [[index-rollover-conditions]]
  21. .`conditions` parameters
  22. [options="header"]
  23. |===
  24. | Name | Description
  25. | max_age | The maximum age of the index
  26. | max_docs | The maximum number of documents the index should contain. This does not add documents multiple times for replicas
  27. | max_size | The maximum estimated size of the primary shard of the index
  28. |===
  29. [source,js]
  30. --------------------------------------------------
  31. PUT /logs-000001 <1>
  32. {
  33. "aliases": {
  34. "logs_write": {}
  35. }
  36. }
  37. # Add > 1000 documents to logs-000001
  38. POST /logs_write/_rollover <2>
  39. {
  40. "conditions": {
  41. "max_age": "7d",
  42. "max_docs": 1000,
  43. "max_size": "5gb"
  44. }
  45. }
  46. --------------------------------------------------
  47. // CONSOLE
  48. // TEST[setup:huge_twitter]
  49. // TEST[s/# Add > 1000 documents to logs-000001/POST _reindex?refresh\n{"source":{"index":"twitter"},"dest":{"index":"logs-000001"}}/]
  50. <1> Creates an index called `logs-0000001` with the alias `logs_write`.
  51. <2> If the index pointed to by `logs_write` was created 7 or more days ago, or
  52. contains 1,000 or more documents, or has an index size at least around 5GB, then the `logs-000002` index is created
  53. and the `logs_write` alias is updated to point to `logs-000002`.
  54. The above request might return the following response:
  55. [source,js]
  56. --------------------------------------------------
  57. {
  58. "acknowledged": true,
  59. "shards_acknowledged": true,
  60. "old_index": "logs-000001",
  61. "new_index": "logs-000002",
  62. "rolled_over": true, <1>
  63. "dry_run": false, <2>
  64. "conditions": { <3>
  65. "[max_age: 7d]": false,
  66. "[max_docs: 1000]": true,
  67. "[max_size: 5gb]": false,
  68. }
  69. }
  70. --------------------------------------------------
  71. // TESTRESPONSE
  72. <1> Whether the index was rolled over.
  73. <2> Whether the rollover was dry run.
  74. <3> The result of each condition.
  75. [float]
  76. === Naming the new index
  77. If the name of the existing index ends with `-` and a number -- e.g.
  78. `logs-000001` -- then the name of the new index will follow the same pattern,
  79. incrementing the number (`logs-000002`). The number is zero-padded with a length
  80. of 6, regardless of the old index name.
  81. If the old name doesn't match this pattern then you must specify the name for
  82. the new index as follows:
  83. [source,js]
  84. --------------------------------------------------
  85. POST /my_alias/_rollover/my_new_index_name
  86. {
  87. "conditions": {
  88. "max_age": "7d",
  89. "max_docs": 1000,
  90. "max_size": "5gb"
  91. }
  92. }
  93. --------------------------------------------------
  94. // CONSOLE
  95. // TEST[s/^/PUT my_old_index_name\nPUT my_old_index_name\/_alias\/my_alias\n/]
  96. [float]
  97. === Using date math with the rollover API
  98. It can be useful to use <<date-math-index-names,date math>> to name the
  99. rollover index according to the date that the index rolled over, e.g.
  100. `logstash-2016.02.03`. The rollover API supports date math, but requires the
  101. index name to end with a dash followed by a number, e.g.
  102. `logstash-2016.02.03-1` which is incremented every time the index is rolled
  103. over. For instance:
  104. [source,js]
  105. --------------------------------------------------
  106. # PUT /<logs-{now/d}-1> with URI encoding:
  107. PUT /%3Clogs-%7Bnow%2Fd%7D-1%3E <1>
  108. {
  109. "aliases": {
  110. "logs_write": {}
  111. }
  112. }
  113. PUT logs_write/_doc/1
  114. {
  115. "message": "a dummy log"
  116. }
  117. POST logs_write/_refresh
  118. # Wait for a day to pass
  119. POST /logs_write/_rollover <2>
  120. {
  121. "conditions": {
  122. "max_docs": "1"
  123. }
  124. }
  125. --------------------------------------------------
  126. // CONSOLE
  127. // TEST[s/now/2016.10.31||/]
  128. <1> Creates an index named with today's date (e.g.) `logs-2016.10.31-1`
  129. <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
  130. //////////////////////////
  131. [source,js]
  132. --------------------------------------------------
  133. GET _alias
  134. --------------------------------------------------
  135. // CONSOLE
  136. // TEST[continued]
  137. [source,js]
  138. --------------------------------------------------
  139. {
  140. "logs-2016.10.31-000002": {
  141. "aliases": {
  142. "logs_write": {}
  143. }
  144. },
  145. "logs-2016.10.31-1": {
  146. "aliases": {}
  147. }
  148. }
  149. --------------------------------------------------
  150. // TESTRESPONSE
  151. //////////////////////////
  152. These indices can then be referenced as described in the
  153. <<date-math-index-names,date math documentation>>. For example, to search
  154. over indices created in the last three days, you could do the following:
  155. [source,js]
  156. --------------------------------------------------
  157. # GET /<logs-{now/d}-*>,<logs-{now/d-1d}-*>,<logs-{now/d-2d}-*>/_search
  158. GET /%3Clogs-%7Bnow%2Fd%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-1d%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-2d%7D-*%3E/_search
  159. --------------------------------------------------
  160. // CONSOLE
  161. // TEST[continued]
  162. // TEST[s/now/2016.10.31||/]
  163. [float]
  164. === Defining the new index
  165. The settings, mappings, and aliases for the new index are taken from any
  166. matching <<indices-templates,index templates>>. Additionally, you can specify
  167. `settings`, `mappings`, and `aliases` in the body of the request, just like the
  168. <<indices-create-index,create index>> API. Values specified in the request
  169. override any values set in matching index templates. For example, the following
  170. `rollover` request overrides the `index.number_of_shards` setting:
  171. [source,js]
  172. --------------------------------------------------
  173. PUT /logs-000001
  174. {
  175. "aliases": {
  176. "logs_write": {}
  177. }
  178. }
  179. POST /logs_write/_rollover
  180. {
  181. "conditions" : {
  182. "max_age": "7d",
  183. "max_docs": 1000,
  184. "max_size": "5gb"
  185. },
  186. "settings": {
  187. "index.number_of_shards": 2
  188. }
  189. }
  190. --------------------------------------------------
  191. // CONSOLE
  192. [float]
  193. === Dry run
  194. The rollover API supports `dry_run` mode, where request conditions can be
  195. checked without performing the actual rollover:
  196. [source,js]
  197. --------------------------------------------------
  198. PUT /logs-000001
  199. {
  200. "aliases": {
  201. "logs_write": {}
  202. }
  203. }
  204. POST /logs_write/_rollover?dry_run
  205. {
  206. "conditions" : {
  207. "max_age": "7d",
  208. "max_docs": 1000,
  209. "max_size": "5gb"
  210. }
  211. }
  212. --------------------------------------------------
  213. // CONSOLE
  214. [float]
  215. === Wait For Active Shards
  216. Because the rollover operation creates a new index to rollover to, the
  217. <<create-index-wait-for-active-shards,`wait_for_active_shards`>> setting on
  218. index creation applies to the rollover action as well.
  219. [[indices-rollover-is-write-index]]
  220. [float]
  221. === Write Index Alias Behavior
  222. The rollover alias when rolling over a write index that has `is_write_index` explicitly set to `true` is not
  223. swapped during rollover actions. Since having an alias point to multiple indices is ambiguous in distinguishing
  224. which is the correct write index to roll over, it is not valid to rollover an alias that points to multiple indices.
  225. For this reason, the default behavior is to swap which index is being pointed to by the write-oriented alias. This
  226. was `logs_write` in some of the above examples. Since setting `is_write_index` enables an alias to point to multiple indices
  227. while also being explicit as to which is the write index that rollover should target, removing the alias from the rolled over
  228. index is not necessary. This simplifies things by allowing for one alias to behave both as the write and read aliases for
  229. indices that are being managed with Rollover.
  230. Look at the behavior of the aliases in the following example where `is_write_index` is set on the rolled over index.
  231. [source,js]
  232. --------------------------------------------------
  233. PUT my_logs_index-000001
  234. {
  235. "aliases": {
  236. "logs": { "is_write_index": true } <1>
  237. }
  238. }
  239. PUT logs/_doc/1
  240. {
  241. "message": "a dummy log"
  242. }
  243. POST logs/_refresh
  244. POST /logs/_rollover
  245. {
  246. "conditions": {
  247. "max_docs": "1"
  248. }
  249. }
  250. PUT logs/_doc/2 <2>
  251. {
  252. "message": "a newer log"
  253. }
  254. --------------------------------------------------
  255. // CONSOLE
  256. <1> configures `my_logs_index` as the write index for the `logs` alias
  257. <2> newly indexed documents against the `logs` alias will write to the new index
  258. [source,js]
  259. --------------------------------------------------
  260. {
  261. "_index" : "my_logs_index-000002",
  262. "_type" : "_doc",
  263. "_id" : "2",
  264. "_version" : 1,
  265. "result" : "created",
  266. "_shards" : {
  267. "total" : 2,
  268. "successful" : 1,
  269. "failed" : 0
  270. },
  271. "_seq_no" : 0,
  272. "_primary_term" : 1
  273. }
  274. --------------------------------------------------
  275. // TESTRESPONSE
  276. //////////////////////////
  277. [source,js]
  278. --------------------------------------------------
  279. GET _alias
  280. --------------------------------------------------
  281. // CONSOLE
  282. // TEST[continued]
  283. //////////////////////////
  284. After the rollover, the alias metadata for the two indices will have the `is_write_index` setting
  285. reflect each index's role, with the newly created index as the write index.
  286. [source,js]
  287. --------------------------------------------------
  288. {
  289. "my_logs_index-000002": {
  290. "aliases": {
  291. "logs": { "is_write_index": true }
  292. }
  293. },
  294. "my_logs_index-000001": {
  295. "aliases": {
  296. "logs": { "is_write_index" : false }
  297. }
  298. }
  299. }
  300. --------------------------------------------------
  301. // TESTRESPONSE