put-mapping.asciidoc 10 KB

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