1 mapping 简介
1.1 什么是mapping
mapping 是定义一个文档包含哪些字段、如何存储和索引的过程。例如,我们可以用mapping定义:
1 哪些字符串字段可以当作全文字段;
2 哪些字段包含数字、日期或者地理位置;
3 日期数据的格式;
4 自定义规则,以便于控制mapping动态添加字段;
简单来说,当向es中插入数据时候,es会自动创建索引,同时建立type以及对应的mapping,mapping 定义了不同的数据类型;在es中既可以通过dynamic mapping,让索引自动建立mapping,自动设置数据类型,又可以根据业务需求提前手动创建mapping,对各个字段进行设置(字段类型、索引行为、分词器等)。
1.2 为何移除多type
es 6.0前,每个索引下可以有多个type字段,如blog/usr,blog/article 等,但官方发现其不合理之处,7.0之后便废弃了索引的多type,每个索引只能有一个type。
es 最初设置index 类似于关系型数据库的一个数据库,type 类似与sql的表,但后来发现这是一个不合理的类比。
在sql db中,同名字段可以在多个不同表中,且在不同表中具有不同的数据类型;然而,在es 同名字段在多个type中必须要有相同的定义,这和sql的表述不符合,而且容易造成误解;此外es中多type的存在会影响Lucene 扽文档压缩效率,基于上述原因es官方决定移除 mapping types 的概念。
当前每个index 都有一个mapping type,mapping type包括Meta-fileds和Fields(或properties) 两部分;meta-fields用来定义一个文档相关的元数据(每个索引都内置这些字段),包括_index,_type,_id, _source 等,properties可以理解为除了meta-fileds外的一系列字段,properties 可以自定义,但是该字段不能和已有的meta-fileds冲突(自定义meta字段会报错)。
2 mapping 数据类型
一个文档中支持多个不同数据类型,具体如下:
- core datatypes
string: text and keyword
Numeric: long, integer, short, byte, double, float, half_float, scaled_float
Date: date
Date nanoseconds: date_nanos
Boolean: boolean
Binary: binary
Range: integer_range, float_range, long_range, double_range, date_range
- complex datatypes
Object: object for single JSON objects
Nested: nested for arrays of JSON objects
- geo datatypes
Geo-point: geo_point for lat/lon points
Geo-shape: geo_shape for complex shapes like polygons
- specialised datatypes
IP, Completion datatype, Token count, mapper-murmur3, mapper-annotated-text, Percolator, Join, Alias, Rank feature, Rank features, Dense vector, Sparse vector, Search-as-you-type
- arrays
数组字段不要去具体类型,但是数组中的值必须要有相同的数据类型
- multi-fields
当把一个字段用于多种不同的方式,从而达到不同的目的的时候,该方法就非常有用;例如字符串类型的字段被设置为text用于全文检索,设置为keyword的时候用于排序、聚合。个人理解如下:
PUT example/_doc/1
{
"addr":{
"street":"001",
"mail":11132
},
"name":"xiaoming is boy"
}
得到name的mapping如下,包括text和keyword类型,可以直接通过name字段就行全文检索,通过name.keyword进行排序聚合等功能。
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
3 mapping 元字段
每个文档都有一些与之相关的元字段,例如 _index, mapping _type, and _id 等常见的meta-fields。
文档身份识别相关的元字段 :_index, _type, _id
文档资源相关的元字段:_source, _size
索引相关的元字段:_field_names, _ignored
路由相关的元字段: _routing
其它元字段: _meta
4 mapping 参数
mapping 参数用于对部分或者全部字段进行限制,使文档能按照要求写入;7.2版本包括如下常见参数:
7.2/mapping-params
analyzer
normalizer
boost
coerce
copy_to
doc_values
dynamic
enabled : 只能用于最上层mapping和object 字段, 设置false后该字段将不可搜索,但是会保存在_source中。
fielddata
eager_global_ordinals
format
ignore_above
ignore_malformed
index_options
index_phrases
index_prefixes
index
fields
norms
null_value
position_increment_gap
properties
search_analyzer
similarity
store
term_vector
5 动态 mapping
es 最重要的一个特征之一:它可以让我们摆脱我们自己的方式,尽快地探索数据。当写入一个数据的文档的时候,我们既不需要提前创建一个索引,又不需要定义一个mapping type,还不需要定义多个字段;我们只需要写入文档到指定的index和type中,es会自动给我创建各种字段, 用es官方表达即:各字段会自动焕发生机(fields will spring to life automatically)。
es 中称 自动检测和添加新字段 为动态mapping。在实际中,我们也可以通过业务需要自定义动态mapping规则,具体包括如下两种方式:
- Dynamic field mappings
可以设置规则用于管理动态字段检测
- Dynamic templates
也可以自定义规则,为动态添加的字段配置mapping
7.2/dynamic-mapping
6 常见 mapping 异常处理
- keyword 类型字段超出某个值
keyword类型字段,其字符串长度又最大限制,因此超过长度会导致报错,此时将字段设置为text, 或者设置"ignore_above":1000 (1000可根据需要更改实际解析的数量)
- boolean 类型字段报错 not of boolean type
解决方法: 设置"enabled":false 或"type": “keyword”
- text 或keyword 类型解析出错
这类解析出错,一般优先设置"enabled":false,确保数据能正确写入
- long 解析出错
假如 mapping中num为long类型,但是写入了字符串,则会报错,错误内容如下: "type": "mapper_parsing_exception",
"reason": "failed to parse field [num] of type [long] in document with id '1'",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "For input string: \"number\""
}
解决方法:
方法一 设置字段为keyword,
方法二 设置 “ignore_malformed”: true ,该方法非自动忽略掉不符合要求的字段,导致该字段无法索引,但是会在 _source中,如下所示:
- object mapping for [num] tried to parse field [num] as object, but found a concrete value
当num为object类型时候,可以正常写入json数据,但此时如果写入非json数据则会报错,如写入222就就导致报错
解决方法:设置"enabled":false
- Limit of total fields [1000] in index [xxx] has been exceeded
es 中一个索引默认1000个字段,超过1000个字段会导致写入出错,出现该问题时需要在mapping的最顶层设置dynamic为false,从而关闭动态mapping。
实际业务中,一般关闭动态mapping,并让用户给出常见字段的mapping类型,只对指定字段进行解析。
当然也可以更改配置参数,增加最大field的数量,但是会导致写入效率很低的问题;实际业务中,如果字段超多,不仅会导致写入报错,还会严重影响索引的写入性能(如:从几十K的写入速度,降低到几百的写入速度),因此正常情况下不推荐增加最大field数量。
- json 解析异常处理方法
异常json解析会导致解析失败,甚至导致pipeline stop,从而无法解析如何数据。报错:
Exception in pipelineworker, the pipeline stopped processing new events, please check your filter configuration and restart Logstash
或者
Error parsing json {:source=>"main_log", :raw=>"xxx", :exception=>#<LogStash::Json::ParserError: Unrecognized token 'peekaboo': was expecting ('true', 'false' or 'null')
at [Source: (byte[])"peekaboo 125.210.57.176 [23/Oct/2020:15:42:05 +0800] "GET /app/v1/user/ping HTTP/1.1" 200 47 "" "okhttp/4.8.1" 0.004 0.007"; line: 1, column: 10]>}
解决方法:skip 掉不合规的json
json {
source => "message"
remove_field => [ "message" ]
skip_on_invalid_json => true
}
- mapping 删除思路
mapping 一旦创建了就不能删除,如果需要删除一个索引的mapping,则可以按照如下方式操作:
创建新索引 ->新索引创建新mapping ->原索引导出数据到新索引 ->新索引创建原索引一致的别名 ->删除原索引
7 说明
参考文档:
7.2/mapping
参考环境:
本文使用的是 es7.2.1 版本