在mapping中使用default字段,那么其它字段会自动继承default中的设置。node
PUT http://node1:9200/my_index { "mappings":{ "_default_":{ "_all":{ "enable":false } }, "user":{ }, "blogpost":{ "_all":{ "enable":true } } } }
default 中的_all字段在 5.x 中已经被废弃 ,因此上面会出异常 可是规则可用。apache
上面的mapping中,default中关闭了all字段,user会继承_default中的配置,所以user中的all字段也是关闭的,blogpost中开启_all,覆盖了_default的默认配置。json
当default被更新之后,只会对后面新加的文档产生做用。数组
文档中有一个以前没有出现过的字段被添加到ELasticsearch以后,文档的type mapping中会自动添加一个新的字段。这个能够经过dynamic属性去控制,dynamic属性为false会忽略新增的字段、dynamic属性为strict会抛出异常。若是dynamic为true的话,ELasticsearch会自动根据字段的值推测出来类型进而肯定mapping:app
JSON格式的数据 | 自动推测的字段类型 |
---|---|
null | 没有字段被添加 |
true or false | boolean类型 |
floating类型数字 | floating类型 |
integer | long类型 |
JSON对象 | object类型 |
数组 | 由数组中第一个非空值决定 |
string | 有多是date类型(开启日期检测)、double或long类型、text类型、keyword类型 |
日期检测默认是检测符合如下日期格式的字符串:ide
[ "strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]
例子:post
POST http://node1:9200/my_index/my_type/1 { "create_date": "2015/09/02" } GET http://node1:9200/my_index/_mapping --> { "my_index": { "mappings": { "my_type": { "properties": { "create_date": { "type": "date", "format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis" } } } } } }
关闭日期检测:spa
PUT http://node1:9200/my_index { "mappings":{ "my_type":{ "date_detection":false } } } POST http://node1:9200/my_index/my_type/1 { "create_date": "2015/09/02" } GET http://node1:9200/my_index/_mapping --> { "my_index": { "mappings": { "my_type": { "date_detection": false, "properties": { "create_date": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } } } }
自定义日期检测的格式:code
PUT http://node1:9200/my_index { "mappings":{ "my_type":{ "dynamic_date_formats":["MM/dd/yyyy"] } } } POST http://node1:9200/my_index/my_type/1 { "create_date": "09/25/2015" } GET http://node1:9200/my_index/_mapping --> { "my_index": { "mappings": { "my_type": { "dynamic_date_formats": [ "MM/dd/yyyy" ], "properties": { "create_date": { "type": "date", "format": "MM/dd/yyyy" } } } } } }
开启数字类型自动检测:orm
PUT http://node1:9200/my_index { "mappings":{ "my_type":{ "numeric_detection":true } } } POST http://node1:9200/my_index/my_type/1 { "my_float": "1.0", "my_integer": "1" } GET http://node1:9200/my_index/_mapping --> { "my_index": { "mappings": { "my_type": { "numeric_detection": true, "properties": { "my_float": { "type": "float" }, "my_integer": { "type": "long" } } } } } }
动态模板能够根据字段名称设置mapping,以下对于string类型的字段,设置mapping为:
"mapping": { "type": "long"}
可是匹配字段名称为long_*格式的,不匹配*_text格式的:
PUT http://node1:9200/my_index { "mappings": { "my_type": { "dynamic_templates": [ { "longs_as_strings": { "match_mapping_type": "string", "match": "long_*", "unmatch": "*_text", "mapping": { "type": "long" } } } ] } } } PUT http://node1:9200/my_index/my_type/1 { "long_num": "5", "long_text": "foo" }
写入文档之后,long_num字段为long类型,long_text扔为string类型。
能够经过default字段覆盖全部索引的mapping配置,例子:
PUT _template/disable_all_field { "order": 0, "template": "*", "mappings": { "_default_": { "_all": { "enabled": false } } } }