ElasticSearch 索引重建

需要重建索引的几种情况:

  • 索引字段类型错误,名称被占用

  • 单个分片数据量大查询性能下降,增加分片数

  • 单个分片容量超过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访问新的索引的数据了。

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值