change-mappings-and-settings.asciidoc 19 KB

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