elasticsearch动态映射
动态字段映射与动态模板映射
动态字段映射
默认情况下, Elasticsearch会将新字段添加到类型映射中 ,通过将dynamic参数设置为false(忽略新字段)或strict(如果遇到未知字段,则引发异常),可以在文档和对象级别上禁用此行为。
字段类型自动映射规则
| json格式数据 | 自动推测字段类型 |
|---|---|
| null | 没有字段被添加 |
| true of false | boolean 类型 |
| 浮点类型数字 | float类型 |
| 数字 | long |
| JSON对象 | object |
| 数组 | 由第一个非空值决定 |
| string | date(开启日期检测),double,long类型,text,keyword类型 |
其它类型必须显示映射
日期类型检测
日期类型检测默认是开启的
默认日期类型 [ “strict_date_optional_time”,“yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z”]
PUT my_index/_doc/1
{
"time":"2019-01-01"
}
GET my_index/_mapping
{
"my_index" : {
"mappings" : {
"properties" : {
"time" : {
"type" : "date" //自动检测为日期类型
}
}
}
}
}
自定义日期类型检测
PUT my_index
{
"mappings": {
"dynamic_date_formats": ["MM/dd/yyyy"]
}
}
禁用date类型检测
将date_detection设置成false
DELETE my_index
PUT my_index
{
"mappings": {
"date_detection": false
}
}
PUT my_index/_doc/1
{
"time":"2019-01-01"
}
GET my_index/_mapping
{
"my_index" : {
"mappings" : {
"date_detection" : false,
"properties" : {
"time" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword", //类型为keyword,非日期类型
"ignore_above" : 256
}
}
}
}
}
}
}
数字类型检测
默认情况下是禁用的,“numeric_detection”: true 开启
PUT my_index
{
"mappings": {
"numeric_detection": true
}
}
动态模板映射
通过条件动态设置模板
- match_mapping_type:根据类型匹配
- match , unmatch , match_pattern:根据字段名正则匹配
- path_match , path_unmatch:根据路径匹配
"dynamic_templates": [
{
"my_template_name": { //模板名称
... match conditions ... //匹配条件
"mapping": { ... } //匹配后的映射
}
},
...
]
- 模板名称可以随意定义,可以同时设置多个模板
- 按模板定义先后顺序匹配,匹配后才会应用映射
- 重新设置模板会覆盖之前模板,这样可以动态设置模板,改变模板属性或删除模板
由于json 不区分长整数与整数或双精度数与浮点数 ,因此采用长类型。整数对应long,浮点数对应double
自定义类型匹配
boolean类型:匹配 true,false
date 类型:开启日期类型检测时,根据 formats 匹配成日期类型
double类型:匹配包含小数点数值
long类型:匹配整数
object类型:匹配对象类型
string类型:匹配字符串
*:匹配所有类型
match_mapping_type
DELETE my_index
PUT my_index
{
"mappings": {
"dynamic_templates":[
{
"integers":{
"match_mapping_type":"long", //匹配长整数类型
"mapping":{
"type":"integer"
}
}
},
{
"strings":{
"match_mapping_type":"string",//匹配字符串类型
"mapping":{
"type":"text",
"fields":{
"raw":{
"type":"keyword",
"ignore_above":256
}
}
}
}
}
]
}
}
PUT my_index/_doc/1
{
"my_integer": 5, //映射成整数
"my_string": "Some string" //映射成多域字段
}
GET my_index/_mapping
{
"my_index" : {
"mappings" : {
"dynamic_templates" : [
...............
],
"properties" : {
"my_integer" : {
"type" : "integer"
},
"my_string" : {
"type" : "text",
"fields" : {
"raw" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
覆盖模板
PUT my_index/_mappings
{
"dynamic_templates":[
{
"xxxxx":{
"match_mapping_type":"long",
"mapping":{
"type":"integer"
}
}
}
]
}
GET my_index/_mapping
{
"my_index" : {
"mappings" : {
"dynamic_templates" : [ //动态模板被覆盖,integers与strings被删除了
{
"xxxxx" : {
"match_mapping_type" : "long",
"mapping" : {
"type" : "integer"
}
}
}
],
"properties" : {
"my_integer" : {
"type" : "integer"
},
"my_string" : {
"type" : "text",
"fields" : {
"raw" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
match与unmatch
通配符匹配(*匹配任意个,?匹配一个)
DELETE my_index
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"long_as_string":{
"match_mapping_type":"string",
"match":"long_*",
"unmatch":"*_text",
"mapping":{
"type":"long"
}
}
}
]
}
}
PUT my_index/_doc/1
{
"long_num": "5",
"long_text": "foo"
}
GET my_index/_mapping
{
"my_index" : {
"mappings" : {
"dynamic_templates" : [
{
.........
}
],
"properties" : {
"long_num" : {
"type" : "long"
},
"long_text" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
match_pattern
支持使用完整的java正则匹配,而非简单的通配符匹配
"match_pattern": "regex",
"match": "^profit_\d+$"
path_match and path_unmatch
路径通配符匹配,只匹配叶子节点
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"full_name": {
"path_match": "name.*",
"path_unmatch": "*.middle",
"mapping": {
"type": "text",
"copy_to": "full_name"
}
}
}
]
}
}
PUT my_index/_doc/1
{
"name": {
"first": "John",
"middle": "Winston",
"last": "Lennon"
}
}
{name}和{dynamic_type}
{name}占位符匹配字段名,{dynamic_type}占位符匹配检测到的类型
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"named_analyzers": {
"match_mapping_type": "string",
"match": "*",
"mapping": {
"type": "text",
"analyzer": "{name}"
}
}
},
{
"no_doc_values": {
"match_mapping_type":"*",
"mapping": {
"type": "{dynamic_type}",
"doc_values": false
}
}
}
]
}
}
PUT my_index/_doc/1
{
"english": "Some English text", //采用english分词器
"count": 5 //long类型,禁用doc_values
}
结构化搜索
默认情况下,elasticsearch会将字符串字段映射为带有子关键字字段的文本字段
POST text/_doc
{
//字符串类型被映射为text类型,并且带有keyword字段的多域字段
"text":"string mapping multi field, text and keyword"
}
GET text/_mapping
{
"text" : {
"mappings" : {
"properties" : {
"text" : { //字符串类型被映射为text类型,并且带有keyword字段的多域字段
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
字符串映射为keyword类型
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"strings_as_keywords": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
}
字符串只映射为text
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"strings_as_text": {
"match_mapping_type": "string",
"mapping": {
"type": "text"
}
}
}
]
}
}
禁用 norms
不关心评分可以禁用norms以节省空间
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"strings_as_keywords": {
"match_mapping_type": "string",
"mapping": {
"type": "text",
"norms": false,
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
]
}
}
禁用索引
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"unindexed_longs": {
"match_mapping_type": "long",
"mapping": {
"type": "long",
"index": false //禁用索引
}
}
},
{
"unindexed_doubles": {
"match_mapping_type": "double",
"mapping": {
"type": "float",
"index": false
}
}
}
]
}
}
本文详细介绍了Elasticsearch的动态映射功能,包括动态字段映射和动态模板映射。动态字段映射允许自动添加新字段并规定字段类型检测,如日期和数字类型。动态模板映射则可以根据条件设定不同类型的映射规则,如match_mapping_type、match_pattern等。此外,还讨论了如何禁用特定类型的检测和优化映射设置。


被折叠的 条评论
为什么被折叠?



