change-mappings-and-settings.asciidoc 19 KB

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