2013-08-29 07:24:34 +08:00
|
|
|
[[mapping]]
|
|
|
|
= Mapping
|
|
|
|
|
|
|
|
[partintro]
|
|
|
|
--
|
2015-08-06 23:24:29 +08:00
|
|
|
|
|
|
|
Mapping is the process of defining how a document, and the fields it contains,
|
2019-10-07 21:37:23 +08:00
|
|
|
are stored and indexed. For instance, use mappings to define:
|
2015-08-06 23:24:29 +08:00
|
|
|
|
|
|
|
* which string fields should be treated as full text fields.
|
|
|
|
* which fields contain numbers, dates, or geolocations.
|
|
|
|
* the <<mapping-date-format,format>> of date values.
|
|
|
|
* custom rules to control the mapping for
|
|
|
|
<<dynamic-mapping,dynamically added fields>>.
|
2013-08-29 07:24:34 +08:00
|
|
|
|
2020-12-10 06:54:58 +08:00
|
|
|
A mapping definition includes metadata fields and fields:
|
2015-08-06 23:24:29 +08:00
|
|
|
|
2020-08-06 01:21:00 +08:00
|
|
|
<<mapping-fields,Metadata fields>>::
|
2015-08-06 23:24:29 +08:00
|
|
|
|
2020-08-06 01:21:00 +08:00
|
|
|
Metadata fields are used to customize how a document's associated metadata is
|
|
|
|
treated. Examples of metadata fields include the document's
|
2020-03-24 03:26:22 +08:00
|
|
|
<<mapping-index-field,`_index`>>, <<mapping-id-field,`_id`>>, and
|
|
|
|
<<mapping-source-field,`_source`>> fields.
|
2015-08-06 23:24:29 +08:00
|
|
|
|
2020-09-03 06:00:01 +08:00
|
|
|
<<mapping-types,Fields>>::
|
2015-08-06 23:24:29 +08:00
|
|
|
|
2020-03-24 03:26:22 +08:00
|
|
|
A mapping contains a list of fields or `properties` pertinent to the
|
2020-09-03 06:00:01 +08:00
|
|
|
document. Each field has its own <<mapping-types, data type>>.
|
2015-08-06 23:24:29 +08:00
|
|
|
|
2020-03-24 03:26:22 +08:00
|
|
|
NOTE: Before 7.0.0, the 'mappings' definition used to include a type name.
|
|
|
|
For more details, please see <<removal-of-types>>.
|
2013-08-29 07:24:34 +08:00
|
|
|
|
2020-07-23 23:48:22 +08:00
|
|
|
[discrete]
|
2020-12-10 06:54:58 +08:00
|
|
|
[[mapping-limit-settings]]
|
|
|
|
== Settings to prevent mapping explosion
|
|
|
|
Defining too many fields in an index can lead to a mapping explosion, which can
|
|
|
|
cause out of memory errors and difficult situations to recover from.
|
2020-05-20 01:40:26 +08:00
|
|
|
|
|
|
|
Consider a situation where every new document inserted
|
|
|
|
introduces new fields, such as with <<dynamic-mapping,dynamic mapping>>.
|
|
|
|
Each new field is added to the index mapping, which can become a
|
2017-07-14 15:47:41 +08:00
|
|
|
problem as the mapping grows.
|
2020-05-20 01:40:26 +08:00
|
|
|
|
2020-12-10 06:54:58 +08:00
|
|
|
Use the <<mapping-settings-limit,mapping limit settings>> to limit the number
|
|
|
|
of field mappings (created manually or dynamically) and prevent documents from
|
|
|
|
causing a mapping explosion.
|
|
|
|
|
|
|
|
[discrete]
|
|
|
|
[[runtime-fields]]
|
|
|
|
== Runtime fields
|
|
|
|
Typically, you index data into {es} to promote faster search. However, indexing
|
|
|
|
can be slow and requires more disk space, and you have to reindex your data to
|
|
|
|
add fields to existing documents.
|
|
|
|
|
|
|
|
<<runtime,Runtime fields>> are not indexed, which saves disk space and makes
|
|
|
|
data ingest faster. You can add runtime fields to existing documents without
|
|
|
|
reindexing your data and calculate field values dynamically at search time.
|
2016-09-22 20:57:05 +08:00
|
|
|
|
2020-07-23 23:48:22 +08:00
|
|
|
[discrete]
|
2020-12-10 06:54:58 +08:00
|
|
|
[[dynamic-mapping-intro]]
|
2015-08-06 23:24:29 +08:00
|
|
|
== Dynamic mapping
|
2013-08-29 07:24:34 +08:00
|
|
|
|
2015-08-06 23:24:29 +08:00
|
|
|
Fields and mapping types do not need to be defined before being used. Thanks
|
2017-07-05 18:30:19 +08:00
|
|
|
to _dynamic mapping_, new field names will be added automatically, just by
|
|
|
|
indexing a document. New fields can be added both to the top-level mapping
|
|
|
|
type, and to inner <<object,`object`>> and <<nested,`nested`>> fields.
|
2013-08-29 07:24:34 +08:00
|
|
|
|
2017-07-05 18:30:19 +08:00
|
|
|
The <<dynamic-mapping,dynamic mapping>> rules can be configured to customise
|
|
|
|
the mapping that is used for new fields.
|
2013-08-29 07:24:34 +08:00
|
|
|
|
2020-07-23 23:48:22 +08:00
|
|
|
[discrete]
|
2015-08-06 23:24:29 +08:00
|
|
|
== Explicit mappings
|
2013-08-29 07:24:34 +08:00
|
|
|
|
2015-08-06 23:24:29 +08:00
|
|
|
You know more about your data than Elasticsearch can guess, so while dynamic
|
|
|
|
mapping can be useful to get started, at some point you will want to specify
|
|
|
|
your own explicit mappings.
|
2015-06-23 05:49:45 +08:00
|
|
|
|
2019-08-19 21:32:37 +08:00
|
|
|
You can create field mappings when you <<create-mapping,create an index>> and
|
|
|
|
<<add-field-mapping,add fields to an existing index>>.
|
2015-08-06 23:24:29 +08:00
|
|
|
|
2020-07-23 23:48:22 +08:00
|
|
|
[discrete]
|
2019-08-19 21:32:37 +08:00
|
|
|
[[create-mapping]]
|
2020-12-10 06:54:58 +08:00
|
|
|
=== Create an index with an explicit mapping
|
2015-08-06 23:24:29 +08:00
|
|
|
|
2019-08-19 21:32:37 +08:00
|
|
|
You can use the <<indices-create-index,create index>> API to create a new index
|
|
|
|
with an explicit mapping.
|
|
|
|
|
2019-09-06 22:55:16 +08:00
|
|
|
[source,console]
|
2019-08-19 21:32:37 +08:00
|
|
|
----
|
2020-07-28 02:46:39 +08:00
|
|
|
PUT /my-index-000001
|
2019-08-19 21:32:37 +08:00
|
|
|
{
|
|
|
|
"mappings": {
|
|
|
|
"properties": {
|
|
|
|
"age": { "type": "integer" }, <1>
|
|
|
|
"email": { "type": "keyword" }, <2>
|
|
|
|
"name": { "type": "text" } <3>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
<1> Creates `age`, an <<number,`integer`>> field
|
|
|
|
<2> Creates `email`, a <<keyword,`keyword`>> field
|
|
|
|
<3> Creates `name`, a <<text,`text`>> field
|
2015-08-07 00:43:52 +08:00
|
|
|
|
2020-07-23 23:48:22 +08:00
|
|
|
[discrete]
|
2019-08-19 21:32:37 +08:00
|
|
|
[[add-field-mapping]]
|
|
|
|
== Add a field to an existing mapping
|
2015-08-07 00:43:52 +08:00
|
|
|
|
2019-08-19 21:32:37 +08:00
|
|
|
You can use the <<indices-put-mapping, put mapping>> API to add one or more new
|
|
|
|
fields to an existing index.
|
|
|
|
|
|
|
|
The following example adds `employee-id`, a `keyword` field with an
|
|
|
|
<<mapping-index,`index`>> mapping parameter value of `false`. This means values
|
|
|
|
for the `employee-id` field are stored but not indexed or available for search.
|
2015-08-07 00:43:52 +08:00
|
|
|
|
2019-09-06 22:55:16 +08:00
|
|
|
[source,console]
|
2019-08-19 21:32:37 +08:00
|
|
|
----
|
2020-07-28 02:46:39 +08:00
|
|
|
PUT /my-index-000001/_mapping
|
2015-08-07 00:43:52 +08:00
|
|
|
{
|
2019-08-19 21:32:37 +08:00
|
|
|
"properties": {
|
|
|
|
"employee-id": {
|
|
|
|
"type": "keyword",
|
|
|
|
"index": false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
// TEST[continued]
|
|
|
|
|
2020-07-23 23:48:22 +08:00
|
|
|
[discrete]
|
2019-08-19 21:32:37 +08:00
|
|
|
[[update-mapping]]
|
|
|
|
=== Update the mapping of a field
|
|
|
|
|
2020-06-02 06:55:05 +08:00
|
|
|
include::{es-repo-dir}/indices/put-mapping.asciidoc[tag=change-field-mapping]
|
2019-10-17 21:30:46 +08:00
|
|
|
|
2020-06-02 06:55:05 +08:00
|
|
|
include::{es-repo-dir}/indices/put-mapping.asciidoc[tag=rename-field]
|
2019-08-19 21:32:37 +08:00
|
|
|
|
2020-07-23 23:48:22 +08:00
|
|
|
[discrete]
|
2019-08-19 21:32:37 +08:00
|
|
|
[[view-mapping]]
|
|
|
|
== View the mapping of an index
|
|
|
|
|
|
|
|
You can use the <<indices-get-mapping, get mapping>> API to view the mapping of
|
|
|
|
an existing index.
|
|
|
|
|
2019-09-06 22:55:16 +08:00
|
|
|
[source,console]
|
2019-08-19 21:32:37 +08:00
|
|
|
----
|
2020-07-28 02:46:39 +08:00
|
|
|
GET /my-index-000001/_mapping
|
2019-08-19 21:32:37 +08:00
|
|
|
----
|
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
The API returns the following response:
|
|
|
|
|
2019-09-06 04:47:18 +08:00
|
|
|
[source,console-result]
|
2019-08-19 21:32:37 +08:00
|
|
|
----
|
|
|
|
{
|
2020-07-28 02:46:39 +08:00
|
|
|
"my-index-000001" : {
|
2019-08-19 21:32:37 +08:00
|
|
|
"mappings" : {
|
|
|
|
"properties" : {
|
|
|
|
"age" : {
|
|
|
|
"type" : "integer"
|
|
|
|
},
|
|
|
|
"email" : {
|
|
|
|
"type" : "keyword"
|
|
|
|
},
|
|
|
|
"employee-id" : {
|
|
|
|
"type" : "keyword",
|
|
|
|
"index" : false
|
|
|
|
},
|
|
|
|
"name" : {
|
|
|
|
"type" : "text"
|
|
|
|
}
|
2015-08-07 00:43:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 21:32:37 +08:00
|
|
|
----
|
|
|
|
|
|
|
|
|
2020-07-23 23:48:22 +08:00
|
|
|
[discrete]
|
2019-08-19 21:32:37 +08:00
|
|
|
[[view-field-mapping]]
|
|
|
|
== View the mapping of specific fields
|
|
|
|
|
|
|
|
If you only want to view the mapping of one or more specific fields, you can use
|
|
|
|
the <<indices-get-field-mapping, get field mapping>> API.
|
|
|
|
|
|
|
|
This is useful if you don't need the complete mapping of an index or your index
|
|
|
|
contains a large number of fields.
|
|
|
|
|
|
|
|
The following request retrieves the mapping for the `employee-id` field.
|
|
|
|
|
2019-09-06 22:55:16 +08:00
|
|
|
[source,console]
|
2019-08-19 21:32:37 +08:00
|
|
|
----
|
2020-07-28 02:46:39 +08:00
|
|
|
GET /my-index-000001/_mapping/field/employee-id
|
2019-08-19 21:32:37 +08:00
|
|
|
----
|
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
The API returns the following response:
|
|
|
|
|
2019-09-06 04:47:18 +08:00
|
|
|
[source,console-result]
|
2019-08-19 21:32:37 +08:00
|
|
|
----
|
|
|
|
{
|
2020-07-28 02:46:39 +08:00
|
|
|
"my-index-000001" : {
|
2019-08-19 21:32:37 +08:00
|
|
|
"mappings" : {
|
|
|
|
"employee-id" : {
|
|
|
|
"full_name" : "employee-id",
|
|
|
|
"mapping" : {
|
|
|
|
"employee-id" : {
|
|
|
|
"type" : "keyword",
|
|
|
|
"index" : false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
----
|
2015-08-07 00:43:52 +08:00
|
|
|
|
2015-08-06 23:24:29 +08:00
|
|
|
--
|
|
|
|
|
2017-07-05 18:30:19 +08:00
|
|
|
include::mapping/removal_of_types.asciidoc[]
|
|
|
|
|
2020-12-10 06:54:58 +08:00
|
|
|
include::mapping/mapping-settings-limit.asciidoc[]
|
|
|
|
|
2015-08-06 23:24:29 +08:00
|
|
|
include::mapping/types.asciidoc[]
|
2013-08-29 07:24:34 +08:00
|
|
|
|
2020-12-10 06:54:58 +08:00
|
|
|
include::mapping/runtime.asciidoc[]
|
|
|
|
|
2015-08-06 23:24:29 +08:00
|
|
|
include::mapping/fields.asciidoc[]
|
|
|
|
|
|
|
|
include::mapping/params.asciidoc[]
|
2013-08-29 07:24:34 +08:00
|
|
|
|
2015-08-06 23:24:29 +08:00
|
|
|
include::mapping/dynamic-mapping.asciidoc[]
|