change-mappings-and-settings.asciidoc 18 KB

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