1.为什么需要重建索引
举个例子,如果一个字段是text类型,如果想修改为Long类型,是不能直接修改的。
在重建的过程中,需要有别名的参与。
2.操作步骤
对当前的索引新建一个别名
新建一个新的索引,同步结构
同步数据
给新的索引见一个别名
删除老的索引的别名
删除老的索引
二:操作步骤详解
1.对当前的索引添加别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba",
"alias": "nba_lastest"
}
}
]
}
2.新增⼀个索引(⽐如修改字段类型,jerseyNo改成keyword类型)
PUT /nba_20200202
{
"mappings": {
"properties": {
"age" : {
"type" : "integer"
},
"birthDay" : {
"type" : "date"
},
"birthDayStr" : {
"type" : "keyword"
},
"code" : {
"type" : "text"
},
"country" : {
"type" : "text"
},
"countryEn" : {
"type" : "text"
},
"displayAffiliation" : {
"type" : "text"
},
"displayName" : {
"type" : "text"
},
"displayNameEn" : {
"type" : "text"
},
"draft" : {
"type" : "long"
},
"heightValue" : {
"type" : "float"
},
"jerseyNo" : {
"type" : "keyword"
},
"playYear" : {
"type" : "long"
},
"playerId" : {
"type" : "keyword"
},
"position" : {
"type" : "text"
},
"schoolType" : {
"type" : "text"
},
"teamCity" : {
"type" : "text"
},
"teamCityEn" : {
"type" : "text"
},
"teamConference" : {
"type" : "keyword"
},
"teamConferenceEn" : {
"type" : "keyword"
},
"teamName" : {
"type" : "keyword"
},
"teamNameEn" : {
"type" : "keyword"
},
"weight" : {
"type" : "text"
}
}
}
}
3.给新的索引,同步数据
添加的参数,是异步执行的意思
POST /_reindex?wait_for_completion=false
{
"source": {
"index": "nba"
},
"dest": {
"index": "nba_20200202"
}
}
效果:
{
"task" : "O9L9cZRfQwWqC2UVUaxMaQ:18594274"
}
4.替换别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "nba_20200202",
"alias": "nba_lastest"
}
},
{
"remove": {
"index": "nba",
"alias": "nba_lastest"
}
}
]
}
5.删除旧的索引
其实,这里不删除也行,但是会占用一些资源
DELETE /nba
6.进行验证
GET /nba_lastest/_search
{
"query": {
"match_all": {}
}
}