put-mapping.asciidoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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,js]
  9. ----
  10. PUT /twitter/_mapping
  11. {
  12. "properties": {
  13. "email": {
  14. "type": "keyword"
  15. }
  16. }
  17. }
  18. ----
  19. // CONSOLE
  20. // TEST[setup:twitter]
  21. NOTE: Before 7.0.0, the 'mappings' definition used to include a type name.
  22. Although specifying types in requests is now deprecated, a type can still be
  23. provided if the request parameter `include_type_name` is set. For more details,
  24. please see <<removal-of-types>>.
  25. [[put-mapping-api-request]]
  26. ==== {api-request-title}
  27. `PUT /<index>/_mapping`
  28. `PUT /_mapping`
  29. [[put-mapping-api-path-params]]
  30. ==== {api-path-parms-title}
  31. include::{docdir}/rest-api/common-parms.asciidoc[tag=index]
  32. +
  33. To update the mapping of all indices, omit this parameter or use a value of
  34. `_all`.
  35. [[put-mapping-api-query-params]]
  36. ==== {api-query-parms-title}
  37. include::{docdir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  38. include::{docdir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  39. +
  40. Defaults to `open`.
  41. include::{docdir}/rest-api/common-parms.asciidoc[tag=include-type-name]
  42. include::{docdir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  43. include::{docdir}/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 index setup
  60. The put mapping API requires an existing index. The following
  61. <<indices-create-index, create index>> API request creates the `publications`
  62. index with no mapping.
  63. [source,js]
  64. ----
  65. PUT /publications
  66. ----
  67. // CONSOLE
  68. The following put mapping API request adds `title`, a new <<text,`text`>> field,
  69. to the `publications` index.
  70. [source,js]
  71. ----
  72. PUT /publications/_mapping
  73. {
  74. "properties": {
  75. "title": { "type": "text"}
  76. }
  77. }
  78. ----
  79. // CONSOLE
  80. // TEST[continued]
  81. [[put-mapping-api-multi-ex]]
  82. ===== Multiple indices
  83. The PUT mapping API can be applied to multiple indices with a single request.
  84. For example, we can update the `twitter-1` and `twitter-2` mappings at the same time:
  85. [source,js]
  86. --------------------------------------------------
  87. # Create the two indices
  88. PUT /twitter-1
  89. PUT /twitter-2
  90. # Update both mappings
  91. PUT /twitter-1,twitter-2/_mapping <1>
  92. {
  93. "properties": {
  94. "user_name": {
  95. "type": "text"
  96. }
  97. }
  98. }
  99. --------------------------------------------------
  100. // CONSOLE
  101. // TEST[setup:twitter]
  102. <1> Note that the indices specified (`twitter-1,twitter-2`) follows <<multi-index,multiple index names>> and wildcard format.
  103. [[updating-field-mappings]]
  104. ===== Update an existing field
  105. // tag::put-field-mapping-exceptions[]
  106. You can't change the mapping of an existing field, with the following
  107. exceptions:
  108. * You can add new <<properties,properties>> to an <<object,`object`>> field.
  109. * You can use the <<multi-fields,`field`>> mapping parameter to enable
  110. multi-fields.
  111. * You can change the value of the <<ignore-above,`ignore_above`>> mapping
  112. parameter.
  113. Changing the mapping of an existing field could invalidate data that's already
  114. indexed. If you need to change the mapping of a field, create a new index with
  115. the correct mappings and <<docs-reindex,reindex>> your data into that index. If
  116. you only want to rename a field, consider adding an <<alias, `alias`>> field.
  117. // end::put-field-mapping-exceptions[]
  118. For example:
  119. [source,js]
  120. -----------------------------------
  121. PUT /my_index <1>
  122. {
  123. "mappings": {
  124. "properties": {
  125. "name": {
  126. "properties": {
  127. "first": {
  128. "type": "text"
  129. }
  130. }
  131. },
  132. "user_id": {
  133. "type": "keyword"
  134. }
  135. }
  136. }
  137. }
  138. PUT /my_index/_mapping
  139. {
  140. "properties": {
  141. "name": {
  142. "properties": {
  143. "last": { <2>
  144. "type": "text"
  145. }
  146. }
  147. },
  148. "user_id": {
  149. "type": "keyword",
  150. "ignore_above": 100 <3>
  151. }
  152. }
  153. }
  154. -----------------------------------
  155. // CONSOLE
  156. <1> Create an index with a `first` field under the `name` <<object>> field, and a `user_id` field.
  157. <2> Add a `last` field under the `name` object field.
  158. <3> Update the `ignore_above` setting from its default of 0.
  159. Each <<mapping-params,mapping parameter>> specifies whether or not its setting
  160. can be updated on an existing field.