put-mapping.asciidoc 8.5 KB

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