注意:
1、数据类型变化
从Elasticsearch 2.x到5.x版本,数据类型从原来的几种增加到几十种,并且区分field datatype和Meta-field。5.x中的多种数据类型在6.x中将继续使用,但是从7.x开始将不再支持,将会只有一种映射的数据类型。
2、说明
虽然将数据库中database、table、column与es进行类比是不正确的,但是可以比较说明问题。同一数据库中两个表中都有相同的字段名,但是字段名的类型可以是完全不相同的。但是在es中则使用相同的字段名则类型必须相同,并且mapping的时候会有影响,这样底层的lucene在对数据进行索引的时候则效率降低。所以以后版本趋势会将数据类型合并为只有一种。
那么遇到上面的问题,可以在同一index中的不同type中不要使用相同的field名称,或者在不同的index中创建type即可。
3、删除映射类型的版本迭代
Elasticsearch 5.6.0
1)、5.6中设置index.mapping.single_type: true:在6.0中将可以使用single-type-per-index 配置继续使用。
2)、5.6中的parent-child(父-子节点)在后续版本中将替换为join datatype。
Elasticsearch 6.x
1)、在5.x版本中索引的数据在6中可以和在5.x一样正常使用。
2)、6.x的索引只允许一个单类型的索引。任何名称都可以用该类型,但只能使用该类型。
3)、类型名称不能再与id相结合,以形成uid(全局唯一)字段。uid字段已成为id字段的别名。
4)、新的索引不再支持老版本的的parent-child(父/子)模式,而是应该使用join datatype。
5)、默认的映射类型被弃用。
Elasticsearch 7.x
1) 、url中的类型参数是可选的。例如,索引文档不再需要文档类型。
2)、GET|PUT _mapping APIs support a query string parameter (include_type_name) which indicates whether the body should include a layer for the type name. It defaults to true. 7.x indices which don’t have an explicit type will use the dummy type name _doc.
3)、_default_映射类型被删除,不再使用。
Elasticsearch 8.x
1)、在url中不再支持类型参数。
2)、include_type_name参数默认为false。
Elasticsearch 9.x
include_type_name参数会被删除。
一、Field datatypes(字段的数据类型)
二、Meta-Fields(元数据类型)
三、Mapping parameters(映射参数)
· analyzer
· boost
· coerce
· copy_to
· dynamic
· enabled
· format
· index
· fields
· norms
· store
四、Dynamic Mapping(动态映射)
1、_default_ mapping(默认映射)
当索引(rest POST)或者修改(rest PUT)一个数据文档的时候时候会使用到_default_进行映射数据文档,以添加或修改数据,默认映射类型可以与。
PUT my_index { "mappings": { "_all": { "enabled": false } }, "_all": { "enabled": true } } } }
|
PUT my_index
{
"mappings": {
"_default_": {
"_all": {
"enabled": false
}
},
"user": {},
"blogpost": {
"_all": {
"enabled": true
}
}
}
}
1)、_default_映射的_all字段的默认值是false
2)、user属性的值继承自_default_.
3)、blogpost的_all的enabled默认为true
2、Dynamic field mapping(动态字段映射)
1)、动态字段映射
默认情况下,当一个文档中发现了一个未被发现的字段时,Elasticsearch将把新字段添加到类型映射中,当然也可以通过这是dynamic属性为false,忽略新发现的字段,不进行索引。也可以设置为strict ,当发现新字段是,抛出异常,设置方式如下:
PUT my_index
{
"mappings": {
"my_type": {
"dynamic": false,
"properties": {
"user": {
"properties": {
"name": {
"type": "text"
},
"social_networks": {
"dynamic": true,
"properties": {}
}
}
}
}
}
}
}
PUT my_index { "mappings": { "my_type": { "properties": { "properties": { "name": { "type": "text" }, "dynamic": true, "properties": {} } } } } } } } |
以下是json数据与Elasticsearch的动态映射对比:
JSON datatype | Elasticsearch datatype |
null | No field is added. |
true or false | boolean field |
floating point number | float field |
integer | long field |
object | object field |
array | Depends on the first non-null value in the array. |
string | string 类型则会添加一个text的主字段和一个.keyword后缀的子字段 |
2) 、Date detection(日期映射)
1、默认日期映射
date_detection: 日期探测属性(可设置为true false),默认为true。则会自动映射日期类型,并且通过dynamic_date_formats属性进行映射,该属性的默认格式为"yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z",只能映射该两种数据格式,例如以下则可以创建一个es的数据类型:
PUT my_index/my_type/1 { "create_date": "2015/09/02" }
|
2、关闭日期映射
date_detection属性设置为false,则不进行日期数据类型映射,但是使用kibana进行数据分析的话,一般需要一个日期字段。设置方式如下:
PUT my_index { "mappings": { "my_type": { "date_detection": false } } } |
PUT my_index
{
"mappings": {
"my_type": {
"date_detection": false
}
}
}
3、自定义日期映射类型
设置方式如下:
PUT my_index { "mappings": { "my_type": { "dynamic_date_formats": ["MM/dd/yyyy"] } } } |
PUT my_index
{
"mappings": {
"my_type": {
"dynamic_date_formats": ["MM/dd/yyyy"]
}
}
}
3) 、Numeric detection(数字探测)
数字映射默认是关闭的,需要配置numeric_detection属性为true,通过如下方式进行映射,则分别会映射为es的float和long类型:
PUT my_index
{
"mappings": {
"my_type": {
"numeric_detection": true
}
}
}
PUT my_index/my_type/1
{
"my_float": "1.0",
"my_integer": "1"
}
3、Dynamic templates(动态模板)
4、Override default template(覆盖默认模板)
PUT my_index
{
"mappings": {
"my_type": {
"numeric_detection": true
}
}
}
PUT my_index/my_type/1
{
"my_float": "1.0",
"my_integer": "1"
}