change-mappings-and-settings.asciidoc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. [[modify-data-streams]]
  2. == Modify a data stream
  3. [discrete]
  4. [[data-streams-change-mappings-and-settings]]
  5. === Change mappings and settings for a data stream
  6. Each data stream has a <<create-index-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.
  12. If you later need to change the mappings or settings for a data stream, you have
  13. a few options:
  14. * <<add-new-field-mapping-to-a-data-stream>>
  15. * <<change-existing-field-mapping-in-a-data-stream>>
  16. * <<change-dynamic-index-setting-for-a-data-stream>>
  17. * <<change-static-index-setting-for-a-data-stream>>
  18. TIP: If your changes include modifications to existing field mappings or
  19. <<index-modules-settings,static index settings>>, a reindex is often required to
  20. apply the changes to a data stream's backing indices. If you are already
  21. performing a reindex, you can use the same process to add new field
  22. mappings and change <<index-modules-settings,dynamic index settings>>. See
  23. <<data-streams-use-reindex-to-change-mappings-settings>>.
  24. ////
  25. [source,console]
  26. ----
  27. PUT /_ilm/policy/my-data-stream-policy
  28. {
  29. "policy": {
  30. "phases": {
  31. "hot": {
  32. "actions": {
  33. "rollover": {
  34. "max_primary_shard_size": "25GB"
  35. }
  36. }
  37. },
  38. "delete": {
  39. "min_age": "30d",
  40. "actions": {
  41. "delete": {}
  42. }
  43. }
  44. }
  45. }
  46. }
  47. PUT /_index_template/my-data-stream-template
  48. {
  49. "index_patterns": [ "my-data-stream*" ],
  50. "data_stream": { }
  51. }
  52. PUT /_index_template/new-data-stream-template
  53. {
  54. "index_patterns": [ "new-data-stream*" ],
  55. "data_stream": { }
  56. }
  57. PUT /_data_stream/my-data-stream
  58. POST /my-data-stream/_rollover/
  59. PUT /_data_stream/new-data-stream
  60. ----
  61. // TESTSETUP
  62. [source,console]
  63. ----
  64. DELETE /_data_stream/*
  65. DELETE /_index_template/*
  66. DELETE /_ilm/policy/my-data-stream-policy
  67. ----
  68. // TEARDOWN
  69. ////
  70. [discrete]
  71. [[add-new-field-mapping-to-a-data-stream]]
  72. ==== Add a new field mapping to a data stream
  73. To add a mapping for a new field to a data stream, following these steps:
  74. . Update the index template used by the data stream. This ensures the new
  75. field mapping is added to future backing indices created for the stream.
  76. +
  77. --
  78. For example, `my-data-stream-template` is an existing index template used by
  79. `my-data-stream`.
  80. The following <<index-templates,create or update index template>> request adds a mapping
  81. for a new field, `message`, to the template.
  82. [source,console]
  83. ----
  84. PUT /_index_template/my-data-stream-template
  85. {
  86. "index_patterns": [ "my-data-stream*" ],
  87. "data_stream": { },
  88. "priority": 500,
  89. "template": {
  90. "mappings": {
  91. "properties": {
  92. "message": { <1>
  93. "type": "text"
  94. }
  95. }
  96. }
  97. }
  98. }
  99. ----
  100. <1> Adds a mapping for the new `message` field.
  101. --
  102. . Use the <<indices-put-mapping,update mapping API>> to add the new field mapping
  103. to the data stream. By default, this adds the mapping to the stream's existing
  104. backing indices, including the write index.
  105. +
  106. --
  107. The following update mapping API request adds the new `message` field mapping to
  108. `my-data-stream`.
  109. [source,console]
  110. ----
  111. PUT /my-data-stream/_mapping
  112. {
  113. "properties": {
  114. "message": {
  115. "type": "text"
  116. }
  117. }
  118. }
  119. ----
  120. --
  121. +
  122. To add the mapping only to the stream's write index, set the update mapping API's
  123. `write_index_only` query parameter to `true`.
  124. +
  125. --
  126. The following update mapping request adds the new `message` field mapping only to
  127. `my-data-stream`'s write index. The new field mapping is not added to
  128. the stream's other backing indices.
  129. [source,console]
  130. ----
  131. PUT /my-data-stream/_mapping?write_index_only=true
  132. {
  133. "properties": {
  134. "message": {
  135. "type": "text"
  136. }
  137. }
  138. }
  139. ----
  140. --
  141. [discrete]
  142. [[change-existing-field-mapping-in-a-data-stream]]
  143. ==== Change an existing field mapping in a data stream
  144. The documentation for each <<mapping-params,mapping parameter>> indicates
  145. whether you can update it for an existing field using the
  146. <<indices-put-mapping,update mapping API>>. To update these parameters for an
  147. existing field, follow these steps:
  148. . Update the index template used by the data stream. This ensures the updated
  149. field mapping is added to future backing indices created for the stream.
  150. +
  151. --
  152. For example, `my-data-stream-template` is an existing index template used by
  153. `my-data-stream`.
  154. The following <<index-templates,create or update index template>> request changes the
  155. argument for the `host.ip` field's <<ignore-malformed,`ignore_malformed`>>
  156. mapping parameter to `true`.
  157. [source,console]
  158. ----
  159. PUT /_index_template/my-data-stream-template
  160. {
  161. "index_patterns": [ "my-data-stream*" ],
  162. "data_stream": { },
  163. "priority": 500,
  164. "template": {
  165. "mappings": {
  166. "properties": {
  167. "host": {
  168. "properties": {
  169. "ip": {
  170. "type": "ip",
  171. "ignore_malformed": true <1>
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. ----
  180. <1> Changes the `host.ip` field's `ignore_malformed` value to `true`.
  181. --
  182. . Use the <<indices-put-mapping,update mapping API>> to apply the mapping changes
  183. to the data stream. By default, this applies the changes to the stream's
  184. existing backing indices, including the write index.
  185. +
  186. --
  187. The following <<indices-put-mapping,update mapping API>> request targets
  188. `my-data-stream`. The request changes the argument for the `host.ip`
  189. field's `ignore_malformed` mapping parameter to `true`.
  190. [source,console]
  191. ----
  192. PUT /my-data-stream/_mapping
  193. {
  194. "properties": {
  195. "host": {
  196. "properties": {
  197. "ip": {
  198. "type": "ip",
  199. "ignore_malformed": true
  200. }
  201. }
  202. }
  203. }
  204. }
  205. ----
  206. --
  207. +
  208. To apply the mapping changes only to the stream's write index, set the put
  209. mapping API's `write_index_only` query parameter to `true`.
  210. +
  211. --
  212. The following update mapping request changes the `host.ip` field's mapping only for
  213. `my-data-stream`'s write index. The change is not applied to the
  214. stream's other backing indices.
  215. [source,console]
  216. ----
  217. PUT /my-data-stream/_mapping?write_index_only=true
  218. {
  219. "properties": {
  220. "host": {
  221. "properties": {
  222. "ip": {
  223. "type": "ip",
  224. "ignore_malformed": true
  225. }
  226. }
  227. }
  228. }
  229. }
  230. ----
  231. --
  232. Except for supported mapping parameters, we don't recommend you change the
  233. mapping or field data type of existing fields, even in a data stream's matching
  234. index template or its backing indices. Changing the mapping of an existing
  235. field could invalidate any data that’s already indexed.
  236. If you need to change the mapping of an existing field, create a new
  237. data stream and reindex your data into it. See
  238. <<data-streams-use-reindex-to-change-mappings-settings>>.
  239. [discrete]
  240. [[change-dynamic-index-setting-for-a-data-stream]]
  241. ==== Change a dynamic index setting for a data stream
  242. To change a <<index-modules-settings,dynamic index setting>> for a data stream,
  243. follow these steps:
  244. . Update the index template used by the data stream. This ensures the setting is
  245. applied to future backing indices created for the stream.
  246. +
  247. --
  248. For example, `my-data-stream-template` is an existing index template used by
  249. `my-data-stream`.
  250. The following <<index-templates,create or update index template>> request changes the
  251. template's `index.refresh_interval` index setting to `30s` (30 seconds).
  252. [source,console]
  253. ----
  254. PUT /_index_template/my-data-stream-template
  255. {
  256. "index_patterns": [ "my-data-stream*" ],
  257. "data_stream": { },
  258. "priority": 500,
  259. "template": {
  260. "settings": {
  261. "index.refresh_interval": "30s" <1>
  262. }
  263. }
  264. }
  265. ----
  266. <1> Changes the `index.refresh_interval` setting to `30s` (30 seconds).
  267. --
  268. . Use the <<indices-update-settings,update index settings API>> to update the
  269. index setting for the data stream. By default, this applies the setting to
  270. the stream's existing backing indices, including the write index.
  271. +
  272. --
  273. The following update index settings API request updates the
  274. `index.refresh_interval` setting for `my-data-stream`.
  275. [source,console]
  276. ----
  277. PUT /my-data-stream/_settings
  278. {
  279. "index": {
  280. "refresh_interval": "30s"
  281. }
  282. }
  283. ----
  284. --
  285. IMPORTANT: To change the `index.lifecycle.name` setting, first use the
  286. <<ilm-remove-policy,remove policy API>> to remove the existing {ilm-init}
  287. policy. See <<switch-lifecycle-policies>>.
  288. [discrete]
  289. [[change-static-index-setting-for-a-data-stream]]
  290. ==== Change a static index setting for a data stream
  291. <<index-modules-settings,Static index settings>> can only be set when a backing
  292. index is created. You cannot update static index settings using the
  293. <<indices-update-settings,update index settings API>>.
  294. To apply a new static setting to future backing indices, update the index
  295. template used by the data stream. The setting is automatically applied to any
  296. backing index created after the update.
  297. For example, `my-data-stream-template` is an existing index template used by
  298. `my-data-stream`.
  299. The following <<index-templates,create or update index template API>> requests
  300. adds new `sort.field` and `sort.order index` settings to the template.
  301. [source,console]
  302. ----
  303. PUT /_index_template/my-data-stream-template
  304. {
  305. "index_patterns": [ "my-data-stream*" ],
  306. "data_stream": { },
  307. "priority": 500,
  308. "template": {
  309. "settings": {
  310. "sort.field": [ "@timestamp"], <1>
  311. "sort.order": [ "desc"] <2>
  312. }
  313. }
  314. }
  315. ----
  316. <1> Adds the `sort.field` index setting.
  317. <2> Adds the `sort.order` index setting.
  318. If wanted, you can <<manually-roll-over-a-data-stream,roll over the data
  319. stream>> to immediately apply the setting to the data stream’s write index. This
  320. affects any new data added to the stream after the rollover. However, it does
  321. not affect the data stream's existing backing indices or existing data.
  322. To apply static setting changes to existing backing indices, you must create a
  323. new data stream and reindex your data into it. See
  324. <<data-streams-use-reindex-to-change-mappings-settings>>.
  325. [discrete]
  326. [[data-streams-use-reindex-to-change-mappings-settings]]
  327. ==== Use reindex to change mappings or settings
  328. You can use a reindex to change the mappings or settings of a data stream. This
  329. is often required to change the data type of an existing field or update static
  330. index settings for backing indices.
  331. To reindex a data stream, first create or update an index template so that it
  332. contains the wanted mapping or setting changes. You can then reindex the
  333. existing data stream into a new stream matching the template. This applies the
  334. mapping and setting changes in the template to each document and backing index
  335. added to the new data stream. These changes also affect any future backing
  336. index created by the new stream.
  337. Follow these steps:
  338. . Choose a name or index pattern for a new data stream. This new data
  339. stream will contain data from your existing stream.
  340. +
  341. --
  342. You can use the resolve index API to check if the name or pattern matches any
  343. existing indices, aliases, or data streams. If so, you should consider using
  344. another name or pattern.
  345. The following resolve index API request checks for any existing indices,
  346. aliases, or data streams that start with `new-data-stream`. If not, the
  347. `new-data-stream*` index pattern can be used to create a new data stream.
  348. [source,console]
  349. ----
  350. GET /_resolve/index/new-data-stream*
  351. ----
  352. The API returns the following response, indicating no existing targets match
  353. this pattern.
  354. [source,console-result]
  355. ----
  356. {
  357. "indices": [ ],
  358. "aliases": [ ],
  359. "data_streams": [ ]
  360. }
  361. ----
  362. // TESTRESPONSE[s/"data_streams": \[ \]/"data_streams": $body.data_streams/]
  363. --
  364. . Create or update an index template. This template should contain the
  365. mappings and settings you'd like to apply to the new data stream's backing
  366. indices.
  367. +
  368. This index template must meet the
  369. <<create-index-template,requirements for a data stream template>>. It
  370. should also contain your previously chosen name or index pattern in the
  371. `index_patterns` property.
  372. +
  373. TIP: If you are only adding or changing a few things, we recommend you create a
  374. new template by copying an existing one and modifying it as needed.
  375. +
  376. --
  377. For example, `my-data-stream-template` is an existing index template used by
  378. `my-data-stream`.
  379. The following <<index-templates,create or update index template API>> request
  380. creates a new index template, `new-data-stream-template`.
  381. `new-data-stream-template` uses `my-data-stream-template` as its basis, with the
  382. following changes:
  383. * The index pattern in `index_patterns` matches any index or data stream
  384. starting with `new-data-stream`.
  385. * The `@timestamp` field mapping uses the `date_nanos` field data type rather
  386. than the `date` data type.
  387. * The template includes `sort.field` and `sort.order` index settings, which were
  388. not in the original `my-data-stream-template` template.
  389. [source,console]
  390. ----
  391. PUT /_index_template/new-data-stream-template
  392. {
  393. "index_patterns": [ "new-data-stream*" ],
  394. "data_stream": { },
  395. "priority": 500,
  396. "template": {
  397. "mappings": {
  398. "properties": {
  399. "@timestamp": {
  400. "type": "date_nanos" <1>
  401. }
  402. }
  403. },
  404. "settings": {
  405. "sort.field": [ "@timestamp"], <2>
  406. "sort.order": [ "desc"] <3>
  407. }
  408. }
  409. }
  410. ----
  411. <1> Changes the `@timestamp` field mapping to the `date_nanos` field data type.
  412. <2> Adds the `sort.field` index setting.
  413. <3> Adds the `sort.order` index setting.
  414. --
  415. . Use the <<indices-create-data-stream,create data stream API>> to manually
  416. create the new data stream. The name of the data stream must match the index
  417. pattern defined in the new template's `index_patterns` property.
  418. +
  419. We do not recommend <<create-data-stream,indexing new data
  420. to create this data stream>>. Later, you will reindex older data from an
  421. existing data stream into this new stream. This could result in one or more
  422. backing indices that contains a mix of new and old data.
  423. +
  424. [IMPORTANT]
  425. ======
  426. [[data-stream-mix-new-old-data]]
  427. *Mixing new and old data in a data stream*
  428. While mixing new and old data is safe, it could interfere with data retention.
  429. If you delete older indices, you could accidentally delete a backing index that
  430. contains both new and old data. To prevent premature data loss, you would need
  431. to retain such a backing index until you are ready to delete its newest data.
  432. ======
  433. +
  434. --
  435. The following create data stream API request targets `new-data-stream`, which
  436. matches the index pattern for `new-data-stream-template`.
  437. Because no existing index or data stream uses this name, this request creates
  438. the `new-data-stream` data stream.
  439. [source,console]
  440. ----
  441. PUT /_data_stream/new-data-stream
  442. ----
  443. // TEST[s/new-data-stream/new-data-stream-two/]
  444. --
  445. . If you do not want to mix new and old data in your new data stream, pause the
  446. indexing of new documents. While mixing old and new data is safe, it could
  447. interfere with data retention. See <<data-stream-mix-new-old-data,Mixing new and
  448. old data in a data stream>>.
  449. . If you use {ilm-init} to <<getting-started-index-lifecycle-management,automate
  450. rollover>>, reduce the {ilm-init} poll interval. This ensures the current write
  451. index doesn’t grow too large while waiting for the rollover check. By default,
  452. {ilm-init} checks rollover conditions every 10 minutes.
  453. +
  454. --
  455. The following <<cluster-update-settings,cluster update settings API>> request
  456. lowers the `indices.lifecycle.poll_interval` setting to `1m` (one minute).
  457. [source,console]
  458. ----
  459. PUT /_cluster/settings
  460. {
  461. "persistent": {
  462. "indices.lifecycle.poll_interval": "1m"
  463. }
  464. }
  465. ----
  466. --
  467. . Reindex your data to the new data stream using an `op_type` of `create`.
  468. +
  469. If you want to partition the data in the order in which it was originally
  470. indexed, you can run separate reindex requests. These reindex requests can use
  471. individual backing indices as the source. You can use the
  472. <<indices-get-data-stream,get data stream API>> to retrieve a list of backing
  473. indices.
  474. +
  475. --
  476. For example, you plan to reindex data from `my-data-stream` into
  477. `new-data-stream`. However, you want to submit a separate reindex request for
  478. each backing index in `my-data-stream`, starting with the oldest backing index.
  479. This preserves the order in which the data was originally indexed.
  480. The following get data stream API request retrieves information about
  481. `my-data-stream`, including a list of its backing indices.
  482. [source,console]
  483. ----
  484. GET /_data_stream/my-data-stream
  485. ----
  486. The response's `indices` property contains an array of the stream's current
  487. backing indices. The first item in the array contains information about the
  488. stream's oldest backing index.
  489. [source,console-result]
  490. ----
  491. {
  492. "data_streams": [
  493. {
  494. "name": "my-data-stream",
  495. "timestamp_field": {
  496. "name": "@timestamp"
  497. },
  498. "indices": [
  499. {
  500. "index_name": ".ds-my-data-stream-2099.03.07-000001", <1>
  501. "index_uuid": "Gpdiyq8sRuK9WuthvAdFbw"
  502. },
  503. {
  504. "index_name": ".ds-my-data-stream-2099.03.08-000002",
  505. "index_uuid": "_eEfRrFHS9OyhqWntkgHAQ"
  506. }
  507. ],
  508. "generation": 2,
  509. "status": "GREEN",
  510. "template": "my-data-stream-template",
  511. "hidden": false,
  512. "system": false,
  513. "allow_custom_routing": false,
  514. "replicated": false
  515. }
  516. ]
  517. }
  518. ----
  519. // TESTRESPONSE[s/"index_uuid": "Gpdiyq8sRuK9WuthvAdFbw"/"index_uuid": $body.data_streams.0.indices.0.index_uuid/]
  520. // TESTRESPONSE[s/"index_uuid": "_eEfRrFHS9OyhqWntkgHAQ"/"index_uuid": $body.data_streams.0.indices.1.index_uuid/]
  521. // TESTRESPONSE[s/"index_name": ".ds-my-data-stream-2099.03.07-000001"/"index_name": $body.data_streams.0.indices.0.index_name/]
  522. // TESTRESPONSE[s/"index_name": ".ds-my-data-stream-2099.03.08-000002"/"index_name": $body.data_streams.0.indices.1.index_name/]
  523. // TESTRESPONSE[s/"status": "GREEN"/"status": "YELLOW"/]
  524. <1> First item in the `indices` array for `my-data-stream`. This item contains
  525. information about the stream's oldest backing index,
  526. `.ds-my-data-stream-2099.03.07-000001`.
  527. The following <<docs-reindex,reindex API>> request copies documents from
  528. `.ds-my-data-stream-2099.03.07-000001` to `new-data-stream`. The request's
  529. `op_type` is `create`.
  530. [source,console]
  531. ----
  532. POST /_reindex
  533. {
  534. "source": {
  535. "index": ".ds-my-data-stream-2099.03.07-000001"
  536. },
  537. "dest": {
  538. "index": "new-data-stream",
  539. "op_type": "create"
  540. }
  541. }
  542. ----
  543. // TEST[setup:my_index]
  544. // TEST[s/.ds-my-data-stream-2099.03.07-000001/my-index-000001/]
  545. --
  546. +
  547. You can also use a query to reindex only a subset of documents with each
  548. request.
  549. +
  550. --
  551. The following <<docs-reindex,reindex API>> request copies documents from
  552. `my-data-stream` to `new-data-stream`. The request
  553. uses a <<query-dsl-range-query,`range` query>> to only reindex documents with a
  554. timestamp within the last week. Note the request's `op_type` is `create`.
  555. [source,console]
  556. ----
  557. POST /_reindex
  558. {
  559. "source": {
  560. "index": "my-data-stream",
  561. "query": {
  562. "range": {
  563. "@timestamp": {
  564. "gte": "now-7d/d",
  565. "lte": "now/d"
  566. }
  567. }
  568. }
  569. },
  570. "dest": {
  571. "index": "new-data-stream",
  572. "op_type": "create"
  573. }
  574. }
  575. ----
  576. --
  577. . If you previously changed your {ilm-init} poll interval, change it back to its
  578. original value when reindexing is complete. This prevents unnecessary load on
  579. the master node.
  580. +
  581. --
  582. The following cluster update settings API request resets the
  583. `indices.lifecycle.poll_interval` setting to its default value.
  584. [source,console]
  585. ----
  586. PUT /_cluster/settings
  587. {
  588. "persistent": {
  589. "indices.lifecycle.poll_interval": null
  590. }
  591. }
  592. ----
  593. --
  594. . Resume indexing using the new data stream. Searches on this stream will now
  595. query your new data and the reindexed data.
  596. . Once you have verified that all reindexed data is available in the new
  597. data stream, you can safely remove the old stream.
  598. +
  599. --
  600. The following <<indices-delete-data-stream,delete data stream API>> request
  601. deletes `my-data-stream`. This request also deletes the stream's
  602. backing indices and any data they contain.
  603. [source,console]
  604. ----
  605. DELETE /_data_stream/my-data-stream
  606. ----
  607. --
  608. [discrete]
  609. [[data-streams-change-alias]]
  610. === Update or add an alias to a data stream
  611. Use the <<indices-aliases,aliases API>> to update an existing data stream's
  612. aliases. Changing an existing data stream's aliases in its index pattern has no
  613. effect.
  614. include::../alias.asciidoc[tag=alias-multiple-actions-example]