put-mapping.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 /twitter/_mapping
  12. {
  13. "properties": {
  14. "email": {
  15. "type": "keyword"
  16. }
  17. }
  18. }
  19. ----
  20. // TEST[setup:twitter]
  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. * <<field-datatypes,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 `twitter-1` and `twitter-2` indices at the same time:
  88. [source,console]
  89. --------------------------------------------------
  90. # Create the two indices
  91. PUT /twitter-1
  92. PUT /twitter-2
  93. # Update both mappings
  94. PUT /twitter-1,twitter-2/_mapping <1>
  95. {
  96. "properties": {
  97. "user_name": {
  98. "type": "text"
  99. }
  100. }
  101. }
  102. --------------------------------------------------
  103. // TEST[setup:twitter]
  104. <1> Note that the indices specified (`twitter-1,twitter-2`) follows <<multi-index,multiple index names>> and wildcard format.
  105. [[add-new-field-to-object]]
  106. ===== Add new properties to an existing object field
  107. You can use the put mapping API
  108. to add new properties
  109. to an existing <<object,`object`>> field.
  110. To see how this works,
  111. try the following example.
  112. Use the <<indices-create-index,create index>> API
  113. to create an index
  114. with the `name` object field
  115. and an inner `first` text field.
  116. [source,console]
  117. ----
  118. PUT /my_index
  119. {
  120. "mappings": {
  121. "properties": {
  122. "name": {
  123. "properties": {
  124. "first": {
  125. "type": "text"
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. ----
  133. Use the put mapping API
  134. to add a new inner `last` text field
  135. to the `name` field.
  136. [source,console]
  137. ----
  138. PUT /my_index/_mapping
  139. {
  140. "properties": {
  141. "name": {
  142. "properties": {
  143. "last": {
  144. "type": "text"
  145. }
  146. }
  147. }
  148. }
  149. }
  150. ----
  151. // TEST[continued]
  152. Use the <<indices-get-mapping,get mapping>> API
  153. to verify your changes.
  154. [source,console]
  155. ----
  156. GET /my_index/_mapping
  157. ----
  158. // TEST[continued]
  159. The API returns the following response:
  160. [source,console-result]
  161. ----
  162. {
  163. "my_index" : {
  164. "mappings" : {
  165. "properties" : {
  166. "name" : {
  167. "properties" : {
  168. "first" : {
  169. "type" : "text"
  170. },
  171. "last" : {
  172. "type" : "text"
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. ----
  181. [[add-multi-fields-existing-field-ex]]
  182. ===== Add multi-fields to an existing field
  183. <<multi-fields,Multi-fields>>
  184. let you index the same field
  185. in different ways.
  186. You can use the put mapping API
  187. to update the `fields` mapping parameter
  188. and enable multi-fields for an existing field.
  189. To see how this works,
  190. try the following example.
  191. Use the <<indices-create-index,create index>> API
  192. to create an index
  193. with the `city` <<text,text>> field.
  194. [source,console]
  195. ----
  196. PUT /my_index
  197. {
  198. "mappings": {
  199. "properties": {
  200. "city": {
  201. "type": "text"
  202. }
  203. }
  204. }
  205. }
  206. ----
  207. While text fields work well for full-text search,
  208. <<keyword,keyword>> fields are not analyzed
  209. and may work better for sorting or aggregations.
  210. Use the put mapping API
  211. to enable a multi-field for the `city` field.
  212. This request adds the `city.raw` keyword multi-field,
  213. which can be used for sorting.
  214. [source,console]
  215. ----
  216. PUT /my_index/_mapping
  217. {
  218. "properties": {
  219. "city": {
  220. "type": "text",
  221. "fields": {
  222. "raw": {
  223. "type": "keyword"
  224. }
  225. }
  226. }
  227. }
  228. }
  229. ----
  230. // TEST[continued]
  231. Use the <<indices-get-mapping,get mapping>> API
  232. to verify your changes.
  233. [source,console]
  234. ----
  235. GET /my_index/_mapping
  236. ----
  237. // TEST[continued]
  238. The API returns the following response:
  239. [source,console-result]
  240. ----
  241. {
  242. "my_index" : {
  243. "mappings" : {
  244. "properties" : {
  245. "city" : {
  246. "type" : "text",
  247. "fields" : {
  248. "raw" : {
  249. "type" : "keyword"
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. ----
  258. [[change-existing-mapping-parms]]
  259. ===== Change supported mapping parameters for an existing field
  260. The documentation for each <<mapping-params,mapping parameter>>
  261. indicates whether you can update it
  262. for an existing field
  263. using the put mapping API.
  264. For example,
  265. you can use the put mapping API
  266. to update the <<ignore-above,`ignore_above`>> parameter.
  267. To see how this works,
  268. try the following example.
  269. Use the <<indices-create-index,create index>> API to create an index
  270. containing a `user_id` keyword field.
  271. The `user_id` field
  272. has an `ignore_above` parameter value
  273. of `20`.
  274. [source,console]
  275. ----
  276. PUT /my_index
  277. {
  278. "mappings": {
  279. "properties": {
  280. "user_id": {
  281. "type": "keyword",
  282. "ignore_above": 20
  283. }
  284. }
  285. }
  286. }
  287. ----
  288. Use the put mapping API
  289. to change the `ignore_above` parameter value
  290. to `100`.
  291. [source,console]
  292. ----
  293. PUT /my_index/_mapping
  294. {
  295. "properties": {
  296. "user_id": {
  297. "type": "keyword",
  298. "ignore_above": 100
  299. }
  300. }
  301. }
  302. ----
  303. // TEST[continued]
  304. Use the <<indices-get-mapping,get mapping>> API
  305. to verify your changes.
  306. [source,console]
  307. ----
  308. GET /my_index/_mapping
  309. ----
  310. // TEST[continued]
  311. The API returns the following response:
  312. [source,console-result]
  313. ----
  314. {
  315. "my_index" : {
  316. "mappings" : {
  317. "properties" : {
  318. "user_id" : {
  319. "type" : "keyword",
  320. "ignore_above" : 100
  321. }
  322. }
  323. }
  324. }
  325. }
  326. ----
  327. [[updating-field-mappings]]
  328. ===== Change the mapping of an existing field
  329. // tag::change-field-mapping[]
  330. Except for supported <<mapping-params,mapping parameters>>,
  331. you can't change the mapping or field type of an existing field.
  332. Changing an existing field could invalidate data that's already indexed.
  333. If you need to change the mapping of a field in a data stream's backing indices,
  334. see <<data-streams-change-mappings-and-settings>>.
  335. If you need to change the mapping of a field in other indices,
  336. create a new index with the correct mapping
  337. and <<docs-reindex,reindex>> your data into that index.
  338. // end::change-field-mapping[]
  339. To see how you can change the mapping of an existing field in an index,
  340. try the following example.
  341. Use the <<indices-create-index,create index>> API
  342. to create the `users` index
  343. with the `user_id` field
  344. with the <<number,`long`>> field type.
  345. [source,console]
  346. ----
  347. PUT /users
  348. {
  349. "mappings" : {
  350. "properties": {
  351. "user_id": {
  352. "type": "long"
  353. }
  354. }
  355. }
  356. }
  357. ----
  358. Use the <<docs-index_,index>> API
  359. to index several documents
  360. with `user_id` field values.
  361. [source,console]
  362. ----
  363. POST /users/_doc?refresh=wait_for
  364. {
  365. "user_id" : 12345
  366. }
  367. POST /users/_doc?refresh=wait_for
  368. {
  369. "user_id" : 12346
  370. }
  371. ----
  372. // TEST[continued]
  373. To change the `user_id` field
  374. to the <<keyword,`keyword`>> field type,
  375. use the create index API
  376. to create the `new_users` index with the correct mapping.
  377. [source,console]
  378. ----
  379. PUT /new_users
  380. {
  381. "mappings" : {
  382. "properties": {
  383. "user_id": {
  384. "type": "keyword"
  385. }
  386. }
  387. }
  388. }
  389. ----
  390. // TEST[continued]
  391. Use the <<docs-reindex,reindex>> API
  392. to copy documents from the `users` index
  393. to the `new_users` index.
  394. [source,console]
  395. ----
  396. POST /_reindex
  397. {
  398. "source": {
  399. "index": "users"
  400. },
  401. "dest": {
  402. "index": "new_users"
  403. }
  404. }
  405. ----
  406. // TEST[continued]
  407. The API returns the following response:
  408. [source,console-result]
  409. ----
  410. {
  411. "took": 147,
  412. "timed_out": false,
  413. "total": 2,
  414. "updated": 0,
  415. "created": 2,
  416. "deleted": 0,
  417. "batches": 1,
  418. "version_conflicts": 0,
  419. "noops": 0,
  420. "retries": {
  421. "bulk": 0,
  422. "search": 0
  423. },
  424. "throttled_millis": 0,
  425. "requests_per_second": -1.0,
  426. "throttled_until_millis": 0,
  427. "failures" : [ ]
  428. }
  429. ----
  430. // TESTRESPONSE[s/"took": 147/"took": "$body.took"/]
  431. [[rename-existing-field]]
  432. ===== Rename a field
  433. // tag::rename-field[]
  434. Renaming a field would invalidate data already indexed under the old field name.
  435. Instead, add an <<alias, `alias`>> field to create an alternate field name.
  436. // end::rename-field[]
  437. For example,
  438. use the <<indices-create-index,create index>> API
  439. to create an index
  440. with the `user_identifier` field.
  441. [source,console]
  442. ----
  443. PUT /my_index
  444. {
  445. "mappings": {
  446. "properties": {
  447. "user_identifier": {
  448. "type": "keyword"
  449. }
  450. }
  451. }
  452. }
  453. ----
  454. Use the put mapping API to add the `user_id` field alias
  455. for the existing `user_identifier` field.
  456. [source,console]
  457. ----
  458. PUT /my_index/_mapping
  459. {
  460. "properties": {
  461. "user_id": {
  462. "type": "alias",
  463. "path": "user_identifier"
  464. }
  465. }
  466. }
  467. ----
  468. // TEST[continued]
  469. Use the <<indices-get-mapping,get mapping>> API
  470. to verify your changes.
  471. [source,console]
  472. ----
  473. GET /my_index/_mapping
  474. ----
  475. // TEST[continued]
  476. The API returns the following response:
  477. [source,console-result]
  478. ----
  479. {
  480. "my_index" : {
  481. "mappings" : {
  482. "properties" : {
  483. "user_id" : {
  484. "type" : "alias",
  485. "path" : "user_identifier"
  486. },
  487. "user_identifier" : {
  488. "type" : "keyword"
  489. }
  490. }
  491. }
  492. }
  493. }
  494. ----