put-mapping.asciidoc 10 KB

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