put-mapping.asciidoc 10 KB

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