mapping.asciidoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * whether the values of all fields in the document should be
  10. indexed into the catch-all <<mapping-all-field,`_all`>> field.
  11. * the <<mapping-date-format,format>> of date values.
  12. * custom rules to control the mapping for
  13. <<dynamic-mapping,dynamically added fields>>.
  14. [float]
  15. [[mapping-type]]
  16. == Mapping Type
  17. Each index has one _mapping type_ which determines how the document will be
  18. indexed.
  19. deprecated[6.0.0,See <<removal-of-types>>].
  20. A mapping type has:
  21. <<mapping-fields,Meta-fields>>::
  22. Meta-fields are used to customize how a document's metadata associated is
  23. treated. Examples of meta-fields include the document's
  24. <<mapping-index-field,`_index`>>, <<mapping-type-field,`_type`>>,
  25. <<mapping-id-field,`_id`>>, and <<mapping-source-field,`_source`>> fields.
  26. <<mapping-types,Fields>> or _properties_::
  27. A mapping type contains a list of fields or `properties` pertinent to the
  28. document.
  29. [float]
  30. == Field datatypes
  31. Each field has a data `type` which can be:
  32. * a simple type like <<text,`text`>>, <<keyword,`keyword`>>, <<date,`date`>>, <<number,`long`>>,
  33. <<number,`double`>>, <<boolean,`boolean`>> or <<ip,`ip`>>.
  34. * a type which supports the hierarchical nature of JSON such as
  35. <<object,`object`>> or <<nested,`nested`>>.
  36. * or a specialised type like <<geo-point,`geo_point`>>,
  37. <<geo-shape,`geo_shape`>>, or <<search-suggesters-completion,`completion`>>.
  38. It is often useful to index the same field in different ways for different
  39. purposes. For instance, a `string` field could be <<mapping-index,indexed>> as
  40. a `text` field for full-text search, and as a `keyword` field for
  41. sorting or aggregations. Alternatively, you could index a string field with
  42. the <<analysis-standard-analyzer,`standard` analyzer>>, the
  43. <<english-analyzer,`english`>> analyzer, and the
  44. <<french-analyzer,`french` analyzer>>.
  45. This is the purpose of _multi-fields_. Most datatypes support multi-fields
  46. via the <<multi-fields>> parameter.
  47. [[mapping-limit-settings]]
  48. [float]
  49. === Settings to prevent mappings explosion
  50. Defining too many fields in an index is a condition that can lead to a
  51. mapping explosion, which can cause out of memory errors and difficult
  52. situations to recover from. This problem may be more common than expected.
  53. As an example, consider a situation in which every new document inserted
  54. introduces new fields. This is quite common with dynamic mappings.
  55. Every time a document contains new fields, those will end up in the index's
  56. mappings. This isn't worrying for a small amount of data, but it can become a
  57. problem as the mapping grows.
  58. The following settings allow you to limit the number of field mappings that
  59. can be created manually or dynamically, in order to prevent bad documents from
  60. causing a mapping explosion:
  61. `index.mapping.total_fields.limit`::
  62. The maximum number of fields in an index. The default value is `1000`.
  63. `index.mapping.depth.limit`::
  64. The maximum depth for a field, which is measured as the number of inner
  65. objects. For instance, if all fields are defined at the root object level,
  66. then the depth is `1`. If there is one object mapping, then the depth is
  67. `2`, etc. The default is `20`.
  68. `index.mapping.nested_fields.limit`::
  69. The maximum number of `nested` fields in an index, defaults to `50`.
  70. Indexing 1 document with 100 nested fields actually indexes 101 documents
  71. as each nested document is indexed as a separate hidden document.
  72. [float]
  73. == Dynamic mapping
  74. Fields and mapping types do not need to be defined before being used. Thanks
  75. to _dynamic mapping_, new field names will be added automatically, just by
  76. indexing a document. New fields can be added both to the top-level mapping
  77. type, and to inner <<object,`object`>> and <<nested,`nested`>> fields.
  78. The <<dynamic-mapping,dynamic mapping>> rules can be configured to customise
  79. the mapping that is used for new fields.
  80. [float]
  81. == Explicit mappings
  82. You know more about your data than Elasticsearch can guess, so while dynamic
  83. mapping can be useful to get started, at some point you will want to specify
  84. your own explicit mappings.
  85. You can create field mappings when you
  86. <<indices-create-index,create an index>>, and you can add
  87. fields to an existing index with the <<indices-put-mapping,PUT mapping API>>.
  88. [float]
  89. == Updating existing field mappings
  90. Other than where documented, *existing field mappings cannot be
  91. updated*. Changing the mapping would mean invalidating already indexed
  92. documents. Instead, you should create a new index with the correct mappings
  93. and <<docs-reindex,reindex>> your data into that index.
  94. [float]
  95. == Example mapping
  96. A mapping could be specified when creating an index, as follows:
  97. [source,js]
  98. ---------------------------------------
  99. PUT my_index <1>
  100. {
  101. "mappings": {
  102. "doc": { <2>
  103. "properties": { <3>
  104. "title": { "type": "text" }, <4>
  105. "name": { "type": "text" }, <4>
  106. "age": { "type": "integer" }, <4>
  107. "created": {
  108. "type": "date", <4>
  109. "format": "strict_date_optional_time||epoch_millis"
  110. }
  111. }
  112. }
  113. }
  114. }
  115. ---------------------------------------
  116. // CONSOLE
  117. <1> Create an index called `my_index`.
  118. <2> Add a mapping type called `doc`.
  119. <3> Specify fields or _properties_.
  120. <4> Specify the data `type` and mapping for each field.
  121. --
  122. include::mapping/removal_of_types.asciidoc[]
  123. include::mapping/types.asciidoc[]
  124. include::mapping/fields.asciidoc[]
  125. include::mapping/params.asciidoc[]
  126. include::mapping/dynamic-mapping.asciidoc[]