前言
记录当生产机器上的es索引需要增加字段,修改索引类型,并且要求不重启es集群时,可以使用别名进行索引重建。
过程
首先假设我们已经有一个索引名为student_v1,我们得先给他创建一个别名,操作如下:
curl -XPUT "http://localhost:9200/student_v1 " -d'
{
"mappings": {
"man": {
"properties": {
"age": {
"type": "long"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}'
设置别名,对外提供操作都需要通过别名对索引进行增删改查。
如下:设置student_v1的别名为student
curl -XPUT "http://localhost:9200/student_v1/_aliases/student"
模拟写入一些数据
curl -XPUT "http://localhost:9200/student/man/1" -d'
{
"age":1
,"name":"1"
}'
创建最新版本的索引,增加需要的字段,模拟增加了一个class字段
curl -XPUT "http://152.136.87.179:9200/student_v2 " -d'
{
"mappings": {
"man": {
"properties": {
"class":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
},
"age": {
"type": "long"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}'
同步一次v1,v2两个索引的数据
curl -XPOST "http://localhost:9200/_reindex" -d'
{
"conflicts": "proceed",
"source":{
"index":"student_v1"
},"dest":{
"index":"student_v2"
}
}'
修改索引v2的别名为student,同时移除v1的别名
curl -XPOST "http://localhost:9200/_aliases" -d'
{
"actions": [
{"remove": {"index": "student_v1", "alias": "student"}},
{ "add": {"index": "student_v2", "alias": "student"}}
]
}'
再同步一次数据,至此,就完成了es不停机索引重建。
总结
reindex 操作可以复制文档,其versiontype属性有
internal: 盲目的复制文档,重复则覆盖。
external: 继承已有的版本,对于不存在的数据则创建,更新新索引中旧版本的数据。
op_type 属性:
create,如果目标索引不存在则创建,否则会冲突报异常。
conflicts:冲突时,忽略,继续操作。