put-mapping.asciidoc 8.9 KB

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