put-mapping.asciidoc 8.9 KB

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