Elasticsearch篇之Mapping设置
Mapping简介
类似数据库中的表结构定义,主要作用如下:
- 定义Index下的字段名(Field Name)
- 定义字段的类型,比如数值型、字符串型、布尔型等
- 定义倒排索引相关的配置,比如是否索引、记录position等
GET books/_mapping
展示效果
自定义Mapping
PUT my_index
{
"settings":
{
"number_of_shards": "5",
"number_of_replicas": "0"
},
"mappings": {
"doc": {
"properties": {
"title":{
"type": "text"
},
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
}
}
}
}
}
- Mapping中的字段类型一旦设定后,禁止直接修改,原因如下
- Lucene实现的倒排索引生成后不允许修改
- 重新建立新的索引,然后做reindex操作
- 允许新增字段
- 通过dynamic参数来控制字段的新增
- true(默认)允许自动新增字段
- false不允许自动新增字段,但是文档可以正常写入,但无法对字段进行查询等操作
- strict文档不能写入,报错
PUT my_index
{
"settings":
{
"number_of_shards": "5",
"number_of_replicas": "0"
},
"mappings": {
"doc": {
"dynamic": false,
"properties": {
"title":{
"type": "text"
},
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
}
}
}
}
}
GET my_index/_mapping
PUT my_index/doc/1
{
"title": "hello word",
"age": 21,
"name": "wf"
}
PUT my_index/doc/2
{
"title": "hello word",
"age": 21,
"name": "ls",
"address": "china"
}
直接使用elasticsearch-head插件进行数据的查看
进行查询对比
“dynamic”: "strict"的时候在插入文档时会出现如下
copy_to参数说明
- 将该字段的值复制到目标字段,实现类似_all的作用
- 不会出现在_source中,只用来搜索
PUT my_index
{
"settings":
{
"number_of_shards": "5",
"number_of_replicas": "0"
},
"mappings": {
"doc": {
"properties": {
"first_name":{
"type": "text",
"copy_to": "full_name"
},
"last_name": {
"type": "text",
"copy_to": "full_name"
},
"full_name": {
"type": "text"
}
}
}
}
}
增加一条数据
PUT my_index/doc/1
{
"first_name": "张",
"last_name": "三"
}
查询数据(full_name为张三,必须同时满足)
GET my_index/_search
{
"query": {
"match": {
"full_name": {
"query": "张三",
"operator": "and"
}
}
}
}
Index参数说明
PUT my_index1
{
"settings":
{
"number_of_shards": "5",
"number_of_replicas": "0"
},
"mappings": {
"doc": {
"properties": {
"cookie":{
"type": "text",
"index": false
}
}
}
}
}
PUT my_index1/doc/1
{
"cookie": "cookie-name-test"
}
GET my_index1/_search
{
"query": {
"match": {
"cookie": "name"
}
}
}
查询结果
身份证号 手机号 密码 可以设置为不检索
index_option
设置和之前index类似,在字段下面有个index_option
- index_option用于控制倒排索引记录的内容,有如下4种配置
- docs只记录doc id
- freqs 记录doc id和term frequencies
- positions 记录doc id、term frequencies和term position
- offsets 记录doc id、term frequencies、term position和character offsets
- text类型默认配置positions,其他默认为docs
- 记录内容越多,占用空间越大
null_value
设置和之前index类似,在字段下面有个null_value
- 当字段遇到null值时的处理策略,默认为null,即空值,此时es会忽略改值。可以通过设定改值设定字段的默认值
Mapping官方文档
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html
数据类型
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html
- 核心数据类型
- 字符串型text, keyword
- 数值型long, integer, short, byte, double, float, halffloat, scaled_float
- 日期类型date
- 布尔类型boolean
- 二进制类型binary
- 范围类型integer_range, float_range, long_range, double_range, date_range
- 复杂数据类型
- 数组类型array
- 对象类型object
- 嵌套类型nested object
- 地理位置数据类型
- geo_point
- geo_shape
- 专用类型
- 记录ip地址ip
- 实现自动补全completion
- 记录分词数token_count
- 记录字符串hash值murmur3
- percolator
- join
- 多字段特性multi-fields
- 允许对同一个字段采用不同的配置,比如分词,常见例子如对人名实现拼音搜索,只需要在人名中新增一个子字段为pinyin即可。
- 允许对同一个字段采用不同的配置,比如分词,常见例子如对人名实现拼音搜索,只需要在人名中新增一个子字段为pinyin即可。
Dynamic Mapping简介
es可以自动识别文档字段类型,从而降低用户使用成本,如下所示
PUT my_index2/doc/1
{
"username": "zs",
"age": 1
}
GET my_index2/_mapping
es是依靠JSON文档的字段类型来实现自动识别字段类型,支持的类型如下:
PUT my_index2/doc/1
{
"username": "zs",
"age": 14,
"birth": "1949-10-01",
"married": false,
"year": "2020",
"tags": ["boy","fashion"],
"money": 999.999
}
GET my_index2/_mapping
dynamic日期与数字识别
日期
日期的自动识别可以自行配置日期格式,以满足各种需求
默认是:["strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z|lyyyy/MM/dd Z"]
strict_date_optional_time
是ISO datetime的格式,完整格式类似:YYYY-MM-DDThh:mm:ssTZD (eg 2020-01-11T14:30:30+01:00)
dynamic_date_formats
可以自定义日期类型
date_detection
可以关闭日期自动识别的机制
这两个参数直接在type下面做
自定日期识别格式
PUT my_index2
{
"settings":
{
"number_of_shards": "5",
"number_of_replicas": "0"
},
"mappings": {
"doc": {
"dynamic_date_formats": ["MM/dd/yyyy"]
}
}
}
PUT my_index2/doc/1
{
"create_date": "11/01/2020"
}
GET my_index2/_mapping
关闭日期自动识别机制
PUT my_index2
{
"settings":
{
"number_of_shards": "5",
"number_of_replicas": "0"
},
"mappings": {
"doc": {
"date_detection": false
}
}
}
PUT my_index2/doc/1
{
"create_date": "11/01/2020"
}
GET my_index2/_mapping
数字
字符串是数字是,默认不会自动识别问整型,因为字符串中出现数字是完全合理的
numeric_detection
可以开启字符串中数字的自动识别
PUT my_index
{
"settings":
{
"number_of_shards": "5",
"number_of_replicas": "0"
},
"mappings": {
"doc": {
"numeric_detection": true
}
}
}
PUT my_index/doc/1
{
"my_float": "99.99",
"my_int": "100"
}
GET my_index/_mapping
dynamic-template简介
允许根据es自动识别的数据类型、字段名、等动态设定字段类型,可以实现如下效果:
-
所有字符串类型都设定为keyword,即默认不分词
-
所有以message_开头的字段都设定为text类型,即分词
-
所有以long_开头的字段都设定为long类型
-
所有自动匹配的double类型都设定为float类型,以节省空间
匹配规则一般有如下几个参数: -
match_mapping_type匹配es自动识别的字段类型,如boolean、long、string等
-
match、unmatch匹配字段名
-
path_match、path_unmatch匹配路径
字符串默认使用keyword类型
以message_开头的字段都设置为text类型
double类型设定为float,节省空间
PUT my_index/doc/1
{
"name": "张三 ",
"message_sex": "男",
"message_address": "中国北京",
"gpa": 9999.123456
}
GET my_index/_mapping
PUT my_index
{
"settings":
{
"number_of_shards": "5",
"number_of_replicas": "0"
},
"mappings": {
"doc": {
"dynamic_templates": [
{
"message_as_text": {
"match_mapping_type": "string",
"match": "message_*",
"mapping": {
"type": "text"
}
}
},
{
"string_as_keywords": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
},
{
"double_as_float": {
"match_mapping_type": "double",
"mapping": {
"type": "float"
}
}
}
]
}
}
}
PUT my_index/doc/1
{
"name": "张三 ",
"message_sex": "男",
"message_address": "中国北京",
"gpa": 9999.123456
}
GET my_index/_mapping
自定义mapping的建议
自定义Mapping的操作步骤如下:
- 写入一条文档到es的临时索引中,获取es自动生成的mapping
- 修改步骤1得到的mapping,自定义相关配置
- 使用步骤2的mapping创建实际所需索引
DELETE my_index
PUT my_index/doc/1
{
"referrer": "-",
"response_code": "200",
"remote_ip": "127.0.0.1",
"method": "POST",
"user_name": "-",
"http_version": "1.1",
"body_sent": {
"bytes": "0"
},
"url": "/test"
}
GET my_index/_mapping
PUT my_index
{
"settings": {
"number_of_shards": "5",
"number_of_replicas": "0"
},
"mappings": {
"doc": {
"properties": {
"body_sent": {
"properties": {
"bytes": {
"type": "long"
}
}
},
"http_version": {
"type": "keyword"
},
"method": {
"type": "keyword"
},
"referrer": {
"type": "keyword"
},
"remote_ip": {
"type": "keyword"
},
"response_code": {
"type": "keyword"
},
"url": {
"type": "text"
},
"user_name": {
"type": "keyword"
}
}
}
}
}
可以使用dynamic-template设置text为keyword然后其他字段再单独设置
索引模板
索引模板,英文为Index Template,主要用于在新建索引时自动应用预先设定的配置,简化索引创建的操作步骤
- 可以设定索引的配置和mapping
- 可以有多个模板,根据order设置,order大的覆盖小的配置
获取与删除的API如下
- GET _template
- GET _template/test_template
- DELETE _template/test_template
GET _template
PUT _template/test_template
{
"index_patterns": ["te*", "bar*"],
"order": 0,
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
},
"mappings": {
"doc": {
"_source": {
"enabled": false
},
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}
PUT _template/test_template2
{
"index_patterns": ["test*"],
"order": 1,
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"doc": {
"_source": {
"enabled": true
},
"properties": {
"name": {
"type": "text"
}
}
}
}
}
PUT foo_index
GET foo_index
PUT bar_index
GET bar_index
PUT test_index
GET test_index