put-mapping.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. [[indices-put-mapping]]
  2. == Put Mapping
  3. The PUT mapping API allows you to add a new type to an existing index, or add new
  4. fields to an existing type:
  5. [source,js]
  6. --------------------------------------------------
  7. PUT twitter <1>
  8. {
  9. "mappings": {
  10. "tweet": {
  11. "properties": {
  12. "message": {
  13. "type": "text"
  14. }
  15. }
  16. }
  17. }
  18. }
  19. PUT twitter/_mapping/user <2>
  20. {
  21. "properties": {
  22. "name": {
  23. "type": "text"
  24. }
  25. }
  26. }
  27. PUT twitter/_mapping/tweet <3>
  28. {
  29. "properties": {
  30. "user_name": {
  31. "type": "text"
  32. }
  33. }
  34. }
  35. --------------------------------------------------
  36. // CONSOLE
  37. <1> <<indices-create-index,Creates an index>> called `twitter` with the `message` field in the `tweet` <<mapping-type,mapping type>>.
  38. <2> Uses the PUT mapping API to add a new mapping type called `user`.
  39. <3> Uses the PUT mapping API to add a new field called `user_name` to the `tweet` mapping type.
  40. More information on how to define type mappings can be found in the
  41. <<mapping,mapping>> section.
  42. [float]
  43. === Multi-index
  44. The PUT mapping API can be applied to multiple indices with a single request.
  45. It has the following format:
  46. [source,js]
  47. --------------------------------------------------
  48. PUT /{index}/_mapping/{type}
  49. { body }
  50. --------------------------------------------------
  51. * `{index}` accepts <<multi-index,multiple index names>> and wildcards.
  52. * `{type}` is the name of the type to update.
  53. * `{body}` contains the mapping changes that should be applied.
  54. NOTE: When updating the `_default_` mapping with the
  55. <<indices-put-mapping,PUT mapping>> API, the new mapping is not merged with
  56. the existing mapping. Instead, the new `_default_` mapping replaces the
  57. existing one.
  58. [[updating-field-mappings]]
  59. [float]
  60. === Updating field mappings
  61. In general, the mapping for existing fields cannot be updated. There are some
  62. exceptions to this rule. For instance:
  63. * new <<properties>> can be added to <<object>> fields.
  64. * new <<multi-fields,multi-fields>> can be added to existing fields.
  65. * <<doc-values>> can be disabled, but not enabled.
  66. * the <<ignore-above>> parameter can be updated.
  67. For example:
  68. [source,js]
  69. -----------------------------------
  70. PUT my_index <1>
  71. {
  72. "mappings": {
  73. "user": {
  74. "properties": {
  75. "name": {
  76. "properties": {
  77. "first": {
  78. "type": "text"
  79. }
  80. }
  81. },
  82. "user_id": {
  83. "type": "keyword"
  84. }
  85. }
  86. }
  87. }
  88. }
  89. PUT my_index/_mapping/user
  90. {
  91. "properties": {
  92. "name": {
  93. "properties": {
  94. "last": { <2>
  95. "type": "text"
  96. }
  97. }
  98. },
  99. "user_id": {
  100. "type": "keyword",
  101. "ignore_above": 100 <3>
  102. }
  103. }
  104. }
  105. -----------------------------------
  106. // CONSOLE
  107. <1> Create an index with a `first` field under the `name` <<object>> field, and a `user_id` field.
  108. <2> Add a `last` field under the `name` object field.
  109. <3> Update the `ignore_above` setting from its default of 0.
  110. Each <<mapping-params,mapping parameter>> specifies whether or not its setting
  111. can be updated on an existing field.
  112. [float]
  113. [[merging-conflicts]]
  114. === Conflicts between fields in different types
  115. Fields in the same index with the same name in two different types must have
  116. the same mapping, as they are backed by the same field internally. Trying to
  117. <<updating-field-mappings,update a mapping parameter>> for a field which
  118. exists in more than one type will throw an exception, unless you specify the
  119. `update_all_types` parameter, in which case it will update that parameter
  120. across all fields with the same name in the same index.
  121. TIP: The only parameters which are exempt from this rule -- they can be set to
  122. different values on each field -- can be found in <<field-conflicts>>.
  123. For example, this fails:
  124. [source,js]
  125. -----------------------------------
  126. PUT my_index
  127. {
  128. "mappings": {
  129. "type_one": {
  130. "properties": {
  131. "text": { <1>
  132. "type": "text",
  133. "analyzer": "standard"
  134. }
  135. }
  136. },
  137. "type_two": {
  138. "properties": {
  139. "text": { <1>
  140. "type": "text",
  141. "analyzer": "standard"
  142. }
  143. }
  144. }
  145. }
  146. }
  147. PUT my_index/_mapping/type_one <2>
  148. {
  149. "properties": {
  150. "text": {
  151. "type": "text",
  152. "analyzer": "standard",
  153. "search_analyzer": "whitespace"
  154. }
  155. }
  156. }
  157. -----------------------------------
  158. // CONSOLE
  159. // TEST[catch:request]
  160. <1> Create an index with two types, both of which contain a `text` field which have the same mapping.
  161. <2> Trying to update the `search_analyzer` just for `type_one` throws an exception like `"Merge failed with failures..."`.
  162. But this then running this succeeds:
  163. [source,js]
  164. -----------------------------------
  165. PUT my_index/_mapping/type_one?update_all_types <1>
  166. {
  167. "properties": {
  168. "text": {
  169. "type": "text",
  170. "analyzer": "standard",
  171. "search_analyzer": "whitespace"
  172. }
  173. }
  174. }
  175. -----------------------------------
  176. // CONSOLE
  177. // TEST[continued]
  178. <1> Adding the `update_all_types` parameter updates the `text` field in `type_one` and `type_two`.