前情提要
本人小白一枚,马上要负责一个项目的开发,由于需要涉及到一些ES的使用,提前思考一下可能会碰到的一些问题形成一个记录,以便后续
查看。
本次记录使用ES版本为8.4.3
问题一、Mapping映射的修改问题
众所周知,ES提供自动映射(在创建索引后根据数据自动创建Mapping映射),但实际开发中,为了更加贴合业务需求、运行维护等,对于
Mapping映射采取的是自己定义。这种情况下,业务需求的变动可能会影响Mapping映射结构,为保证我们修改Mapping映射不影响
原数据,这时我们就得考虑Mapping映射的修改问题。
1. Mapping映射新增字段
2. Mapping映射删除字段
3. Mapping映射修改字段
准备
# 创建索引old_student
PUT /old_student
{
"mappings": {
"properties": {
"stuName": {
"type": "keyword"
},
"stuSex": {
"type": "short"
},
"stuMail": {
"type": "keyword"
},
"stuAge": {
"type": "integer"
}
}
},
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}
# 查看索引old_student
GET /old_student
# 添加数据
POST /old_student/_doc
{
"stuName": "王五",
"stuSex": 1,
"stuMail": "2583651948@qq.com",
"stuAge": 18,
"stuPhone": 12345678910
}
此处省略多条数据...
注意: 此处会出现自动映射(因为我们在创建Mapping映射时未禁用自动映射加之我们数据中出现映射不存在的字段"stuPhone")
# 如果不希望数据影响我们的Mapping映射结构,可以设置自动映射为false
PUT /old_student/_mapping
{
"dynamic": false
}
# 或者使用严格模式
PUT /old_student/_mapping
{
"dynamic": "strict"
}
二者区别: 设置自动映射为false的话,上面所演示数据依然可以添加成功且不会改变我们的Mapping映射结构。使用严格模式的话,上述数据添加失败,且后续我们修改Mapping映射报错
mapping set to strict, dynamic introduction of [properties] within [_doc] is not allowed
1. 新增字段
# 新增字段比较简单且不会对原有数据造成影响
PUT /old_student/_mapping
{
"properties": {
"stuClass": {
"type": "keyword"
}
}
}
2. 删除字段或修改字段
# 删除字段stuAge、修改字段stuSex类型为keyword、修改字段stuClass类型为text,添加ik分词器
# 重建一个索引new_student
PUT /new_student
{
"mappings": {
"dynamic": false,
"properties": {
"stuName": {
"type": "keyword"
},
"stuMail": {
"type": "keyword"
},
"stuSex": {
"type": "keyword"
},
"stuClass": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
# 为old_student设置别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "old_student",
"alias": "student"
}
}
]
}
# 将old_student数据迁移至new_student
POST /_reindex
{
"source": {
"index": "old_student"
},
"dest": {
"index": "new_student"
}
}
# 移除old_student别名并为new_student设置相同别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "new_student",
"alias": "student"
}
},
{
"remove": {
"index": "old_student",
"alias": "student"
}
}
]
}
# 删除old_student
DELETE /old_student
# 新索引new_student添加数据测试
POST /new_student/_doc
{
"stuName": "阿柒",
"stuSex": 1,
"stuMail": "3484332313@qq.com",
"stuAge": 7777777777777777,
"stuPhone": 12345678910,
"stuClass": "二般"
}
# 查询数据查看结果
GET /new_student/_search
{
"query": {
"match_all": {}
}
}
# 结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "new_student",
"_id": "lexcNocBl-_Ye7BZU-Dy",
"_score": 1,
"_source": {
"stuName": "王五",
"stuSex": 1,
"stuMail": "2583651948@qq.com",
"stuAge": 18,
"stuPhone": 15838451526
}
},
{
"_index": "new_student",
"_id": "k-xYNocBl-_Ye7BZmODi",
"_score": 1,
"_source": {
"stuName": "张三",
"stuSex": 1,
"stuMail": "2583651948@qq.com",
"stuAge": 18
}
},
{
"_index": "new_student",
"_id": "luyVNocBl-_Ye7BZ8eCt",
"_score": 1,
"_source": {
"stuName": "老六",
"stuSex": 1,
"stuMail": "353332313@qq.com",
"stuAge": 66,
"stuPhone": 12345678910,
"stuClass": "不一般"
}
},
{
"_index": "new_student",
"_id": "lOxZNocBl-_Ye7BZIuDw",
"_score": 1,
"_source": {
"stuName": "李四",
"stuSex": 0,
"stuMail": "2583651948@qq.com",
"stuAge": 18
}
},
{
"_index": "new_student",
"_id": "l-yuNocBl-_Ye7BZ9uDF",
"_score": 1,
"_source": {
"stuName": "阿柒",
"stuSex": 1,
"stuMail": "3484332313@qq.com",
"stuAge": 7777777777777777,
"stuPhone": 12345678910,
"stuClass": "二般"
}
}
]
}
}
总结: Mapping映射修改成功且不影响原数据
问题二、…
此时,貂蝉还在骑马来的路上…