change-mappings-and-settings.asciidoc 19 KB

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