mapping.asciidoc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 Types
  17. Each index has one or more _mapping types_, which are used to divide the
  18. documents in an index into logical groups. User documents might be stored in a
  19. `user` type, and blog posts in a `blogpost` type.
  20. Each 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. Each mapping type contains a list of fields or `properties` pertinent to that
  28. type. A `user` type might contain `title`, `name`, and `age` fields, while a
  29. `blogpost` type might contain `title`, `body`, `user_id` and `created` fields.
  30. Fields with the same name in different mapping types in the same index
  31. <<field-conflicts,must have the same mapping>>.
  32. [float]
  33. == Field datatypes
  34. Each field has a data `type` which can be:
  35. * a simple type like <<text,`text`>>, <<keyword,`keyword`>>, <<date,`date`>>, <<number,`long`>>,
  36. <<number,`double`>>, <<boolean,`boolean`>> or <<ip,`ip`>>.
  37. * a type which supports the hierarchical nature of JSON such as
  38. <<object,`object`>> or <<nested,`nested`>>.
  39. * or a specialised type like <<geo-point,`geo_point`>>,
  40. <<geo-shape,`geo_shape`>>, or <<search-suggesters-completion,`completion`>>.
  41. It is often useful to index the same field in different ways for different
  42. purposes. For instance, a `string` field could be <<mapping-index,indexed>> as
  43. a `text` field for full-text search, and as a `keyword` field for
  44. sorting or aggregations. Alternatively, you could index a string field with
  45. the <<analysis-standard-analyzer,`standard` analyzer>>, the
  46. <<english-analyzer,`english`>> analyzer, and the
  47. <<french-analyzer,`french` analyzer>>.
  48. This is the purpose of _multi-fields_. Most datatypes support multi-fields
  49. via the <<multi-fields>> parameter.
  50. [float]
  51. == Dynamic mapping
  52. Fields and mapping types do not need to be defined before being used. Thanks
  53. to _dynamic mapping_, new mapping types and new field names will be added
  54. automatically, just by indexing a document. New fields can be added both to
  55. the top-level mapping type, and to inner <<object,`object`>> and
  56. <<nested,`nested`>> fields.
  57. The
  58. <<dynamic-mapping,dynamic mapping>> rules can be configured to
  59. customise the mapping that is used for new types and new fields.
  60. [float]
  61. == Explicit mappings
  62. You know more about your data than Elasticsearch can guess, so while dynamic
  63. mapping can be useful to get started, at some point you will want to specify
  64. your own explicit mappings.
  65. You can create mapping types and field mappings when you
  66. <<indices-create-index,create an index>>, and you can add mapping types and
  67. fields to an existing index with the <<indices-put-mapping,PUT mapping API>>.
  68. [float]
  69. == Updating existing mappings
  70. Other than where documented, *existing type and field mappings cannot be
  71. updated*. Changing the mapping would mean invalidating already indexed
  72. documents. Instead, you should create a new index with the correct mappings
  73. and reindex your data into that index.
  74. [[field-conflicts]]
  75. [float]
  76. == Fields are shared across mapping types
  77. Mapping types are used to group fields, but the fields in each mapping type
  78. are not independent of each other. Fields with:
  79. * the _same name_
  80. * in the _same index_
  81. * in _different mapping types_
  82. * map to the _same field_ internally,
  83. * and *must have the same mapping*.
  84. If a `title` field exists in both the `user` and `blogpost` mapping types, the
  85. `title` fields must have exactly the same mapping in each type. The only
  86. exceptions to this rule are the <<copy-to>>, <<dynamic>>, <<enabled>>,
  87. <<ignore-above>>, <<include-in-all>>, and <<properties>> parameters, which may
  88. have different settings per field.
  89. Usually, fields with the same name also contain the same type of data, so
  90. having the same mapping is not a problem. When conflicts do arise, these can
  91. be solved by choosing more descriptive names, such as `user_title` and
  92. `blog_title`.
  93. [float]
  94. == Example mapping
  95. A mapping for the example described above could be specified when creating the
  96. index, as follows:
  97. [source,js]
  98. ---------------------------------------
  99. PUT my_index <1>
  100. {
  101. "mappings": {
  102. "user": { <2>
  103. "_all": { "enabled": false }, <3>
  104. "properties": { <4>
  105. "title": { "type": "text" }, <5>
  106. "name": { "type": "text" }, <5>
  107. "age": { "type": "integer" } <5>
  108. }
  109. },
  110. "blogpost": { <2>
  111. "_all": { "enabled": false }, <3>
  112. "properties": { <4>
  113. "title": { "type": "text" }, <5>
  114. "body": { "type": "text" }, <5>
  115. "user_id": {
  116. "type": "keyword" <5>
  117. },
  118. "created": {
  119. "type": "date", <5>
  120. "format": "strict_date_optional_time||epoch_millis"
  121. }
  122. }
  123. }
  124. }
  125. }
  126. ---------------------------------------
  127. // CONSOLE
  128. <1> Create an index called `my_index`.
  129. <2> Add mapping types called `user` and `blogpost`.
  130. <3> Disable the `_all` <<mapping-fields,meta field>> for the `user` mapping type.
  131. <4> Specify fields or _properties_ in each mapping type.
  132. <5> Specify the data `type` and mapping for each field.
  133. --
  134. include::mapping/types.asciidoc[]
  135. include::mapping/fields.asciidoc[]
  136. include::mapping/params.asciidoc[]
  137. include::mapping/dynamic-mapping.asciidoc[]