1. mapping
类似于数据库的schema的定义,mapping会把文档映射成lucene需要的扁平格式,一个mapping属于一个索引的type,一个type中有一个mapping定义,7.0后一个索引只有一个type,所以不需要在mapping中定义type的信息。作用如下:
- 定义索引这里面的字段和名称
- 定义字段的数据类型,字符串、布尔、数字......
- 字段,倒排索引相关的配置,是否分词。
1.1 字段的类型
- 简单类型
text/keyword,Date,Integer/Floating,Boolean,IPv4 &IPv6 - 复杂类型
对象类型/嵌套类型 - 特殊类型
geo_point & geo_shape/percolator(地理信息) - 数组类型
ES不提供专门的数组类型,任何字段都可以包含多个相同类型的数值
2.Dynamic Mapping
写入文档的时候,索引不存在,会自动创建索引, 无需手动创建,ES会根据内容推断字段的类型,推断会不准确,可能造成某些功能无法使用,例如 范围查询。
查看一个索引当前的mapping
GET /movies/_mapping
{
"movies" : {
"mappings" : {
"properties" : {
"@version" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"genre" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"year" : {
"type" : "long"
}
}
}
}
}
类型的自动识别关系:
JSON类型 | Elasticsearch |
---|---|
字符串 | 匹配日期格式,设置为date;匹配数字,设置为float或者long,功能默认关闭;设置为text,并增加keyword子字段。 |
布尔值 | boolean |
浮点数 | float |
整数 | long |
对象 | object |
数组 | 由第一个非空数值的类型决定 |
空值 | 忽略 |
测试
PUT mapping_test/_doc/1
{
"uid":1,
"is_vip":false,
"user_name":"wang",
"info":{
"address":"地址信息",
"card":"picture.png"
}
}
获取查看信息 GET /mapping_test/_mapping
{
"mapping_test" : {
"mappings" : {
"properties" : {
"info" : {
"properties" : {
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"card" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"is_vip" : {
"type" : "boolean"
},
"uid" : {
"type" : "long"
},
"user_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
3.修改Mapping的字段类型
- 新增字段
- dynamic设置为true,一旦有新增字段的文档写入,mapping也同时被更新。
- dynamic设置为false,mapping不会被更新,新增的字段数据无法被索引,但是信息会出现在source中.
- dynamic设置为strict,文档写入失败
- 已有的字段,一旦有数据写入,不支持修改(倒排索引不支持修改)
- 希望更改字段类型,用
Reindex API,重建索引
设计原因 - 如果修改字段数据类型,会导致已经被索引的文档不能被搜索。
- 新增字段不存在影响。
POST mapping_test2/_doc
{
"field":"one"
}
GET mapping_test2/_search
{
"query": {
"match": {
"field": "one"
}
}
}
PUT mapping_test2/_mapping
{
"dynamic":"false"
}
POST mapping_test2/_doc
{
"field_2":"two"
}
//
GET mapping_test2/_search
{
"query": {
"match": {
"field_2": "two"
}
}
}
//但是可以查出来
GET mapping_test2/_search
POST mapping_test2/_doc
{
"field_2":"three",
"field":"3"
}
//但是根据field字段可以查出来
GET mapping_test2/_search
{
"query": {
"match": {
"field": "3"
}
}
}
4. 显示的设置mapping信息
显示的设置mapping可以更灵活控制ES。
根据API手册,手写,减少出错概率的方法。
- 创建一个临时的index,写入一些样本数据
- 通过mapping api获得该临时文件的动态mapping定义
- 修改后用,使用该配置创建索引
- 删除临时索引
4.1 控制字段是否被index
index的选项配置
- docs:记录doc id
- freqs:记录doc id和term的频次
- position: 记录doc id、term频次、term位置
- offsets:doc id、term频次、term位置、字符串的偏移量
text类型默认是positions,其他的默认为docs,记录内容越多,占据空间越大
例如创建mapping,字段名为user_name,字符串类型。不需要索引,info字段的倒排索引类型为positions。
PUT mapping_test3
{
"mappings": {
"properties": {
"user_name":{
"index": false,
"type": "text"
},
"info":{
"index_options": "positions",
"type": "text"
}
}
}
}
4.2 null_value
对Null值进行搜索,可以通过keyword类型的字段设置null_value。
"mobile":{
"type":"keyword",
"null_value":"NULL"
}
就可以对NULL搜索:GET users/_search?q=mobile:NULL
4.3 copy_to
- 用来满足一些搜索需要,类似于数据库
title like "%a%" or title2 like "%a%"
- copy_to的字段不会出现在_source里面
PUT users
{
"mappings": {
"properties": {
"first_name":{
"type": "text",
"copy_to": "full_name"
},
"last_name":{
"type": "text",
"copy_to": "full_name"
}
}
}
}
就可以在支持 :GET users/_search?q=full_name:(zhang san)
搜索。