rollover-index.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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,console-result]
  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. <1> Whether the index was rolled over.
  72. <2> Whether the rollover was dry run.
  73. <3> The result of each condition.
  74. [float]
  75. ==== Naming the new index
  76. If the name of the existing index ends with `-` and a number -- e.g.
  77. `logs-000001` -- then the name of the new index will follow the same pattern,
  78. incrementing the number (`logs-000002`). The number is zero-padded with a length
  79. of 6, regardless of the old index name.
  80. If the old name doesn't match this pattern then you must specify the name for
  81. the new index as follows:
  82. [source,js]
  83. --------------------------------------------------
  84. POST /my_alias/_rollover/my_new_index_name
  85. {
  86. "conditions": {
  87. "max_age": "7d",
  88. "max_docs": 1000,
  89. "max_size": "5gb"
  90. }
  91. }
  92. --------------------------------------------------
  93. // CONSOLE
  94. // TEST[s/^/PUT my_old_index_name\nPUT my_old_index_name\/_alias\/my_alias\n/]
  95. [float]
  96. ==== Using date math with the rollover API
  97. It can be useful to use <<date-math-index-names,date math>> to name the
  98. rollover index according to the date that the index rolled over, e.g.
  99. `logstash-2016.02.03`. The rollover API supports date math, but requires the
  100. index name to end with a dash followed by a number, e.g.
  101. `logstash-2016.02.03-1` which is incremented every time the index is rolled
  102. over. For instance:
  103. [source,js]
  104. --------------------------------------------------
  105. # PUT /<logs-{now/d}-1> with URI encoding:
  106. PUT /%3Clogs-%7Bnow%2Fd%7D-1%3E <1>
  107. {
  108. "aliases": {
  109. "logs_write": {}
  110. }
  111. }
  112. PUT logs_write/_doc/1
  113. {
  114. "message": "a dummy log"
  115. }
  116. POST logs_write/_refresh
  117. # Wait for a day to pass
  118. POST /logs_write/_rollover <2>
  119. {
  120. "conditions": {
  121. "max_docs": "1"
  122. }
  123. }
  124. --------------------------------------------------
  125. // CONSOLE
  126. // TEST[s/now/2016.10.31||/]
  127. <1> Creates an index named with today's date (e.g.) `logs-2016.10.31-1`
  128. <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
  129. //////////////////////////
  130. [source,js]
  131. --------------------------------------------------
  132. GET _alias
  133. --------------------------------------------------
  134. // CONSOLE
  135. // TEST[continued]
  136. [source,console-result]
  137. --------------------------------------------------
  138. {
  139. "logs-2016.10.31-000002": {
  140. "aliases": {
  141. "logs_write": {}
  142. }
  143. },
  144. "logs-2016.10.31-1": {
  145. "aliases": {}
  146. }
  147. }
  148. --------------------------------------------------
  149. //////////////////////////
  150. These indices can then be referenced as described in the
  151. <<date-math-index-names,date math documentation>>. For example, to search
  152. over indices created in the last three days, you could do the following:
  153. [source,js]
  154. --------------------------------------------------
  155. # GET /<logs-{now/d}-*>,<logs-{now/d-1d}-*>,<logs-{now/d-2d}-*>/_search
  156. GET /%3Clogs-%7Bnow%2Fd%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-1d%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-2d%7D-*%3E/_search
  157. --------------------------------------------------
  158. // CONSOLE
  159. // TEST[continued]
  160. // TEST[s/now/2016.10.31||/]
  161. [float]
  162. ==== Defining the new index
  163. The settings, mappings, and aliases for the new index are taken from any
  164. matching <<indices-templates,index templates>>. Additionally, you can specify
  165. `settings`, `mappings`, and `aliases` in the body of the request, just like the
  166. <<indices-create-index,create index>> API. Values specified in the request
  167. override any values set in matching index templates. For example, the following
  168. `rollover` request overrides the `index.number_of_shards` setting:
  169. [source,js]
  170. --------------------------------------------------
  171. PUT /logs-000001
  172. {
  173. "aliases": {
  174. "logs_write": {}
  175. }
  176. }
  177. POST /logs_write/_rollover
  178. {
  179. "conditions" : {
  180. "max_age": "7d",
  181. "max_docs": 1000,
  182. "max_size": "5gb"
  183. },
  184. "settings": {
  185. "index.number_of_shards": 2
  186. }
  187. }
  188. --------------------------------------------------
  189. // CONSOLE
  190. [float]
  191. ==== Dry run
  192. The rollover API supports `dry_run` mode, where request conditions can be
  193. checked without performing the actual rollover:
  194. [source,js]
  195. --------------------------------------------------
  196. PUT /logs-000001
  197. {
  198. "aliases": {
  199. "logs_write": {}
  200. }
  201. }
  202. POST /logs_write/_rollover?dry_run
  203. {
  204. "conditions" : {
  205. "max_age": "7d",
  206. "max_docs": 1000,
  207. "max_size": "5gb"
  208. }
  209. }
  210. --------------------------------------------------
  211. // CONSOLE
  212. [float]
  213. ==== Wait For Active Shards
  214. Because the rollover operation creates a new index to rollover to, the
  215. <<create-index-wait-for-active-shards,`wait_for_active_shards`>> setting on
  216. index creation applies to the rollover action as well.
  217. [[indices-rollover-is-write-index]]
  218. [float]
  219. ==== Write Index Alias Behavior
  220. The rollover alias when rolling over a write index that has `is_write_index` explicitly set to `true` is not
  221. swapped during rollover actions. Since having an alias point to multiple indices is ambiguous in distinguishing
  222. which is the correct write index to roll over, it is not valid to rollover an alias that points to multiple indices.
  223. For this reason, the default behavior is to swap which index is being pointed to by the write-oriented alias. This
  224. was `logs_write` in some of the above examples. Since setting `is_write_index` enables an alias to point to multiple indices
  225. while also being explicit as to which is the write index that rollover should target, removing the alias from the rolled over
  226. index is not necessary. This simplifies things by allowing for one alias to behave both as the write and read aliases for
  227. indices that are being managed with Rollover.
  228. Look at the behavior of the aliases in the following example where `is_write_index` is set on the rolled over index.
  229. [source,js]
  230. --------------------------------------------------
  231. PUT my_logs_index-000001
  232. {
  233. "aliases": {
  234. "logs": { "is_write_index": true } <1>
  235. }
  236. }
  237. PUT logs/_doc/1
  238. {
  239. "message": "a dummy log"
  240. }
  241. POST logs/_refresh
  242. POST /logs/_rollover
  243. {
  244. "conditions": {
  245. "max_docs": "1"
  246. }
  247. }
  248. PUT logs/_doc/2 <2>
  249. {
  250. "message": "a newer log"
  251. }
  252. --------------------------------------------------
  253. // CONSOLE
  254. <1> configures `my_logs_index` as the write index for the `logs` alias
  255. <2> newly indexed documents against the `logs` alias will write to the new index
  256. [source,console-result]
  257. --------------------------------------------------
  258. {
  259. "_index" : "my_logs_index-000002",
  260. "_type" : "_doc",
  261. "_id" : "2",
  262. "_version" : 1,
  263. "result" : "created",
  264. "_shards" : {
  265. "total" : 2,
  266. "successful" : 1,
  267. "failed" : 0
  268. },
  269. "_seq_no" : 0,
  270. "_primary_term" : 1
  271. }
  272. --------------------------------------------------
  273. //////////////////////////
  274. [source,js]
  275. --------------------------------------------------
  276. GET _alias
  277. --------------------------------------------------
  278. // CONSOLE
  279. // TEST[continued]
  280. //////////////////////////
  281. After the rollover, the alias metadata for the two indices will have the `is_write_index` setting
  282. reflect each index's role, with the newly created index as the write index.
  283. [source,console-result]
  284. --------------------------------------------------
  285. {
  286. "my_logs_index-000002": {
  287. "aliases": {
  288. "logs": { "is_write_index": true }
  289. }
  290. },
  291. "my_logs_index-000001": {
  292. "aliases": {
  293. "logs": { "is_write_index" : false }
  294. }
  295. }
  296. }
  297. --------------------------------------------------