change-mappings-and-settings.asciidoc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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-index-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. IMPORTANT: To change the `index.lifecycle.name` setting, first use the
  284. <<ilm-remove-policy,remove policy API>> to remove the existing {ilm-init}
  285. policy. See <<switch-lifecycle-policies>>.
  286. [discrete]
  287. [[change-static-index-setting-for-a-data-stream]]
  288. === Change a static index setting for a data stream
  289. <<index-modules-settings,Static index settings>> can only be set when a backing
  290. index is created. You cannot update static index settings using the
  291. <<indices-update-settings,update index settings API>>.
  292. To apply a new static setting to future backing indices, update the index
  293. template used by the data stream. The setting is automatically applied to any
  294. backing index created after the update.
  295. For example, `my-data-stream-template` is an existing index template used by
  296. `my-data-stream`.
  297. The following <<index-templates,create or update index template API>> requests
  298. adds new `sort.field` and `sort.order index` settings to the template.
  299. [source,console]
  300. ----
  301. PUT /_index_template/my-data-stream-template
  302. {
  303. "index_patterns": [ "my-data-stream*" ],
  304. "data_stream": { },
  305. "priority": 500,
  306. "template": {
  307. "settings": {
  308. "sort.field": [ "@timestamp"], <1>
  309. "sort.order": [ "desc"] <2>
  310. }
  311. }
  312. }
  313. ----
  314. <1> Adds the `sort.field` index setting.
  315. <2> Adds the `sort.order` index setting.
  316. If wanted, you can <<manually-roll-over-a-data-stream,roll over the data
  317. stream>> to immediately apply the setting to the data stream’s write index. This
  318. affects any new data added to the stream after the rollover. However, it does
  319. not affect the data stream's existing backing indices or existing data.
  320. To apply static setting changes to existing backing indices, you must create a
  321. new data stream and reindex your data into it. See
  322. <<data-streams-use-reindex-to-change-mappings-settings>>.
  323. [discrete]
  324. [[data-streams-use-reindex-to-change-mappings-settings]]
  325. === Use reindex to change mappings or settings
  326. You can use a reindex to change the mappings or settings of a data stream. This
  327. is often required to change the data type of an existing field or update static
  328. index settings for backing indices.
  329. To reindex a data stream, first create or update an index template so that it
  330. contains the wanted mapping or setting changes. You can then reindex the
  331. existing data stream into a new stream matching the template. This applies the
  332. mapping and setting changes in the template to each document and backing index
  333. added to the new data stream. These changes also affect any future backing
  334. index created by the new stream.
  335. Follow these steps:
  336. . Choose a name or index pattern for a new data stream. This new data
  337. stream will contain data from your existing stream.
  338. +
  339. You can use the resolve index API to check if the name or pattern matches any
  340. existing indices, aliases, or data streams. If so, you should consider using
  341. another name or pattern.
  342. --
  343. The following resolve index API request checks for any existing indices,
  344. aliases, or data streams that start with `new-data-stream`. If not, the
  345. `new-data-stream*` index pattern can be used to create a new data stream.
  346. [source,console]
  347. ----
  348. GET /_resolve/index/new-data-stream*
  349. ----
  350. The API returns the following response, indicating no existing targets match
  351. this pattern.
  352. [source,console-result]
  353. ----
  354. {
  355. "indices": [ ],
  356. "aliases": [ ],
  357. "data_streams": [ ]
  358. }
  359. ----
  360. // TESTRESPONSE[s/"data_streams": \[ \]/"data_streams": $body.data_streams/]
  361. --
  362. . Create or update an index template. This template should contain the
  363. mappings and settings you'd like to apply to the new data stream's backing
  364. indices.
  365. +
  366. This index template must meet the
  367. <<create-index-template,requirements for a data stream template>>. It
  368. should also contain your previously chosen name or index pattern in the
  369. `index_patterns` property.
  370. +
  371. TIP: If you are only adding or changing a few things, we recommend you create a
  372. new template by copying an existing one and modifying it as needed.
  373. +
  374. --
  375. For example, `my-data-stream-template` is an existing index template used by
  376. `my-data-stream`.
  377. The following <<index-templates,create or update index template API>> request
  378. creates a new index template, `new-data-stream-template`.
  379. `new-data-stream-template` uses `my-data-stream-template` as its basis, with the
  380. following changes:
  381. * The index pattern in `index_patterns` matches any index or data stream
  382. starting with `new-data-stream`.
  383. * The `@timestamp` field mapping uses the `date_nanos` field data type rather
  384. than the `date` data type.
  385. * The template includes `sort.field` and `sort.order` index settings, which were
  386. not in the original `my-data-stream-template` template.
  387. [source,console]
  388. ----
  389. PUT /_index_template/new-data-stream-template
  390. {
  391. "index_patterns": [ "new-data-stream*" ],
  392. "data_stream": { },
  393. "priority": 500,
  394. "template": {
  395. "mappings": {
  396. "properties": {
  397. "@timestamp": {
  398. "type": "date_nanos" <1>
  399. }
  400. }
  401. },
  402. "settings": {
  403. "sort.field": [ "@timestamp"], <2>
  404. "sort.order": [ "desc"] <3>
  405. }
  406. }
  407. }
  408. ----
  409. <1> Changes the `@timestamp` field mapping to the `date_nanos` field data type.
  410. <2> Adds the `sort.field` index setting.
  411. <3> Adds the `sort.order` index setting.
  412. --
  413. . Use the <<indices-create-data-stream,create data stream API>> to manually
  414. create the new data stream. The name of the data stream must match the index
  415. pattern defined in the new template's `index_patterns` property.
  416. +
  417. We do not recommend <<create-data-stream,indexing new data
  418. to create this data stream>>. Later, you will reindex older data from an
  419. existing data stream into this new stream. This could result in one or more
  420. backing indices that contains a mix of new and old data.
  421. +
  422. [[data-stream-mix-new-old-data]]
  423. .Mixing new and old data in a data stream
  424. [IMPORTANT]
  425. ====
  426. While mixing new and old data is safe, it could interfere with data retention.
  427. If you delete older indices, you could accidentally delete a backing index that
  428. contains both new and old data. To prevent premature data loss, you would need
  429. to retain such a backing index until you are ready to delete its newest data.
  430. ====
  431. +
  432. --
  433. The following create data stream API request targets `new-data-stream`, which
  434. matches the index pattern for `new-data-stream-template`.
  435. Because no existing index or data stream uses this name, this request creates
  436. the `new-data-stream` data stream.
  437. [source,console]
  438. ----
  439. PUT /_data_stream/new-data-stream
  440. ----
  441. // TEST[s/new-data-stream/new-data-stream-two/]
  442. --
  443. . If you do not want to mix new and old data in your new data stream, pause the
  444. indexing of new documents. While mixing old and new data is safe, it could
  445. interfere with data retention. See <<data-stream-mix-new-old-data,Mixing new and
  446. old data in a data stream>>.
  447. . If you use {ilm-init} to <<getting-started-index-lifecycle-management,automate
  448. rollover>>, reduce the {ilm-init} poll interval. This ensures the current write
  449. index doesn’t grow too large while waiting for the rollover check. By default,
  450. {ilm-init} checks rollover conditions every 10 minutes.
  451. +
  452. --
  453. The following <<cluster-update-settings,update cluster settings API>> request
  454. lowers the `indices.lifecycle.poll_interval` setting to `1m` (one minute).
  455. [source,console]
  456. ----
  457. PUT /_cluster/settings
  458. {
  459. "transient": {
  460. "indices.lifecycle.poll_interval": "1m"
  461. }
  462. }
  463. ----
  464. --
  465. . Reindex your data to the new data stream using an `op_type` of `create`.
  466. +
  467. If you want to partition the data in the order in which it was originally
  468. indexed, you can run separate reindex requests. These reindex requests can use
  469. individual backing indices as the source. You can use the
  470. <<indices-get-data-stream,get data stream API>> to retrieve a list of backing
  471. indices.
  472. +
  473. --
  474. For example, you plan to reindex data from `my-data-stream` into
  475. `new-data-stream`. However, you want to submit a separate reindex request for
  476. each backing index in `my-data-stream`, starting with the oldest backing index.
  477. This preserves the order in which the data was originally indexed.
  478. The following get data stream API request retrieves information about
  479. `my-data-stream`, including a list of its backing indices.
  480. [source,console]
  481. ----
  482. GET /_data_stream/my-data-stream
  483. ----
  484. The response's `indices` property contains an array of the stream's current
  485. backing indices. The first item in the array contains information about the
  486. stream's oldest backing index.
  487. [source,console-result]
  488. ----
  489. {
  490. "data_streams": [
  491. {
  492. "name": "my-data-stream",
  493. "timestamp_field": {
  494. "name": "@timestamp"
  495. },
  496. "indices": [
  497. {
  498. "index_name": ".ds-my-data-stream-2099.03.07-000001", <1>
  499. "index_uuid": "Gpdiyq8sRuK9WuthvAdFbw"
  500. },
  501. {
  502. "index_name": ".ds-my-data-stream-2099.03.08-000002",
  503. "index_uuid": "_eEfRrFHS9OyhqWntkgHAQ"
  504. }
  505. ],
  506. "generation": 2,
  507. "status": "GREEN",
  508. "template": "my-data-stream-template",
  509. "hidden": false,
  510. "system": false
  511. }
  512. ]
  513. }
  514. ----
  515. // TESTRESPONSE[s/"index_uuid": "Gpdiyq8sRuK9WuthvAdFbw"/"index_uuid": $body.data_streams.0.indices.0.index_uuid/]
  516. // TESTRESPONSE[s/"index_uuid": "_eEfRrFHS9OyhqWntkgHAQ"/"index_uuid": $body.data_streams.0.indices.1.index_uuid/]
  517. // TESTRESPONSE[s/"index_name": ".ds-my-data-stream-2099.03.07-000001"/"index_name": $body.data_streams.0.indices.0.index_name/]
  518. // TESTRESPONSE[s/"index_name": ".ds-my-data-stream-2099.03.08-000002"/"index_name": $body.data_streams.0.indices.1.index_name/]
  519. // TESTRESPONSE[s/"status": "GREEN"/"status": "YELLOW"/]
  520. <1> First item in the `indices` array for `my-data-stream`. This item contains
  521. information about the stream's oldest backing index,
  522. `.ds-my-data-stream-2099.03.07-000001`.
  523. The following <<docs-reindex,reindex API>> request copies documents from
  524. `.ds-my-data-stream-2099.03.07-000001` to `new-data-stream`. The request's
  525. `op_type` is `create`.
  526. [source,console]
  527. ----
  528. POST /_reindex
  529. {
  530. "source": {
  531. "index": ".ds-my-data-stream-2099.03.07-000001"
  532. },
  533. "dest": {
  534. "index": "new-data-stream",
  535. "op_type": "create"
  536. }
  537. }
  538. ----
  539. // TEST[setup:my_index]
  540. // TEST[s/.ds-my-data-stream-2099.03.07-000001/my-index-000001/]
  541. --
  542. +
  543. You can also use a query to reindex only a subset of documents with each
  544. request.
  545. +
  546. --
  547. The following <<docs-reindex,reindex API>> request copies documents from
  548. `my-data-stream` to `new-data-stream`. The request
  549. uses a <<query-dsl-range-query,`range` query>> to only reindex documents with a
  550. timestamp within the last week. Note the request's `op_type` is `create`.
  551. [source,console]
  552. ----
  553. POST /_reindex
  554. {
  555. "source": {
  556. "index": "my-data-stream",
  557. "query": {
  558. "range": {
  559. "@timestamp": {
  560. "gte": "now-7d/d",
  561. "lte": "now/d"
  562. }
  563. }
  564. }
  565. },
  566. "dest": {
  567. "index": "new-data-stream",
  568. "op_type": "create"
  569. }
  570. }
  571. ----
  572. --
  573. . If you previously changed your {ilm-init} poll interval, change it back to its
  574. original value when reindexing is complete. This prevents unnecessary load on
  575. the master node.
  576. +
  577. --
  578. The following update cluster settings API request resets the
  579. `indices.lifecycle.poll_interval` setting to its default value, 10 minutes.
  580. [source,console]
  581. ----
  582. PUT /_cluster/settings
  583. {
  584. "transient": {
  585. "indices.lifecycle.poll_interval": null
  586. }
  587. }
  588. ----
  589. --
  590. . Resume indexing using the new data stream. Searches on this stream will now
  591. query your new data and the reindexed data.
  592. . Once you have verified that all reindexed data is available in the new
  593. data stream, you can safely remove the old stream.
  594. +
  595. --
  596. The following <<indices-delete-data-stream,delete data stream API>> request
  597. deletes `my-data-stream`. This request also deletes the stream's
  598. backing indices and any data they contain.
  599. [source,console]
  600. ----
  601. DELETE /_data_stream/my-data-stream
  602. ----
  603. --