ES可以自动检测新字段并根据数据自动添加到映射中。这是动态映射的一个强大特性,可以简化索引管理。但是,动态映射可能导致字段类型不一致和性能问题。因此,在生产环境中,最好在索引创建之前明确定义映射。
动态映射规则
- null:不添加任何字段
- true/false: boolean
- 浮点数:float
- 整数:long
- 对象:对象类型字段
- 数组:取决于第一个元素的类型
- 字符串:datefloatlongtext-keyword
自动映射
直接塞入数据,默认自动创建索引
代码语言:javascript复制PUT lglbc_dynamic_mapping/_doc/1
{
"string_field":"字符串",
"date_field":"2023/07/29 10:00:00",
"date_field2":"2023-07-29 10:00:00",
"bool_field":true,
"long_field":5,
"object_field":{
"name":"乐哥聊编程"
},
"float_field":4.5,
"null_field":null
}
获取生成的映射
代码语言:javascript复制GET lglbc_dynamic_mapping/_mapping
可以看出,所有字段类型都是按照规则生成的,日期也能解析出来,但是注意到date_field2并没有被解析成日期类型,这是因为es默认不支持这种日期格式,所以自己配置
代码语言:javascript复制{
"lglbc_dynamic_mapping": {
"mappings": {
"properties": {
"bool_field": {
"type": "boolean"
},
"date_field": {
"type": "date",
"format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
},
"date_field2": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"float_field": {
"type": "float"
},
"long_field": {
"type": "long"
},
"object_field": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"string_field": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
手动配置,支持指定日期格式
代码语言:javascript复制PUT lglbc_dynamic_mapping
{
"mappings": {
"dynamic_date_formats": ["yyyy-MM-dd HH:mm:ss","yyyy/MM/dd HH:mm:ss"]
}
}
代码语言:javascript复制GET lglbc_dynamic_mapping/_mapping
{
"lglbc_dynamic_mapping": {
"mappings": {
"dynamic_date_formats": [
"yyyy-MM-dd HH:mm:ss",
"yyyy/MM/dd HH:mm:ss"
],
"properties": {
"bool_field": {
"type": "boolean"
},
"date_field": {
"type": "date",
"format": "yyyy/MM/dd HH:mm:ss"
},
"date_field2": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"float_field": {
"type": "float"
},
"long_field": {
"type": "long"
},
"object_field": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"string_field": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
字符串数值自动转换
执行如下操作,string_int字段会被识别成text字段
代码语言:javascript复制PUT lglbc_dynamic_mapping/_doc/1
{
"string_field":"字符串",
"string_int":"10",
"date_field":"2023/07/29 10:00:00",
"date_field2":"2023-07-29 10:00:00",
"bool_field":true,
"long_field":5,
"object_field":{
"name":"乐哥聊编程"
},
"float_field":4.5,
"null_field":null
}
配置mapping。使其能自动转换
代码语言:javascript复制PUT lglbc_dynamic_mapping
{
"mappings": {
"dynamic_date_formats": ["yyyy-MM-dd HH:mm:ss","yyyy/MM/dd HH:mm:ss"],
"numeric_detection": true
}
}
名称匹配指定类型
以long_开头且不以text结尾的字符串字段,将被映射成long类型
代码语言:javascript复制PUT lglbc_dynamic_mapping
{
"mappings": {
"dynamic_templates":[
{
"long_as_string":{
"match_mapping_type":"string",
"match":"long_*",
"unmatch":"*_text",
"mapping":{
"type":"long"
}
}
}]
}
}
代码语言:javascript复制{
"lglbc_dynamic_mapping": {
"mappings": {
"dynamic_templates": [
{
"long_as_string": {
"match": "long_*",
"unmatch": "*_text",
"match_mapping_type": "string",
"mapping": {
"type": "long"
}
}
}
],
"properties": {
"long_num": {
"type": "long"
},
"long_text": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}