change-mappings-and-settings.asciidoc 19 KB

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