映射就是 Mapping,它用来定义一个文档以及文档所包含的字段该如何被存储和索引。所以,它其实有点类似于关系型数据库中表的定义。
映射分类
动态映射
顾名思义,就是自动创建出来的映射。es 根据存入的文档,自动分析出来文档中字段的类型以及存储方式,这种就是动态映射。
举一个简单例子,新建一个索引,然后查看索引信息:
在这里插入代码片
在创建好的索引信息中,可以看到,mappings 为空,这个 mappings 中保存的就是映射信息。
现在我们向索引中添加一个文档,如下:
PUT blog/_doc/1
{"title":"1111","date":"2020-11-11"
}
文档添加成功后,就会自动生成 Mappings:
可以看到,date 字段的类型为 date,title 的类型有两个,text 和 keyword。
默认情况下,文档中如果新增了字段,mappings 中也会自动新增进来。
有的时候,如果希望新增字段时,能够抛出异常来提醒开发者,这个可以通过 mappings 中 dynamic 属性来配置。
dynamic 属性有三种取值:
true,默认即此。自动添加新字段。
false,忽略新字段。
strict,严格模式,发现新字段会抛出异常。
具体配置方式如下,创建索引时指定 mappings(这其实就是静态映射):
PUT blog
{"mappings": {"dynamic":"strict","properties": {"title":{"type": "text"},"age":{"type":"long"}}}
}
然后向 blog 中索引中添加数据:
PUT blog/_doc/2
{"title":"1111","date":"2020-11-11","age":99
}
在添加的文档中,多出了一个 date 字段,而该字段没有预定义,所以这个添加操作就会报错:
{"error" : {"root_cause" : [{"type" : "strict_dynamic_mapping_exception","reason" : "mapping set to strict, dynamic introduction of [date] within [_doc] is not allowed"}],"type" : "strict_dynamic_mapping_exception","reason" : "mapping set to strict, dynamic introduction of [date] within [_doc] is not allowed"},"status" : 400
}
动态映射还有一个日期检测的问题。
例如新建一个索引,然后添加一个含有日期的文档,如下:
PUT blog/_doc/1
{"remark":"2020-11-11"
}
添加成功后,remark 字段会被推断是一个日期类型。
此时,remark 字段就无法存储其他类型了。
PUT blog/_doc/1
{"remark":"javaboy"
}
此时报错如下:
{"error" : {"root_cause" : [{"type" : "mapper_parsing_exception","reason" : "failed to parse field [remark] of type [date] in document with id '1'. Preview of field's value: 'javaboy'"}],"type" : "mapper_parsing_exception","reason" : "failed to parse field [remark] of type [date] in document with id '1'. Preview of field's value: 'javaboy'","caused_by" : {"type" : "illegal_argument_exception","reason" : "failed to parse date field [javaboy] with format [strict_date_optional_time||epoch_millis]","caused_by" : {"type" : "date_time_parse_exception","reason" : "Failed to parse with all enclosed parsers"}}},"status" : 400
}
要解决这个问题,可以使用静态映射,即在索引定义时,将 remark 指定为 text 类型。也可以关闭日期检测。
PUT blog
{"mappings": {"date_detection": false}
}
此时日期类型就回当成文本来处理。
类型推断
es 中动态映射类型推断方式如下:
json中的数据 | 自动推断出来的数据类型 |
---|---|
null | 没有字段被添加 |
true/false | boolean |
浮点类型 | float |
数字 | long |
json对象 | object |
string | text/keyword/date/double/long |