- Mapping类似数据库中的schema的定义,作用如下
- 定义索引汇总字段的名称
- 定义字段的数据类型,如字符串、数字,时间。。。
- 字段、倒排索引的相关配置(Analyzed or Not Analyzed, Analyzer)
- Mapping会吧JSON文档映射成Lucene所需的扁平格式
- 一个Mapping属于一个索引的Type
- 每个document都属于一个Type
- 一个Type有一个Mapping定义
- 7.0开始,不需要在Mapping定义中指定Type信息
字段的数据类型
- 简单类型
- Text / Keyword
- Date
- Integer / Floating
- Boolean
- IPv4 & IPv6
- 复杂类型
- 对象和嵌套对象
- 特殊类型
- geo_point & geo_shape / percolator (地理信息)
Dynamic Mapping
- 写入文档时,如果索引不存在,会自动个创建索引
- Dynamic Mapping的机制,使得我们无需手动定义mappings
- 有时推断不准,如地理位置信息
- 当类型设置不对时,会导致查询异常,如range查询(待查询数字类型,但是字段为字符串类型,range时会出错)
类型自动识别
| JSON类型 | Elasticsearch类型 |
|---|---|
| 字符串 | - 匹配日期格式,设置成Date - 配置数字设置为float或long类型,该选项默认关闭 - 设置为Text类型,并增加keyword子字段 |
| 布尔值 | boolean |
| 浮点数 | float |
| 整数 | long |
| 对象 | object |
| 数组 | 由第一个非空数值的类型所决定 |
| 空值 | 忽略 |
例子
POST mapping_test/_doc{"uid": "123.123", # text, keyword"isVIP": true, # boolean"isAdmin": "true", # text, keyword"age": 18, # long"heigh": 180.3 # float"createTIme": "2019-12-26 00:00:00", # date}# Dynamic Mapping结果GET mapping_test/_mapping{"mapping_test" : {"mappings" : {"properties" : {"age" : {"type" : "long"},"createTime" : {"type" : "date"},"heigh" : {"type" : "float"},"isAdmin" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"isVIP" : {"type" : "boolean"},"uid" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}}}}}}
能否更改Mapping的字段类型
新加入的字段
- Dynamic设置为true时,一旦有新增字段的文档写入,Mapping也会同时更新
- Dynamic设置为false时,Mapping不会被更新,新增字段的数据无法被索引,但是信息会在
_source中出现 -
已有字段
一旦已经有数据写入,就不再支持修改字段定义
- Lucene实现的倒排索引,一旦生成后,就不允许修改
如果希望改变字段类型,必须Reindex API , 重建索引
原因
如果修改了字段的数据类型,会导致已经被索引的属性无法被搜索
- 如果是增加了新的字段,就不会有这样的影响
控制Dynamic Mappings更新增字段策略
| true | false | strict | |
|---|---|---|---|
| 文档可索引 | YES | YES | NO |
| 字段可索引 | YES | NO | NO |
| Mapping被更新 | YES | NO | NO |
PUT mapping_test/_mapping{"dynamic": "true"}
测试
DELETE mapping_test
POST mapping_test/_doc
{
"uid": "123.123"
}
# 查看第一次的mapping
GET mapping_test/_mapping
# 结果
{
"mapping_test" : {
"mappings" : {
"properties" : {
"uid" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
# 设置mapping 为false
PUT mapping_test/_mapping
{
"dynamic": "false"
}
# 新增字段
POST mapping_test/_doc
{
"uid": "123.123",
"isVIP": true
}
# 查看结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "mapping_test",
"_type" : "_doc",
"_id" : "pP76QG8BQ6e2WndFtrk4",
"_score" : 1.0,
"_source" : {
"uid" : "123.123"
}
},
{
"_index" : "mapping_test",
"_type" : "_doc",
"_id" : "yf77QG8BQ6e2WndFFLlJ",
"_score" : 1.0,
"_source" : {
"uid" : "123.123",
"isVIP" : true # 新增的字段在_source中出现
}
}
]
}
}
# 查看mapping, mapping中没有出现isVIP
{
"mapping_test" : {
"mappings" : {
"dynamic" : "false",
"properties" : {
"uid" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
# 设置mapping为strict
PUT mapping_test/_mapping
{
"dynamic": "strict"
}
# 新增字段
POST mapping_test/_doc
{
"uid": "123.123",
"isVIP": true
}
# 结果报错
{
"error": {
"root_cause": [
{
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [isVIP] within [_doc] is not allowed"
}
],
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [isVIP] within [_doc] is not allowed"
},
"status": 400
}
显示设置Mapping
定义
PUT mapping_test
{
"mappings": {
"properties": {
"uid": {
"type": "text"
}
}
}
}
# 查看mapping
GET mapping_test/_mapping
{
"mapping_test" : {
"mappings" : {
"properties" : {
"uid" : {
"type" : "text"
}
}
}
}
}
建议
- 可以参考API手册,纯手写
PUT users
{
"mappings": {
"properties": {
"mobile": {
"type": "text",
"index": false # 控制字段不被索引,查询该字段是会报错
},
"username": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
根据mobile查询会报如下错误:
{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "failed to create query: {\n \"match\" : {\n \"mobile\" : {\n \"query\" : \"1111\",\n \"operator\" : \"OR\",\n \"prefix_length\" : 0,\n \"max_expansions\" : 50,\n \"fuzzy_transpositions\" : true,\n \"lenient\" : false,\n \"zero_terms_query\" : \"NONE\",\n \"auto_generate_synonyms_phrase_query\" : true,\n \"boost\" : 1.0\n }\n }\n}",
"index_uuid": "LHPDjKVbQDCqBf8QizDr2g",
"index": "users"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "users",
"node": "griHJ7c0SkiqV9bhOUQMRQ",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: {\n \"match\" : {\n \"mobile\" : {\n \"query\" : \"1111\",\n \"operator\" : \"OR\",\n \"prefix_length\" : 0,\n \"max_expansions\" : 50,\n \"fuzzy_transpositions\" : true,\n \"lenient\" : false,\n \"zero_terms_query\" : \"NONE\",\n \"auto_generate_synonyms_phrase_query\" : true,\n \"boost\" : 1.0\n }\n }\n}",
"index_uuid": "LHPDjKVbQDCqBf8QizDr2g",
"index": "users",
"caused_by": {
"type": "illegal_argument_exception", # 因为mobile字段不能被索引
"reason": "Cannot search on field [mobile] since it is not indexed."
}
}
}
]
},
"status": 400
}
index options
PUT users
{
"mappings": {
"properties": {
"age": {
"type": "integer",
"index_options": "freqs", # index options级别
}
}
}
}
- 四种不同级别的 Index Options配置。可以控制倒排索引记录的内容
- docs - 记录 doc id
- freqs - 记录 doc id 和 term frequencies
- positions - 记录 doc id , term frequencies , term position
- offsets - doc id, term frequencies , term position, character offects
- Text类型默认记录 positions, 其他默认为docs
-
null_value 处理
如果需要对Null值实现搜索
- 只有keyword类型支持设定 Null_Value
