put-mapping.asciidoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. [[updating-field-mappings]]
  100. ===== Update an existing field
  101. // tag::put-field-mapping-exceptions[]
  102. You can't change the mapping of an existing field, with the following
  103. exceptions:
  104. * You can add new <<properties,properties>> to an <<object,`object`>> field.
  105. * You can use the <<multi-fields,`field`>> mapping parameter to enable
  106. multi-fields.
  107. * You can change the value of the <<ignore-above,`ignore_above`>> mapping
  108. parameter.
  109. Changing the mapping of an existing field could invalidate data that's already
  110. indexed. If you need to change the mapping of a field, create a new index with
  111. the correct mappings and <<docs-reindex,reindex>> your data into that index. If
  112. you only want to rename a field, consider adding an <<alias, `alias`>> field.
  113. // end::put-field-mapping-exceptions[]
  114. For example:
  115. [source,console]
  116. -----------------------------------
  117. PUT /my_index <1>
  118. {
  119. "mappings": {
  120. "properties": {
  121. "name": {
  122. "properties": {
  123. "first": {
  124. "type": "text"
  125. }
  126. }
  127. },
  128. "user_id": {
  129. "type": "keyword"
  130. }
  131. }
  132. }
  133. }
  134. PUT /my_index/_mapping
  135. {
  136. "properties": {
  137. "name": {
  138. "properties": {
  139. "last": { <2>
  140. "type": "text"
  141. }
  142. }
  143. },
  144. "user_id": {
  145. "type": "keyword",
  146. "ignore_above": 100 <3>
  147. }
  148. }
  149. }
  150. -----------------------------------
  151. <1> Create an index with a `first` field under the `name` <<object>> field, and a `user_id` field.
  152. <2> Add a `last` field under the `name` object field.
  153. <3> Update the `ignore_above` setting from its default of 0.
  154. Each <<mapping-params,mapping parameter>> specifies whether or not its setting
  155. can be updated on an existing field.