需要重建索引的几种情况:
-
索引字段类型错误,名称被占用
-
单个分片数据量大查询性能下降,增加分片数
-
单个分片容量超过50G,增加分片数
-
需修改索引类型type名称
1.先创建个movies索引,然后插入测试数据
PUT /movies
PUT /movies/_bulk
{ "index": {"_id": 1}}
{"title":"The Godfather","director":"Francis Ford Coppola","year":1972,"genres":["Crime","Drama"]}
{ "index": {"_id": 2}}
{"title":"Lawrence of Arabia","director":"David Lean","year":1962,"genres":["Adventure","Biography","Drama"]}
{ "index": {"_id": 3}}
{"title":"To Kill a Mockingbird","director":"Robert Mulligan","year":1962,"genres":["Crime","Drama","Mystery"]}
2.为movies索引添加一个别名movies_aliase
PUT /movies/_aliases
{
"actions": [
{ "add": {
"alias": "movies_aliase",
"index": "movies"
}}
]
}
此时,可以用别名movies_aliase查询到movies中的所有数据,也可以执行和movies其他的相同操作:
到这里都是准备工作,下面开始模拟真正的重建索引的操作。
3.新的需求变了,需要重建索引了,先创建一个movies_v2的索引
PUT /movies_v2/
{
"mappings": {
"properties": {
"title": {
"type": "text",
"store": true,
"index": true,
"analyzer": "standard"
},
"director":{
"type": "text",
"store": true,
"index": true,
"analyzer": "standard"
},
"year": {
"type": "long",
"store": true,
"index": true
}
}
}
}
此时movies_v2里面的数据还是空的,需要先把movies里面原来保存的数据拷贝到movies_v2里面来
4.迁移数据
执行_reindex操作(同步操作):
POST /_reindex
{"source":{"index":"movies","size": 10000},"dest":{"index":"movies_v2"}}
如果 reindex 时间过⻓,建议加上 wait_for_completion=false 的参数条件,让其执行异步操作,这样 reindex 将直接返回 taskId
POST /_reindex?wait_for_completion=false
{"source":{"index":"movies","size": 10000},"dest":{"index":"movies_v2"}}
5.别名切换
将原先指向movies的别名movies_aliase删除,添加指向movies_v2的别名movies_aliase:
PUT /movies/_aliases
{
"actions": [
{ "remove": {
"alias": "movies_aliase",
"index": "movies"
}},
{ "add": {
"alias": "movies_aliase",
"index": "movies_v2"
}}
]
}
此时通过别名可以访问到movies和movies_v2的两份数据,所以需要删除原先的旧的索引。
6.删除原先的索引
DELETE movies
这样就可以通过movies_aliase访问新的索引的数据了。