put-mapping.asciidoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. [[indices-put-mapping]]
  2. == Put Mapping
  3. The PUT mapping API allows you to add fields to an existing index or to change search only settings of existing fields.
  4. [source,js]
  5. --------------------------------------------------
  6. PUT twitter <1>
  7. {}
  8. PUT twitter/_mapping/_doc <2>
  9. {
  10. "properties": {
  11. "email": {
  12. "type": "keyword"
  13. }
  14. }
  15. }
  16. --------------------------------------------------
  17. // CONSOLE
  18. <1> <<indices-create-index,Creates an index>> called `twitter` without any type mapping.
  19. <2> Uses the PUT mapping API to add a new field called `email` to the `_doc` mapping type.
  20. More information on how to define type mappings can be found in the
  21. <<mapping,mapping>> section.
  22. [float]
  23. === Multi-index
  24. The PUT mapping API can be applied to multiple indices with a single request.
  25. For example, we can update the `twitter-1` and `twitter-2` mappings at the same time:
  26. [source,js]
  27. --------------------------------------------------
  28. # Create the two indices
  29. PUT twitter-1
  30. PUT twitter-2
  31. # Update both mappings
  32. PUT /twitter-1,twitter-2/_mapping/_doc <1>
  33. {
  34. "properties": {
  35. "user_name": {
  36. "type": "text"
  37. }
  38. }
  39. }
  40. --------------------------------------------------
  41. // CONSOLE
  42. <1> Note that the indices specified (`twitter-1,twitter-2`) follows <<multi-index,multiple index names>> and wildcard format.
  43. [[updating-field-mappings]]
  44. [float]
  45. === Updating field mappings
  46. In general, the mapping for existing fields cannot be updated. There are some
  47. exceptions to this rule. For instance:
  48. * new <<properties>> can be added to <<object>> fields.
  49. * new <<multi-fields,multi-fields>> can be added to existing fields.
  50. * the <<ignore-above>> parameter can be updated.
  51. For example:
  52. [source,js]
  53. -----------------------------------
  54. PUT my_index <1>
  55. {
  56. "mappings": {
  57. "_doc": {
  58. "properties": {
  59. "name": {
  60. "properties": {
  61. "first": {
  62. "type": "text"
  63. }
  64. }
  65. },
  66. "user_id": {
  67. "type": "keyword"
  68. }
  69. }
  70. }
  71. }
  72. }
  73. PUT my_index/_mapping/_doc
  74. {
  75. "properties": {
  76. "name": {
  77. "properties": {
  78. "last": { <2>
  79. "type": "text"
  80. }
  81. }
  82. },
  83. "user_id": {
  84. "type": "keyword",
  85. "ignore_above": 100 <3>
  86. }
  87. }
  88. }
  89. -----------------------------------
  90. // CONSOLE
  91. <1> Create an index with a `first` field under the `name` <<object>> field, and a `user_id` field.
  92. <2> Add a `last` field under the `name` object field.
  93. <3> Update the `ignore_above` setting from its default of 0.
  94. Each <<mapping-params,mapping parameter>> specifies whether or not its setting
  95. can be updated on an existing field.
  96. [float]
  97. === Skipping types
  98. Types are scheduled to be fully removed in Elasticsearch 8.0 and will not appear
  99. in requests or responses anymore. You can opt in for this future behaviour by
  100. setting `include_type_name=false`.
  101. NOTE: This should only be done on indices that have been created with
  102. `include_type_name=false` or that used `_doc` as a type name.
  103. The Console script from the above section is equivalent to the below invocation:
  104. [source,js]
  105. -----------------------------------
  106. PUT my_index?include_type_name=false <1>
  107. {
  108. "mappings": {
  109. "properties": {
  110. "name": {
  111. "properties": {
  112. "first": {
  113. "type": "text"
  114. }
  115. }
  116. },
  117. "user_id": {
  118. "type": "keyword"
  119. }
  120. }
  121. }
  122. }
  123. PUT my_index/_mapping?include_type_name=false
  124. {
  125. "properties": {
  126. "name": {
  127. "properties": {
  128. "last": { <2>
  129. "type": "text"
  130. }
  131. }
  132. },
  133. "user_id": {
  134. "type": "keyword",
  135. "ignore_above": 100 <3>
  136. }
  137. }
  138. }
  139. -----------------------------------
  140. // CONSOLE