当ES在文档中探测到新的字符串字段将默认设置为全文string字段并使用标准分析器(standard)分析。为了使用更适合的分析器或者不做任何处理,则需要使用映射(mapping)。
之前提到每个文档都有一个类型(type),每个类型都有一个映射(mapping)或者模式定义(schema definition)一个映射定义了字段类型,每个字段的数据类型,以及字段被Elasticsearch处理的方式。映射还用于设置关联到类型上的元数据。
 查看映射
GET /{index}/_mapping/{type}
例如:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 | GET /gb/_mapping/tweet
 {
 "gb": {
 "mappings": {
 "tweet": {
 "properties": {
 "date": {
 "type": "date",
 "format": "strict_date_optional_time||epoch_millis"
 },
 "name": {
 "type": "string"
 },
 "tweet": {
 "type": "string"
 },
 "user_id": {
 "type": "long"
 }
 }
 }
 }
 }
 }
 
 | 
 自定义字段映射
自定义字段映射可以:
- 区分全文(full text)字符串字段和准确字符串字段。
- 使用特定语言的分析器
- 优化部分匹配字段
- 指定自定义日期格式
- …
| 12
 3
 4
 5
 6
 
 | {"tag": {
 "type": "string",
 "index": "not_analyzed"
 }
 }
 
 | 
 更新映射
可以在第一次创建索引的时候指定映射的类型。也可以晚些时候为新类型添加映射(或者为已有的类型更新映射)。
可以向已有映射中增加字段,但不能修改它。如果一个字段在映射中已经存在,这可能意味着那个字段的数据已经被索引。如果你改变了字段映射,那已经被索引的数据将错误并且不能被正确的搜索到。
 复合对象的索引结构
对于复杂对象的mapping :
| 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
 
 | {"gb": {
 "tweet": {
 "properties": {
 "tweet": {
 "type": "string"
 },
 "user": {
 "type": "object",
 "properties": {
 "id": {
 "type": "string"
 },
 "gender": {
 "type": "string"
 },
 "age": {
 "type": "long"
 },
 "name": {
 "type": "object",
 "properties": {
 "full": {
 "type": "string"
 },
 "first": {
 "type": "string"
 },
 "last": {
 "type": "string"
 }
 }
 }
 }
 }
 }
 }
 }
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 
 | {"tweet": "Elasticsearch is very flexible",
 "user": {
 "id": "@johnsmith",
 "gender": "male",
 "age": 26,
 "name": {
 "full": "John Smith",
 "first": "John",
 "last": "Smith"
 }
 }
 }
 
 # 其索引方式为平铺的单层结构:
 {
 "tweet": [elasticsearch, flexible, very],
 "user.id": [@johnsmith],
 "user.gender": [male],
 "user.age": [26],
 "user.name.full": [john, smith],
 "user.name.first": [john],
 "user.name.last": [smith]
 }
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | {"followers": [
 { "age": 35, "name": "Mary White"},
 { "age": 26, "name": "Alex Jones"},
 { "age": 19, "name": "Lisa Smith"}
 ]
 }
 
 # 其索引方式为平铺的单层结构:
 {
 "followers.age": [19, 26, 35],
 "followers.name": [alex, jones, lisa, smith, mary, white]
 }
 
 |