字段的数据类型
- 简单类型
- Text / Keyword
- Date
- Integer / Floating
- Boolean
- IPv4 & IPv6
 
- 复杂类型 - 对象和嵌套对象
- 特殊类型
- geo_point & geo_shape / percolator
 
Dynamic Mapping
- 写入文档的时候,如果索引不存在,会自动创建索引
- Dynamic Mapping 的机制,使得我们无需手动定义 Mappings。Elasticsearch 会自动根据文档信息,推算出字段的类型
- 但是会有时候推算不对。例如地理位置信息
- 当类型如果设置不对时,会导致一些功能无法正常运行,例如 Range 查询
| JSON 类型 | Elasticsearch 类型 | 
| 字符串 | 1 匹配日期格式设置成 Date | 
|  | 2 设置数字设置为 float 或者 long,该选项默认关闭 | 
|  | 3 设置为 Text, 并增加 keyword 子字段 | 
| 布尔值 | boolean | 
| 浮点数 | float | 
| 整数 | long | 
| 对象 | Object | 
| 数组 | 由第一个非空数值的类型所决定 | 
| 空值 | 忽略 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 
 | //写入文档PUT mapping_test/_doc/1
 {
 "firstName":"Lee",
 "lastName":"Crazy",
 "loginDate":"2019-10-22T21:08:48"
 }
 //查看Mapping 文件
 GET mapping_test/_mapping
 {
 "mapping_test" : {
 "mappings" : {
 "properties" : {
 "firstName" : {
 "type" : "text",
 "fields" : {
 "keyword" : {
 "type" : "keyword",
 "ignore_above" : 256
 }
 }
 },
 "lastName" : {
 "type" : "text",
 "fields" : {
 "keyword" : {
 "type" : "keyword",
 "ignore_above" : 256
 }
 }
 },
 "loginDate" : {
 "type" : "date"
 }
 }
 }
 }
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 
 | //dynamic mapping 推断字符的类型PUT mapping_test/_doc/1
 {
 "uid":"123",
 "isVip": false,
 "isAdmin":"true",
 "age": 18,
 "heigh" : 180
 }
 //返回结果
 {
 "mapping_test" : {
 "mappings" : {
 "properties" : {
 "age" : {
 "type" : "long"
 },
 "heigh" : {
 "type" : "long"
 },
 "isAdmin" : {
 "type" : "text",
 "fields" : {
 "keyword" : {
 "type" : "keyword",
 "ignore_above" : 256
 }
 }
 },
 "isVip" : {
 "type" : "boolean"
 },
 "uid" : {
 "type" : "text",
 "fields" : {
 "keyword" : {
 "type" : "keyword",
 "ignore_above" : 256
 }
 }
 }
 }
 }
 }
 }
 
 | 
显示 Mapping 设置与常见参数
控制当前字段是否被索引
- index - 控制当前字段是否被索引。默认为 true。如果设置成 false,该字段不可被搜索。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | PUT users{
 "mappings" : {
 "properties" : {
 "firstName" : {
 "type" : "text"
 },
 "lastName" : {
 "type" : "text"
 },
 "mobile" : {
 "type" : "text",
 "index": false
 }
 }
 }
 }
 
 | 
null_value
- 需要对 NULL 值实现搜索
- 只有 Keyword 类型支持设定 Null_Value
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 
 | DELETE usersPUT users
 {
 "mappings" : {
 "properties" : {
 "firstName" : {
 "type" : "text"
 },
 "lastName" : {
 "type" : "text"
 },
 "mobile" : {
 "type" : "keyword", //这个如果是text 无法设置为空
 "null_value": "NULL"
 }
 }
 }
 }
 PUT users/_doc/2
 {
 "firstName":"Li",
 "lastName": "Sunke",
 "mobile": null
 }
 GET users/_search?q=mobile:NULL
 //搜索结果
 "_source" : {
 "firstName" : "Li",
 "lastName" : "Sunke",
 "mobile" : null
 }
 
 | 
copy_to
- _all 在 7 中已经被 copy_to 所替代
- 满足一些特定的搜索需求
- copy_to 将字段的数值拷贝到目标字段,实现类似 _all 的作用
- copy_to 的目标字段不出现在_source 中
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 
 | DELETE usersPUT users
 {
 "mappings": {
 "properties": {
 "firstName":{
 "type": "text",
 "copy_to": "fullName"
 },
 "lastName":{
 "type": "text",
 "copy_to": "fullName"
 }
 }
 }
 }
 PUT users/_doc/1
 {
 "firstName":"Li",
 "lastName": "Sunke"
 }
 //没有新建字段
 GET users/_doc/1
 {
 "_index" : "users",
 "_type" : "_doc",
 "_id" : "1",
 "_version" : 1,
 "_seq_no" : 0,
 "_primary_term" : 1,
 "found" : true,
 "_source" : {
 "firstName" : "Li",
 "lastName" : "Sunke"
 }
 }
 GET users/_search?q=fullName:(Li sunke)
 
 | 
专题目录
ElasticStack-安装篇
ElasticStack-elasticsearch篇
ElasticStack-logstash篇
elasticSearch-mapping相关
elasticSearch-分词器介绍
elasticSearch-分词器实践笔记
elasticSearch-同义词分词器自定义实践
docker-elk集群实践
filebeat与logstash实践
filebeat之pipeline实践
Elasticsearch 7.x 白金级 破解实践
elk的告警调研与实践