put-mapping.asciidoc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. [[indices-put-mapping]]
  2. === Update mapping API
  3. ++++
  4. <titleabbrev>Update mapping</titleabbrev>
  5. ++++
  6. Adds new fields to an existing data stream or index. You can also use this
  7. API to change the search settings of existing fields.
  8. For data streams, these changes are applied to all backing indices by default.
  9. [source,console]
  10. ----
  11. PUT /my-index-000001/_mapping
  12. {
  13. "properties": {
  14. "email": {
  15. "type": "keyword"
  16. }
  17. }
  18. }
  19. ----
  20. // TEST[setup:my_index]
  21. [[put-mapping-api-request]]
  22. ==== {api-request-title}
  23. `PUT /<target>/_mapping`
  24. [[put-mapping-api-prereqs]]
  25. ==== {api-prereq-title}
  26. * If the {es} {security-features} are enabled, you must have the `manage`
  27. <<privileges-list-indices,index privilege>> for the target data stream, index,
  28. or alias.
  29. +
  30. deprecated:[7.9] If the request targets an index or index alias, you can also
  31. update its mapping with the `create`, `create_doc`, `index`, or `write` index
  32. privilege.
  33. [[put-mapping-api-path-params]]
  34. ==== {api-path-parms-title}
  35. `<target>`::
  36. (Required, string) Comma-separated list of data streams, indices, and aliases
  37. used to limit the request. Supports wildcards (`*`). To target all data streams
  38. and indices, omit this parameter or use `*` or `_all`.
  39. [[put-mapping-api-query-params]]
  40. ==== {api-query-parms-title}
  41. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  42. +
  43. Defaults to `false`.
  44. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  45. +
  46. Defaults to `open`.
  47. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  48. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  49. `write_index_only`::
  50. (Optional, Boolean)
  51. If `true`,
  52. the mappings are applied only to the current write index for the target.
  53. Defaults to `false`.
  54. [[put-mapping-api-request-body]]
  55. ==== {api-request-body-title}
  56. `properties`::
  57. +
  58. --
  59. (Required, <<mapping,mapping object>>) Mapping for a field. For new
  60. fields, this mapping can include:
  61. * Field name
  62. * <<mapping-types,Field data type>>
  63. * <<mapping-params,Mapping parameters>>
  64. For existing fields, see <<updating-field-mappings>>.
  65. --
  66. [[put-mapping-api-example]]
  67. ==== {api-examples-title}
  68. [[put-field-mapping-api-basic-ex]]
  69. ===== Example with single target
  70. The update mapping API requires an existing data stream or index. The following
  71. <<indices-create-index, create index>> API request creates the `publications`
  72. index with no mapping.
  73. [source,console]
  74. ----
  75. PUT /publications
  76. ----
  77. The following update mapping API request adds `title`, a new <<text,`text`>> field,
  78. to the `publications` index.
  79. [source,console]
  80. ----
  81. PUT /publications/_mapping
  82. {
  83. "properties": {
  84. "title": { "type": "text"}
  85. }
  86. }
  87. ----
  88. // TEST[continued]
  89. [[put-mapping-api-multi-ex]]
  90. ===== Multiple targets
  91. The update mapping API can be applied to multiple data streams or indices with a single request.
  92. For example, you can update mappings for the `my-index-000001` and `my-index-000002` indices at the same time:
  93. [source,console]
  94. --------------------------------------------------
  95. # Create the two indices
  96. PUT /my-index-000001
  97. PUT /my-index-000002
  98. # Update both mappings
  99. PUT /my-index-000001,my-index-000002/_mapping
  100. {
  101. "properties": {
  102. "user": {
  103. "properties": {
  104. "name": {
  105. "type": "keyword"
  106. }
  107. }
  108. }
  109. }
  110. }
  111. --------------------------------------------------
  112. [[add-new-field-to-object]]
  113. ===== Add new properties to an existing object field
  114. You can use the update mapping API to add new properties to an existing
  115. <<object,`object`>> field. To see how this works, try the following example.
  116. Use the <<indices-create-index,create index>> API to create an index with the
  117. `name` object field and an inner `first` text field.
  118. [source,console]
  119. ----
  120. PUT /my-index-000001
  121. {
  122. "mappings": {
  123. "properties": {
  124. "name": {
  125. "properties": {
  126. "first": {
  127. "type": "text"
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. ----
  135. Use the update mapping API to add a new inner `last` text field to the `name`
  136. field.
  137. [source,console]
  138. ----
  139. PUT /my-index-000001/_mapping
  140. {
  141. "properties": {
  142. "name": {
  143. "properties": {
  144. "last": {
  145. "type": "text"
  146. }
  147. }
  148. }
  149. }
  150. }
  151. ----
  152. // TEST[continued]
  153. [[add-multi-fields-existing-field-ex]]
  154. ===== Add multi-fields to an existing field
  155. <<multi-fields,Multi-fields>> let you index the same field in different ways.
  156. You can use the update mapping API to update the `fields` mapping parameter and
  157. enable multi-fields for an existing field.
  158. To see how this works, try the following example.
  159. Use the <<indices-create-index,create index>> API to create an index with the
  160. `city` <<text,text>> field.
  161. [source,console]
  162. ----
  163. PUT /my-index-000001
  164. {
  165. "mappings": {
  166. "properties": {
  167. "city": {
  168. "type": "text"
  169. }
  170. }
  171. }
  172. }
  173. ----
  174. While text fields work well for full-text search, <<keyword,keyword>> fields are
  175. not analyzed and may work better for sorting or aggregations.
  176. Use the update mapping API to enable a multi-field for the `city` field. This
  177. request adds the `city.raw` keyword multi-field, which can be used for sorting.
  178. [source,console]
  179. ----
  180. PUT /my-index-000001/_mapping
  181. {
  182. "properties": {
  183. "city": {
  184. "type": "text",
  185. "fields": {
  186. "raw": {
  187. "type": "keyword"
  188. }
  189. }
  190. }
  191. }
  192. }
  193. ----
  194. // TEST[continued]
  195. [[change-existing-mapping-parms]]
  196. ===== Change supported mapping parameters for an existing field
  197. The documentation for each <<mapping-params,mapping parameter>> indicates
  198. whether you can update it for an existing field using the update mapping API. For
  199. example, you can use the update mapping API to update the
  200. <<ignore-above,`ignore_above`>> parameter.
  201. To see how this works, try the following example.
  202. Use the <<indices-create-index,create index>> API to create an index containing
  203. a `user_id` keyword field. The `user_id` field has an `ignore_above` parameter
  204. value of `20`.
  205. [source,console]
  206. ----
  207. PUT /my-index-000001
  208. {
  209. "mappings": {
  210. "properties": {
  211. "user_id": {
  212. "type": "keyword",
  213. "ignore_above": 20
  214. }
  215. }
  216. }
  217. }
  218. ----
  219. Use the update mapping API to change the `ignore_above` parameter value to `100`.
  220. [source,console]
  221. ----
  222. PUT /my-index-000001/_mapping
  223. {
  224. "properties": {
  225. "user_id": {
  226. "type": "keyword",
  227. "ignore_above": 100
  228. }
  229. }
  230. }
  231. ----
  232. // TEST[continued]
  233. [[updating-field-mappings]]
  234. ===== Change the mapping of an existing field
  235. // tag::change-field-mapping[]
  236. Except for supported <<mapping-params,mapping parameters>>,
  237. you can't change the mapping or field type of an existing field.
  238. Changing an existing field could invalidate data that's already indexed.
  239. If you need to change the mapping of a field in a data stream's backing indices,
  240. see <<data-streams-change-mappings-and-settings>>.
  241. If you need to change the mapping of a field in other indices,
  242. create a new index with the correct mapping
  243. and <<docs-reindex,reindex>> your data into that index.
  244. // end::change-field-mapping[]
  245. To see how you can change the mapping of an existing field in an index,
  246. try the following example.
  247. Use the <<indices-create-index,create index>> API
  248. to create an index
  249. with the `user_id` field
  250. with the <<number,`long`>> field type.
  251. [source,console]
  252. ----
  253. PUT /my-index-000001
  254. {
  255. "mappings" : {
  256. "properties": {
  257. "user_id": {
  258. "type": "long"
  259. }
  260. }
  261. }
  262. }
  263. ----
  264. Use the <<docs-index_,index>> API
  265. to index several documents
  266. with `user_id` field values.
  267. [source,console]
  268. ----
  269. POST /my-index-000001/_doc?refresh=wait_for
  270. {
  271. "user_id" : 12345
  272. }
  273. POST /my-index-000001/_doc?refresh=wait_for
  274. {
  275. "user_id" : 12346
  276. }
  277. ----
  278. // TEST[continued]
  279. To change the `user_id` field
  280. to the <<keyword,`keyword`>> field type,
  281. use the create index API
  282. to create a new index with the correct mapping.
  283. [source,console]
  284. ----
  285. PUT /my-new-index-000001
  286. {
  287. "mappings" : {
  288. "properties": {
  289. "user_id": {
  290. "type": "keyword"
  291. }
  292. }
  293. }
  294. }
  295. ----
  296. // TEST[continued]
  297. Use the <<docs-reindex,reindex>> API
  298. to copy documents from the old index
  299. to the new one.
  300. [source,console]
  301. ----
  302. POST /_reindex
  303. {
  304. "source": {
  305. "index": "my-index-000001"
  306. },
  307. "dest": {
  308. "index": "my-new-index-000001"
  309. }
  310. }
  311. ----
  312. // TEST[continued]
  313. [[rename-existing-field]]
  314. ===== Rename a field
  315. // tag::rename-field[]
  316. Renaming a field would invalidate data already indexed under the old field name.
  317. Instead, add an <<field-alias, `alias`>> field to create an alternate field name.
  318. // end::rename-field[]
  319. For example,
  320. use the <<indices-create-index,create index>> API
  321. to create an index
  322. with the `user_identifier` field.
  323. [source,console]
  324. ----
  325. PUT /my-index-000001
  326. {
  327. "mappings": {
  328. "properties": {
  329. "user_identifier": {
  330. "type": "keyword"
  331. }
  332. }
  333. }
  334. }
  335. ----
  336. Use the update mapping API to add the `user_id` field alias
  337. for the existing `user_identifier` field.
  338. [source,console]
  339. ----
  340. PUT /my-index-000001/_mapping
  341. {
  342. "properties": {
  343. "user_id": {
  344. "type": "alias",
  345. "path": "user_identifier"
  346. }
  347. }
  348. }
  349. ----
  350. // TEST[continued]