change-mappings-and-settings.asciidoc 21 KB

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