ElasticSearch的reIndex

由于Elasticsearch已创建的index是不允许修改的,其原因是由于创建的时候就会创建倒排索引,但是此时应用程序已经在使用了该索引,并且不允许应用暂停,那么怎么去处理这样的事情呢?下面就是一个比较靠谱的解决方案,就是使用alias,scroll api,bucket api。

具体案例如下:

保存数据
PUT /index_before/type_before/1
{
"title":"2017-01-01"
}
查询数据
GET index_before/_mapping/type_before
结果发现类型不对
{
"index_before": {
"mappings": {
"type_before": {
"properties": {
"title": {
"type": "date"
}
}
}
}
}
}

设置别名,index_mid中间过渡(java程序可以使用别名的index),指向之前的索引
PUT index_before/_alias/index_mid

新建正确类型的索引
PUT /index_after
{
"mappings": {
"type_before":{
"properties": {
"title":{
"type": "string"
}
}
}
}
}


使用scroll api获得原始数据
GET index_before/_search?scroll=1m
{
"query": {
"match_all": {}
},
"sort": [
"_doc"
],
"size": 1
}


使用bulk api将查询的数据转存到新的索引中去
POST /_bulk
{"index":{"_index":"index_after","_type":"type_before","_id":1}}
{"title":"2017-01-01"}

更改索引别名

POST /_aliases
{
"actions": [
{ "remove": { "index": "index_before", "alias": "index_mid" }},
{ "add":{ "index": "index_after", "alias": "index_mid" }}
]
}

使用别名查询
GET index_mid/type_before/_search
结果就可以了
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "index_after",
"_type": "type_before",
"_id": "1",
"_score": 1,
"_source": {
"title": "2017-01-01"
}
}
]
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值