ElasticSearch34:初识搜索引擎_mapping复杂数据类型以及object类型数据底层结构揭秘
1.mapping复杂数据类型
1)multivalue field,字段可以接收多个值
{"tags":["tag1","tag2"]}
建立索引时与string是一样的,数据类型不能混。
2)empty field
null, [], [null]
3)object field
如下,其中的author就是object field
{
"_index": "company",
"_type": "employee",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
而ES的底层存储格式其实是下面这样的,上面的数据对应的底层数据结构
1.mapping复杂数据类型
1)multivalue field,字段可以接收多个值
{"tags":["tag1","tag2"]}
建立索引时与string是一样的,数据类型不能混。
2)empty field
null, [], [null]
3)object field
如下,其中的author就是object field
{
"title":"my first title",
"author":{
"author_id":"1",
"gender":"male",
"age":27,
"names":{
"full":"John_Jack",
"first":"John",
"last":"Jack"
}
}
}
例子:
PUT /company/employee/1
{
"address":{
"country":"China",
"province":"Zhejiang",
"city":"Hangzhou"
},
"name":"jack",
"age":27,
"join_date":"2018-01-04"
}
执行结果:
{
"_index": "company",
"_type": "employee",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
查看company/employee的mapping
GET /company/_mapping/employee
执行结果:es动态建立的mapping,可以看到address 的数据类型,里面包含了properties:city,country,province,这个address就是object类型
{
"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的底层存储格式其实是下面这样的,上面的数据对应的底层数据结构
{
"name":[jack],
"age":[27],
"join_date":[2018-01-04],
"address.province":[Zhejiang],
"address.country":[China],
"address.city":[Hangzhou]
}
而更复杂一点的数据类型,如
{
"author":[
{"name":"jack1 white","age":27},
{"name":"jack2 blue","age":28},
{"name":"jack3 yellow","age":29}
]
}
对应的底层数据结构
{
"author.name":[jack1,white,jack2,blue,jack3,yellow],
"author.age":[27,28,29]
}