目录
现有索引:testold
{
"testold": {
"aliases": {},
"mappings": {
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "keyword"
},
"cotent": {
"type": "keyword"
},
"tid": {
"type": "keyword"
}
}
},
"settings": {
"index": {
"creation_date": "1713842844859",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "YmaRFkrmR-SuD-V95VpmwQ",
"version": {
"created": "7090099"
},
"provided_name": "testold"
}
}
}
}
工具人索引:testnew
{
"mappings": {
"properties": {
"cotent": {
"type": "keyword"
},
"id": {
"type": "keyword"
},
"tid": {
"type": "long"
}
}
},
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
}
需要把testold的tid字段修改为long类型。
1、新建索引
请求方式:PUT
请求URL:
http://localhost:9200/testnew
{
"mappings": {
"properties": {
"cotent": {
"type": "keyword"
},
"id": {
"type": "keyword"
},
"tid": {
"type": "long"
}
}
},
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
}
2、数据迁移
请求方式:POST
请求URL:
http://localhost:9200/_reindex
请求体:
{
"source": {
"index": "testold"
},
"dest": {
"index": "testnew"
}
}
3、验证新索引
请求方式:GET
请求URL:
http://localhost:9200/testnew/_mapping
响应体:
{
"testnew": {
"aliases": { },
"mappings": {
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "keyword"
},
"cotent": {
"type": "keyword"
},
"tid": {
"type": "long"
}
}
},
"settings": {
"index": {
"creation_date": "1713842844859",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "YmaRFkrmR-SuD-V95VpmwQ",
"version": {
"created": "7090145"
},
"provided_name": "testnew"
}
}
}
}
4、删除旧索引
请求方式:DELETE
请求URL:
http://localhost:9200/testold
注意:删除旧索引后,需要按照新要求的字段类型进行新建
5、新建旧索引
使用旧的索引名进行新建,以上动作目的是保证数据不丢失。
请求方式:PUT
请求URL:
http://localhost:9200/testold
{
"mappings": {
"properties": {
"cotent": {
"type": "keyword"
},
"id": {
"type": "keyword"
},
"tid": {
"type": "long"
}
}
},
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
}
5、再次数据迁移
请求方式:POST
请求URL:
http://localhost:9200/_reindex
请求体:
{
"source": {
"index": "testnew"
},
"dest": {
"index": "testold"
}
}
6、再次验证
请求方式:GET
请求URL:
http://localhost:9200/testold/_mapping
数据验证:
http://localhost:9200/testold/_search
总结:在整个过程中,testnew只是祈祷一个中间过度的作用,确保在修改字段类型的过程中造成的数据丢失问题。