(1)mapping 定义每个field的数据类型、索引行为、是否分词以及分词器等,就是index的type的元数据
string --> text/keyword
byte,short,integer,long --> 123 long
float,double --> 123.45 double
boolean --> true or false
date --> 2019-02-02
Text:会分词,然后进行索引,支持模糊、精确查询,不支持聚合
keyword:不进行分词,直接索引,支持模糊、精确查询,支持聚合
analyzed 分词建立索引
not_analyzed 不分词建立索引
no 不分词不建立索引
(2)建立mapping,结构化索引
PUT /person
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"person": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"birthday": {
"type": "date",
"format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"country": {
"type": "keyword"
},
"tag": {
"type": "text",
"index": "not_analyzed"
}
}
}
}
}
(3)查看mapping
GET /person/_mapping
(4)新增field
PUT /person
{
"mappings": {
"person": {
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}
--> 报错,不能修改已经定义好的field
PUT /person/_mapping/person
{
"properties": {
"nickname": {
"type": "keyword"
}
}
}
--> 成功
(5)测试分词
GET /person/_analyze
{
"field": "name",
"text": "kill tiger"
}
--> 结果分词
GET /person/_analyze
{
"field": "nickname",
"text": "kill tiger"
}
--> 结果不分词
(6)嵌套Object的mapping
PUT /company/employee/1
{
"address": {
"country": "china",
"province": "guangdong",
"city": "guangzhou"
},
"name": "jack",
"age": 27,
"join_date": "2017-01-01"
}
GET company/_mapping
{
"company": {
"mappings": {
"employee": {
"properties": {
"address": {
"properties": {
"city": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"country": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"province": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"age": {
"type": "long"
},
"join_date": {
"type": "date"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
实际上es内部会转成列式存储
{
"address": {
"country": "china",
"province": "guangdong",
"city": "guangzhou"
},
"name": "jack",
"age": 27,
"join_date": "2017-01-01"
}
--> 这样储存
{
"name": [jack],
"age": [27],
"join_date": [2017-01-01],
"address.country": [china],
"address.province": [guangdong],
"address.city": [guangzhou]
"province": "guangdong",
}
{
"authors": [
{ "age": 26, "name": "Jack White"},
{ "age": 55, "name": "Tom Jones"},
{ "age": 39, "name": "Kitty Smith"}
]
}
--> 这样储存
{
"authors.age": [26, 55, 39],
"authors.name": [jack, white, tom, jones, kitty, smith]
}