mapping就是index下type的元数据,每一个type都有一个自己的mapping,决定了数据类型,建立倒排索引的行为,以及搜索的行为
-
mapping数据类型
- 字符型:string
- 整数型:byte,short,integer,long
- 浮点型:float,double
- 布尔型:boolean
- 日期型:date
-
dynamic mapping(动态分配)原则
- true或false -->boolean
- 123-->long
- 123.45-->double
- 2017-01-01-->date
- "hello world"-->string/text
-
Field内部存储结构
- multivalue field,如:{"tags":["tage1","tage2","tage3"]}
- enpty field,如:null, [ ], [null]
- object field内部存储方式如下:
例1:
{
"names": {
"fname": "zeng",
"mname": "sam",
"fullname": "sam.zeng"
}
}内部会如下存储:
{
"names.fname": "zeng",
"names.mname": "sam",
"names.fullname": "sam.zeng"
}例2:
{
"info": [{
"name": "zeng",
"age": 16
}, {
"name": "sam",
"age": 20
}]
}内部会如下存储:
{
"info.name": ["zeng", "sam"],
"info.age": [16, 20]
}
-
查看mapping元数据语法:
GET index/type/_mapping
或者: GET index/_mapping/type
-
示例
GET accounts/person/_mapping
返回:
{
"accounts": {
"mappings": {
"person": {
"properties": {
"age": {
"type": "long"
},
"desc": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"salary": {
"type": "long"
},
"tag": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
},
"fielddata": true
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
},
"fielddata": true
},
"user": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
-
自定义mapping
PUT newindex
{
"mappings": {
"newtype":{
"properties": {
"name":{
"type": "text",
"analyzer": "english"
},
"age":{
"type": "integer"
},
"salary":{
"type": "double"
},
"sex":{
"type": "boolean"
},
"createDate":{
"type": "date"
},
"hobby":{
"type": "text",
"index": "not_analyzed",
"analyzer": "standard"
}
}
}
}
}
返回:
{
"acknowledged": true,
"shards_acknowledged": true
}
查看:
GET newindex/_mapping/newtype
返回:
{
"newindex": {
"mappings": {
"newtype": {
"properties": {
"age": {
"type": "integer"
},
"createDate": {
"type": "date"
},
"hobby": {
"type": "text",
"analyzer": "standard"
},
"name": {
"type": "text",
"analyzer": "english"
},
"salary": {
"type": "double"
},
"sex": {
"type": "boolean"
}
}
}
}
}
}
-
不可修改mapping元数据
报如下错误:
PUT newindex
{
"mappings": {
"newtype":{
"properties": {
"name":{
"type": "double"
}
}
}
}
}返回:
{
"error": {
"root_cause": [
{
"type": "index_already_exists_exception",
"reason": "index [newindex/_dBTOyVxRVeUiaUDjx_beg] already exists",
"index_uuid": "_dBTOyVxRVeUiaUDjx_beg",
"index": "newindex"
}
],
"type": "index_already_exists_exception",
"reason": "index [newindex/_dBTOyVxRVeUiaUDjx_beg] already exists",
"index_uuid": "_dBTOyVxRVeUiaUDjx_beg",
"index": "newindex"
},
"status": 400
}
-
增加mapping元数据
PUT newindex/_mapping/newtype
{
"properties": {
"addFiled2":{
"type": "text"
}
}
}
返回:
{
"acknowledged": true
}
查看:
GET newindex/_mapping/newtype
返回:
{
"newindex": {
"mappings": {
"newtype": {
"properties": {
"addFiled2": {
"type": "text"
},
"age": {
"type": "integer"
},
"createDate": {
"type": "date"
},
"hobby": {
"type": "text",
"analyzer": "standard"
},
"name": {
"type": "text",
"analyzer": "english"
},
"salary": {
"type": "double"
},
"sex": {
"type": "boolean"
}
}
}
}
}
}