四、动态Mapping
4.1 default mapping
在mapping中使用default字段,那么其它字段会自动继承default中的设置。
PUT http://node1:9200/my_index
{
"mappings":{
"_default_":{
"_all":{
"enable":false
}
},
"user":{
},
"blogpost":{
"_all":{
"enable":true
}
}
}
}
default 中的_all字段在 5.x 中已经被废弃 ,所以上面会出异常 但是规则可用。
上面的mapping中,default中关闭了all字段,user会继承_default中的配置,因此user中的all字段也是关闭的,blogpost中开启_all,覆盖了_default的默认配置。
当default被更新以后,只会对后面新加的文档产生作用。
4.2 Dynamic field mapping
文档中有一个之前没有出现过的字段被添加到ELasticsearch之后,文档的type mapping中会自动添加一个新的字段。这个可以通过dynamic属性去控制,dynamic属性为false会忽略新增的字段、dynamic属性为strict会抛出异常。如果dynamic为true的话,ELasticsearch会自动根据字段的值推测出来类型进而确定mapping:
JSON格式的数据 | 自动推测的字段类型 |
---|---|
null | 没有字段被添加 |
true or false | boolean类型 |
floating类型数字 | floating类型 |
integer | long类型 |
JSON对象 | object类型 |
数组 | 由数组中第一个非空值决定 |
string | 有可能是date类型(开启日期检测)、double或long类型、text类型、keyword类型 |
日期检测默认是检测符合以下日期格式的字符串:
[ "strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]
例子:
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"
}
}
}
}
}
}
关闭日期检测:
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
}
}
}
}
}
}
}
}
自定义日期检测的格式:
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"
}
}
}
}
}
}
开启数字类型自动检测:
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"
}
}
}
}
}
}
4.3 Dynamic templates
动态模板可以根据字段名称设置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类型。
4.4 Override default template
可以通过default字段覆盖所有索引的mapping配置,例子:
PUT _template/disable_all_field
{
"order": 0,
"template": "*",
"mappings": {
"_default_": {
"_all": {
"enabled": false
}
}
}
}