mapping.asciidoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. [[mapping]]
  2. = Mapping
  3. [partintro]
  4. --
  5. Mapping is the process of defining how a document, and the fields it contains,
  6. are stored and indexed. For instance, use mappings to define:
  7. * which string fields should be treated as full text fields.
  8. * which fields contain numbers, dates, or geolocations.
  9. * the <<mapping-date-format,format>> of date values.
  10. * custom rules to control the mapping for
  11. <<dynamic-mapping,dynamically added fields>>.
  12. A mapping definition includes metadata fields and fields:
  13. <<mapping-fields,Metadata fields>>::
  14. Metadata fields are used to customize how a document's associated metadata is
  15. treated. Examples of metadata fields include the document's
  16. <<mapping-index-field,`_index`>>, <<mapping-id-field,`_id`>>, and
  17. <<mapping-source-field,`_source`>> fields.
  18. <<mapping-types,Fields>>::
  19. A mapping contains a list of fields or `properties` pertinent to the
  20. document. Each field has its own <<mapping-types, data type>>.
  21. NOTE: Before 7.0.0, the 'mappings' definition used to include a type name.
  22. For more details, please see <<removal-of-types>>.
  23. [discrete]
  24. [[mapping-limit-settings]]
  25. == Settings to prevent mapping explosion
  26. Defining too many fields in an index can lead to a mapping explosion, which can
  27. cause out of memory errors and difficult situations to recover from.
  28. Consider a situation where every new document inserted
  29. introduces new fields, such as with <<dynamic-mapping,dynamic mapping>>.
  30. Each new field is added to the index mapping, which can become a
  31. problem as the mapping grows.
  32. Use the <<mapping-settings-limit,mapping limit settings>> to limit the number
  33. of field mappings (created manually or dynamically) and prevent documents from
  34. causing a mapping explosion.
  35. [discrete]
  36. [[runtime-fields]]
  37. == Runtime fields
  38. Typically, you index data into {es} to promote faster search. However, indexing
  39. can be slow and requires more disk space, and you have to reindex your data to
  40. add fields to existing documents.
  41. <<runtime,Runtime fields>> are not indexed, which saves disk space and makes
  42. data ingest faster. You can add runtime fields to existing documents without
  43. reindexing your data and calculate field values dynamically at search time.
  44. [discrete]
  45. [[dynamic-mapping-intro]]
  46. == Dynamic mapping
  47. Fields and mapping types do not need to be defined before being used. Thanks
  48. to _dynamic mapping_, new field names will be added automatically, just by
  49. indexing a document. New fields can be added both to the top-level mapping
  50. type, and to inner <<object,`object`>> and <<nested,`nested`>> fields.
  51. The <<dynamic-mapping,dynamic mapping>> rules can be configured to customise
  52. the mapping that is used for new fields.
  53. [discrete]
  54. == Explicit mappings
  55. You know more about your data than Elasticsearch can guess, so while dynamic
  56. mapping can be useful to get started, at some point you will want to specify
  57. your own explicit mappings.
  58. You can create field mappings when you <<create-mapping,create an index>> and
  59. <<add-field-mapping,add fields to an existing index>>.
  60. [discrete]
  61. [[create-mapping]]
  62. === Create an index with an explicit mapping
  63. You can use the <<indices-create-index,create index>> API to create a new index
  64. with an explicit mapping.
  65. [source,console]
  66. ----
  67. PUT /my-index-000001
  68. {
  69. "mappings": {
  70. "properties": {
  71. "age": { "type": "integer" }, <1>
  72. "email": { "type": "keyword" }, <2>
  73. "name": { "type": "text" } <3>
  74. }
  75. }
  76. }
  77. ----
  78. <1> Creates `age`, an <<number,`integer`>> field
  79. <2> Creates `email`, a <<keyword,`keyword`>> field
  80. <3> Creates `name`, a <<text,`text`>> field
  81. [discrete]
  82. [[add-field-mapping]]
  83. == Add a field to an existing mapping
  84. You can use the <<indices-put-mapping, put mapping>> API to add one or more new
  85. fields to an existing index.
  86. The following example adds `employee-id`, a `keyword` field with an
  87. <<mapping-index,`index`>> mapping parameter value of `false`. This means values
  88. for the `employee-id` field are stored but not indexed or available for search.
  89. [source,console]
  90. ----
  91. PUT /my-index-000001/_mapping
  92. {
  93. "properties": {
  94. "employee-id": {
  95. "type": "keyword",
  96. "index": false
  97. }
  98. }
  99. }
  100. ----
  101. // TEST[continued]
  102. [discrete]
  103. [[update-mapping]]
  104. === Update the mapping of a field
  105. include::{es-repo-dir}/indices/put-mapping.asciidoc[tag=change-field-mapping]
  106. include::{es-repo-dir}/indices/put-mapping.asciidoc[tag=rename-field]
  107. [discrete]
  108. [[view-mapping]]
  109. == View the mapping of an index
  110. You can use the <<indices-get-mapping, get mapping>> API to view the mapping of
  111. an existing index.
  112. [source,console]
  113. ----
  114. GET /my-index-000001/_mapping
  115. ----
  116. // TEST[continued]
  117. The API returns the following response:
  118. [source,console-result]
  119. ----
  120. {
  121. "my-index-000001" : {
  122. "mappings" : {
  123. "properties" : {
  124. "age" : {
  125. "type" : "integer"
  126. },
  127. "email" : {
  128. "type" : "keyword"
  129. },
  130. "employee-id" : {
  131. "type" : "keyword",
  132. "index" : false
  133. },
  134. "name" : {
  135. "type" : "text"
  136. }
  137. }
  138. }
  139. }
  140. }
  141. ----
  142. [discrete]
  143. [[view-field-mapping]]
  144. == View the mapping of specific fields
  145. If you only want to view the mapping of one or more specific fields, you can use
  146. the <<indices-get-field-mapping, get field mapping>> API.
  147. This is useful if you don't need the complete mapping of an index or your index
  148. contains a large number of fields.
  149. The following request retrieves the mapping for the `employee-id` field.
  150. [source,console]
  151. ----
  152. GET /my-index-000001/_mapping/field/employee-id
  153. ----
  154. // TEST[continued]
  155. The API returns the following response:
  156. [source,console-result]
  157. ----
  158. {
  159. "my-index-000001" : {
  160. "mappings" : {
  161. "employee-id" : {
  162. "full_name" : "employee-id",
  163. "mapping" : {
  164. "employee-id" : {
  165. "type" : "keyword",
  166. "index" : false
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. ----
  174. --
  175. include::mapping/removal_of_types.asciidoc[]
  176. include::mapping/mapping-settings-limit.asciidoc[]
  177. include::mapping/types.asciidoc[]
  178. include::mapping/runtime.asciidoc[]
  179. include::mapping/fields.asciidoc[]
  180. include::mapping/params.asciidoc[]
  181. include::mapping/dynamic-mapping.asciidoc[]