change-mappings-and-settings.asciidoc 19 KB

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