put-mapping.asciidoc 11 KB

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