change-mappings-and-settings.asciidoc 18 KB

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