put-mapping.asciidoc 5.2 KB

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