put-mapping.asciidoc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. PUT twitter/_mapping/_doc <2>
  10. {
  11. "properties": {
  12. "name": {
  13. "type": "text"
  14. }
  15. }
  16. }
  17. PUT twitter/_mapping/_doc <3>
  18. {
  19. "properties": {
  20. "email": {
  21. "type": "keyword"
  22. }
  23. }
  24. }
  25. --------------------------------------------------
  26. // CONSOLE
  27. <1> <<indices-create-index,Creates an index>> called `twitter` without any type mapping.
  28. <2> Uses the PUT mapping API to add a new mapping type called `user`.
  29. <3> Uses the PUT mapping API to add a new field called `email` to the `user` mapping type.
  30. More information on how to define type mappings can be found in the
  31. <<mapping,mapping>> section.
  32. [float]
  33. === Multi-index
  34. The PUT mapping API can be applied to multiple indices with a single request.
  35. For example, we can update the `twitter-1` and `twitter-2` mappings at the same time:
  36. [source,js]
  37. --------------------------------------------------
  38. # Create the two indices
  39. PUT twitter-1
  40. PUT twitter-2
  41. # Update both mappings
  42. PUT /twitter-1,twitter-2/_mapping/_doc <1>
  43. {
  44. "properties": {
  45. "user_name": {
  46. "type": "text"
  47. }
  48. }
  49. }
  50. --------------------------------------------------
  51. // CONSOLE
  52. <1> Note that the indices specified (`twitter-1,twitter-2`) follows <<multi-index,multiple index names>> and wildcard format.
  53. NOTE: When updating the `_default_` mapping with the
  54. <<indices-put-mapping,PUT mapping>> API, the new mapping is not merged with
  55. the existing mapping. Instead, the new `_default_` mapping replaces the
  56. existing one.
  57. [[updating-field-mappings]]
  58. [float]
  59. === Updating field mappings
  60. In general, the mapping for existing fields cannot be updated. There are some
  61. exceptions to this rule. For instance:
  62. * new <<properties>> can be added to <<object>> fields.
  63. * new <<multi-fields,multi-fields>> can be added to existing fields.
  64. * the <<ignore-above>> parameter can be updated.
  65. For example:
  66. [source,js]
  67. -----------------------------------
  68. PUT my_index <1>
  69. {
  70. "mappings": {
  71. "_doc": {
  72. "properties": {
  73. "name": {
  74. "properties": {
  75. "first": {
  76. "type": "text"
  77. }
  78. }
  79. },
  80. "user_id": {
  81. "type": "keyword"
  82. }
  83. }
  84. }
  85. }
  86. }
  87. PUT my_index/_mapping/_doc
  88. {
  89. "properties": {
  90. "name": {
  91. "properties": {
  92. "last": { <2>
  93. "type": "text"
  94. }
  95. }
  96. },
  97. "user_id": {
  98. "type": "keyword",
  99. "ignore_above": 100 <3>
  100. }
  101. }
  102. }
  103. -----------------------------------
  104. // CONSOLE
  105. <1> Create an index with a `first` field under the `name` <<object>> field, and a `user_id` field.
  106. <2> Add a `last` field under the `name` object field.
  107. <3> Update the `ignore_above` setting from its default of 0.
  108. Each <<mapping-params,mapping parameter>> specifies whether or not its setting
  109. can be updated on an existing field.