change-mappings-and-settings.asciidoc 19 KB

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