十二、重建索引、索引别名

一、重建索引

  • es的索引一旦创建,只允许添加字段,不允许更改字段,因为改变字段需要重建倒排索引,影响内部缓存结构,性能太低。
# 使用reindex拷贝数据
POST _reindex
{
  "source": {
    "index": "index_v1"
  },
  "dest": {
    "index": "index_v2"
  }
}

完整脚本为:

# 重建索引

# 新建索引:index_v1,注意:索引名称必须全部小写
PUT index_v1
{
  "mappings": {
    "properties": {
      "birthday":{
        "type": "date"
      }
    }
  }
}

PUT index_v1/_doc/id1
{
  "birthday":"1995-05-10"
}

# 业务变更,需要改变birthday字段的类型为text

# 1、创建新的索引index_v2
# 2、将v1数据拷贝到v2

# 创建v2
PUT index_v2
{
  "mappings": {
    "properties": {
      "birthday":{
        "type": "text"
      }
    }
  }
}

# 使用reindex拷贝数据
POST _reindex
{
  "source": {
    "index": "index_v1"
  },
  "dest": {
    "index": "index_v2"
  }
}

PUT index_v2/_doc/id2
{
  "birthday":"1995年5月10日"
}

GET index_v2/_search

二、索引别名

前面的说到由于业务需求重建了索引,但java代码中还是使用的v1,为了避免修改代码,采用索引别名的方式来解决。

# 索引别名
# v2取代了v1,但为了不修改代码,采用索引别名
# 步骤:
# 1、先删除index_v1
DELETE index_v1
# 2、给index_v2设置一个别名:index_v1
POST index_v2/_alias/index_v1

GET index_v1/_search
GET index_v2/_search
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值