四、映射
1、查看索引的映射
GET /bank/_mapping
创建索引是若没有指定字段的映射类型,则ES会根据存入的数据进行类型猜测,并给定一个猜测后的类型
如:数字类型的ES会默认给定long,字符类型的ES会默认给定text类型,用作分词全文搜索,并同时给keyword用于精确搜索
2、创建映射
若是类型不是我们所需要的,可以在创建索引是对类型进行指定:
官方类型文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html
PUT /my_index
{
"mappings": {
"properties": {
"age":{"type": "integer"},
"email":{"type": "keyword"},
"name":{"type": "text"}
}
}
}
3、添加新的字段映射
PUT /my_index/_mapping
{
"properties":{
"employee-id":{
"type":"keyword",
"index":true //用来控制这个字段是否能被索引,默认为true
}
}
}
4、更新映射
对于已经存在的映射字段,不能更新,更新必须创建新的索引并指定新的映射关系再进行数据迁移
5、数据迁移
-
创建新的索引并指定新的映射
PUT /newbank { "mappings": { "properties": { "account_number": { "type": "integer" }, "address": { "type": "text" }, "age": { "type": "integer" }, "balance": { "type": "long" }, "city": { "type": "text" }, "email": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "employer": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "firstname": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "gender": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "lastname": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "state": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } }
-
查看映射
GET /newbank/_mapping
-
旧的索引有类型,查看旧的索引类型
GET /bank/_search
-
迁移数据
POST _reindex //固定语法 { "source": { //原数据 "index": "bank", //原索引 "type": "account" //原索引的类型 }, "dest": { "index": "newbank" //目标索引 } } //注意:此处因为原索引有类型,所以需要指定原索引的类型 //在8.0之后没有类型,则不需要指定,直接省略type即可
-
此时数据已经迁移成功
GET /newbank/_search
此时的索引类型为默认的_doc
感谢b站-尚硅谷-雷丰阳老师
雷丰阳视频地址