change-mappings-and-settings.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. [[data-streams-change-mappings-and-settings]]
  2. == Change mappings and settings of backing indices
  3. ++++
  4. <titleabbrev>Change mappings and settings</titleabbrev>
  5. ++++
  6. Each data stream has a <<create-a-data-stream-template,matching index
  7. template>>. Mappings and index settings from this template are applied to new
  8. backing indices created for the stream. This includes the stream's first
  9. backing index, which is auto-generated when the stream is created.
  10. Before creating a data stream, we recommend you carefully consider which
  11. mappings and settings to include in this template. However, if you later need to
  12. change the mappings or settings of a data stream's backing indices, you have a
  13. couple options:
  14. * To apply changes to future backing indices, simply update the index
  15. template used by the data stream. Mapping and setting changes will be
  16. automatically applied to any backing indices created after the update.
  17. +
  18. .*Example*
  19. [%collapsible]
  20. ====
  21. `logs_data_stream` is an existing index template used by the
  22. `logs` data stream.
  23. The following <<indices-templates,put index template API>> makes several
  24. changes to the `logs_data_stream` template:
  25. * It changes the `@timestamp` field mapping from the `date` field datatype to
  26. the `date_nanos` datatype.
  27. * It adds new `sort.field` and `sort.order` index settings.
  28. ////
  29. [source,console]
  30. ----
  31. PUT /_ilm/policy/logs_policy
  32. {
  33. "policy": {
  34. "phases": {
  35. "hot": {
  36. "actions": {
  37. "rollover": {
  38. "max_size": "25GB"
  39. }
  40. }
  41. },
  42. "delete": {
  43. "min_age": "30d",
  44. "actions": {
  45. "delete": {}
  46. }
  47. }
  48. }
  49. }
  50. }
  51. PUT /_index_template/logs_data_stream
  52. {
  53. "index_patterns": [ "logs*" ],
  54. "data_stream": {
  55. "timestamp_field": "@timestamp"
  56. },
  57. "template": {
  58. "mappings": {
  59. "properties": {
  60. "@timestamp": {
  61. "type": "date"
  62. }
  63. }
  64. },
  65. "settings": {
  66. "index.lifecycle.name": "logs_policy"
  67. }
  68. }
  69. }
  70. PUT /logs/_bulk?refresh
  71. {"create":{"_index" : "logs"}}
  72. { "@timestamp": "2020-12-08T11:04:05.000Z" }
  73. {"create":{"_index" : "logs"}}
  74. { "@timestamp": "2020-12-08T11:06:07.000Z" }
  75. {"create":{"_index" : "logs"}}
  76. { "@timestamp": "2020-12-09T11:07:08.000Z" }
  77. ----
  78. ////
  79. [source,console]
  80. ----
  81. PUT /_index_template/logs_data_stream
  82. {
  83. "index_patterns": [ "logs*" ],
  84. "data_stream": {
  85. "timestamp_field": "@timestamp"
  86. },
  87. "template": {
  88. "mappings": {
  89. "properties": {
  90. "@timestamp": {
  91. "type": "date_nanos" <1>
  92. }
  93. }
  94. },
  95. "settings": {
  96. "index.lifecycle.name": "logs_policy",
  97. "sort.field" : [ "@timestamp"], <2>
  98. "sort.order" : [ "desc"] <3>
  99. }
  100. }
  101. }
  102. ----
  103. // TEST[continued]
  104. <1> Changes the `@timestamp` field mapping to the `date_nanos` datatype.
  105. <2> Adds the `sort.field` index setting.
  106. <3> Adds the `sort.order` index setting.
  107. ====
  108. +
  109. If wanted, you can <<manually-roll-over-a-data-stream,roll over the data
  110. stream>> to immediately apply the new mappings and settings to the data stream's
  111. write index. This affects any new data added to the stream after the rollover.
  112. +
  113. .*Example*
  114. [%collapsible]
  115. ====
  116. The following <<indices-rollover-index,rollover API>> request rolls over the
  117. `logs` data stream. This creates a new write index with mappings and index
  118. settings from the recently updated `logs_data_stream` template.
  119. [source,console]
  120. ----
  121. POST /logs/_rollover/
  122. ----
  123. // TEST[continued]
  124. ====
  125. +
  126. IMPORTANT: You cannot use these methods to change the mapping of a data stream's
  127. <<create-a-data-stream-template,timestamp field>>. To change the timestamp
  128. field's mapping, you must reindex the data stream. See
  129. <<data-streams-use-reindex-to-change-mappings-settings>>.
  130. * To apply mapping and setting changes to all existing backing indices and
  131. future ones, you must create a new data stream and reindex your data into it.
  132. See <<data-streams-use-reindex-to-change-mappings-settings>>.
  133. [discrete]
  134. [[data-streams-use-reindex-to-change-mappings-settings]]
  135. === Use reindex to change mappings or settings
  136. To change the mappings or settings for every backing index in a data stream, you
  137. must first create or update an index template so that it contains the
  138. changes. You can then reindex the existing data stream into a new one matching
  139. the template. This applies the mapping and setting changes in the template
  140. to each document and backing index of the data stream destination. These changes
  141. also affect any future backing index created by the new stream.
  142. Follow these steps:
  143. . Choose a name or wildcard (`*`) pattern for a new data stream. This new data
  144. stream will contain data from your existing stream.
  145. +
  146. You can use the resolve index API to check if the name or pattern matches any
  147. existing indices, index aliases, or data streams. If so, you should consider
  148. using another name or pattern.
  149. +
  150. .*Example*
  151. [%collapsible]
  152. ====
  153. The following resolve index API request checks for any existing indices, index
  154. aliases, or data streams that start with `new_logs`. If not, the `new_logs*`
  155. wildcard pattern can be used to create a new data stream.
  156. [source,console]
  157. ----
  158. GET /_resolve/index/new_logs*
  159. ----
  160. // TEST[continued]
  161. The API returns the following response, indicating no existing targets match
  162. this pattern.
  163. [source,console-result]
  164. ----
  165. {
  166. "indices" : [ ],
  167. "aliases" : [ ],
  168. "data_streams" : [ ]
  169. }
  170. ----
  171. ====
  172. . Create or update an index template. This template should contain the
  173. mappings and settings you'd like to apply to the new data stream's backing
  174. indices.
  175. +
  176. This index template must meet the
  177. <<create-a-data-stream-template,requirements for a data stream template>>. It
  178. should also contain your previously chosen name or wildcard pattern in the
  179. `index_patterns` property.
  180. +
  181. TIP: If you are only adding or changing a few things, we recommend you create a
  182. new template by copying an existing one and modifying it as needed.
  183. +
  184. .*Example*
  185. [%collapsible]
  186. ====
  187. `logs_data_stream` is an existing index template used by the
  188. `logs` data stream.
  189. The following <<indices-templates,put index template API>> request creates
  190. a new index template, `new_logs_data_stream`. `new_logs_data_stream`
  191. uses the `logs_data_stream` template as its basis, with the following changes:
  192. * The `index_patterns` wildcard pattern matches any index or data stream
  193. starting with `new_logs`.
  194. * The `@timestamp` field mapping uses the `date_nanos` field datatype rather
  195. than the `date` datatype.
  196. * The template includes `sort.field` and `sort.order` index settings, which were
  197. not in the original `logs_data_stream` template.
  198. [source,console]
  199. ----
  200. PUT /_index_template/new_logs_data_stream
  201. {
  202. "index_patterns": [ "new_logs*" ],
  203. "data_stream": {
  204. "timestamp_field": "@timestamp"
  205. },
  206. "template": {
  207. "mappings": {
  208. "properties": {
  209. "@timestamp": {
  210. "type": "date_nanos" <1>
  211. }
  212. }
  213. },
  214. "settings": {
  215. "index.lifecycle.name": "logs_policy",
  216. "sort.field" : [ "@timestamp"], <2>
  217. "sort.order" : [ "desc"] <3>
  218. }
  219. }
  220. }
  221. ----
  222. // TEST[continued]
  223. <1> Changes the `@timestamp` field mapping to the `date_nanos` field datatype.
  224. <2> Adds the `sort.field` index setting.
  225. <3> Adds the `sort.order` index setting.
  226. ====
  227. . Use the <<indices-create-data-stream,create data stream API>> to manually
  228. create the new data stream. The name of the data stream must match the name or
  229. wildcard pattern defined in the new template's `index_patterns` property.
  230. +
  231. We do not recommend <<index-documents-to-create-a-data-stream,indexing new data
  232. to create this data stream>>. Later, you will reindex older data from an
  233. existing data stream into this new stream. This could result in one or more
  234. backing indices that contains a mix of new and old data.
  235. +
  236. [[data-stream-mix-new-old-data]]
  237. .Mixing new and old data in a data stream
  238. [IMPORTANT]
  239. ====
  240. While mixing new and old data is safe, it could interfere with data retention.
  241. If you delete older indices, you could accidentally delete a backing index that
  242. contains both new and old data. To prevent premature data loss, you would need
  243. to retain such a backing index until you are ready to delete its newest data.
  244. ====
  245. +
  246. .*Example*
  247. [%collapsible]
  248. ====
  249. The following create data stream API request targets `new_logs`, which matches
  250. the wildcard pattern for the `new_logs_data_stream` template. Because no
  251. existing index or data stream uses this name, this request creates the
  252. `new_logs` data stream.
  253. [source,console]
  254. ----
  255. PUT /_data_stream/new_logs
  256. ----
  257. // TEST[continued]
  258. ====
  259. . If you do not want to mix new and old data in your new data stream, pause the
  260. indexing of new documents. While mixing old and new data is safe, it could
  261. interfere with data retention. See <<data-stream-mix-new-old-data,Mixing new and
  262. old data in a data stream>>.
  263. . If you use {ilm-init} to <<getting-started-index-lifecycle-management,automate
  264. rollover>>, reduce the {ilm-init} poll interval. This ensures the current write
  265. index doesn’t grow too large while waiting for the rollover check. By default,
  266. {ilm-init} checks rollover conditions every 10 minutes.
  267. +
  268. .*Example*
  269. [%collapsible]
  270. ====
  271. The following <<cluster-update-settings,update cluster settings API>> request
  272. lowers the `indices.lifecycle.poll_interval` setting to `1m` (one minute).
  273. [source,console]
  274. ----
  275. PUT /_cluster/settings
  276. {
  277. "transient": {
  278. "indices.lifecycle.poll_interval": "1m"
  279. }
  280. }
  281. ----
  282. // TEST[continued]
  283. ////
  284. [source,console]
  285. ----
  286. DELETE /_data_stream/logs
  287. DELETE /_data_stream/new_logs
  288. DELETE /_index_template/logs_data_stream
  289. DELETE /_index_template/new_logs_data_stream
  290. DELETE /_ilm/policy/logs_policy
  291. ----
  292. // TEST[continued]
  293. ////
  294. ====
  295. . Reindex your data to the new data stream using an `op_type` of `create`.
  296. +
  297. If you want to partition the data in the order in which it was originally
  298. indexed, you can run separate reindex requests. These reindex requests can use
  299. individual backing indices as the source. You can use the
  300. <<indices-get-data-stream,get data stream API>> to retrieve a list of backing
  301. indices.
  302. +
  303. .*Example*
  304. [%collapsible]
  305. ====
  306. You plan to reindex data from the `logs` data stream into the newly created
  307. `new_logs` data stream. However, you want to submit a separate reindex request
  308. for each backing index in the `logs` data stream, starting with the oldest
  309. backing index. This preserves the order in which the data was originally
  310. indexed.
  311. The following get data stream API request retrieves information about the `logs`
  312. data stream, including a list of its backing indices.
  313. [source,console]
  314. ----
  315. GET /_data_stream/logs
  316. ----
  317. // TEST[skip: shard failures]
  318. The API returns the following response. Note the `indices` property contains an
  319. array of the stream's current backing indices. The oldest backing index,
  320. `.ds-logs-000001`, is the first item in the array.
  321. [source,console-result]
  322. ----
  323. [
  324. {
  325. "name": "logs",
  326. "timestamp_field": "@timestamp",
  327. "indices": [
  328. {
  329. "index_name": ".ds-logs-000001",
  330. "index_uuid": "DXAE-xcCQTKF93bMm9iawA"
  331. },
  332. {
  333. "index_name": ".ds-logs-000002",
  334. "index_uuid": "Wzxq0VhsQKyPxHhaK3WYAg"
  335. }
  336. ],
  337. "generation": 2
  338. }
  339. ]
  340. ----
  341. // TESTRESPONSE[skip:unable to assert responses with top level array]
  342. The following <<docs-reindex,reindex API>> request copies documents from
  343. `.ds-logs-000001` to the `new_logs` data stream. Note the request's `op_type` is
  344. `create`.
  345. ////
  346. [source,console]
  347. ----
  348. PUT /_index_template/logs_data_stream
  349. {
  350. "index_patterns": [ "logs*" ],
  351. "data_stream": {
  352. "timestamp_field": "@timestamp"
  353. },
  354. "template": {
  355. "mappings": {
  356. "properties": {
  357. "@timestamp": {
  358. "type": "date"
  359. }
  360. }
  361. }
  362. }
  363. }
  364. PUT /_index_template/new_logs_data_stream
  365. {
  366. "index_patterns": [ "new_logs*" ],
  367. "data_stream": {
  368. "timestamp_field": "@timestamp"
  369. },
  370. "template": {
  371. "mappings": {
  372. "properties": {
  373. "@timestamp": {
  374. "type": "date"
  375. }
  376. }
  377. }
  378. }
  379. }
  380. PUT /_data_stream/logs
  381. PUT /_data_stream/new_logs
  382. ----
  383. ////
  384. [source,console]
  385. ----
  386. POST /_reindex
  387. {
  388. "source": {
  389. "index": ".ds-logs-000001"
  390. },
  391. "dest": {
  392. "index": "new_logs",
  393. "op_type": "create"
  394. }
  395. }
  396. ----
  397. // TEST[continued]
  398. ====
  399. +
  400. You can also use a query to reindex only a subset of documents with each
  401. request.
  402. +
  403. .*Example*
  404. [%collapsible]
  405. ====
  406. The following <<docs-reindex,reindex API>> request copies documents from the
  407. `logs` data stream to the `new_logs` data stream. The request uses a
  408. <<query-dsl-range-query,`range` query>> to only reindex documents with a
  409. timestamp within the last week. Note the request's `op_type` is `create`.
  410. [source,console]
  411. ----
  412. POST /_reindex
  413. {
  414. "source": {
  415. "index": "logs",
  416. "query": {
  417. "range": {
  418. "@timestamp": {
  419. "gte": "now-7d/d",
  420. "lte": "now/d"
  421. }
  422. }
  423. }
  424. },
  425. "dest": {
  426. "index": "new_logs",
  427. "op_type": "create"
  428. }
  429. }
  430. ----
  431. // TEST[continued]
  432. ====
  433. . If you previously changed your {ilm-init} poll interval, change it back to its
  434. original value when reindexing is complete. This prevents unnecessary load on
  435. the master node.
  436. +
  437. .*Example*
  438. [%collapsible]
  439. ====
  440. The following update cluster settings API request resets the
  441. `indices.lifecycle.poll_interval` setting to its default value, 10 minutes.
  442. [source,console]
  443. ----
  444. PUT /_cluster/settings
  445. {
  446. "transient": {
  447. "indices.lifecycle.poll_interval": null
  448. }
  449. }
  450. ----
  451. // TEST[continued]
  452. ====
  453. . Resume indexing using the new data stream. Searches on this stream will now
  454. query your new data and the reindexed data.
  455. . Once you have verified that all reindexed data is available in the new
  456. data stream, you can safely remove the old stream.
  457. +
  458. .*Example*
  459. [%collapsible]
  460. ====
  461. The following <<indices-delete-data-stream,delete data stream API>> request
  462. deletes the `logs` data stream. This request also deletes the stream's backing
  463. indices and any data they contain.
  464. [source,console]
  465. ----
  466. DELETE /_data_stream/logs
  467. ----
  468. // TEST[continued]
  469. ====
  470. ////
  471. [source,console]
  472. ----
  473. DELETE /_data_stream/new_logs
  474. DELETE /_index_template/logs_data_stream
  475. DELETE /_index_template/new_logs_data_stream
  476. ----
  477. // TEST[continued]
  478. ////