rollover-index.asciidoc 9.8 KB

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