elasticesearch想要修改mapping,使用reindex

发现以前老的索引需要改动,加字段啊,改类型什么的就必须修改索引的mapping. 但是elasticesearch的mapping一旦创建是不能修改.

要么你重新建一个索引,设置新的mapping然后再导入数据或者我们reindex的方式来修改mapping并完成数据的迁移;

1,创建新的索引,设置好mapping

POST  admin_regin2/_doc
  {  
  "mapping": {
  "_doc":{
      "properties": {
        "areaCode": {
          "type": "long"
        },
        "createUsername": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256,
              "index":false,
            "store":false
            }
          }
        },
        "id": {
          "type": "long"
        },
        "level": {
          "type": "boolean"
        },
        "mergerName": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

2,为新的索引设置好别名(查询时候可以通过指定此别名查询,和指定原索引名效果一样),设置别名的目的就是为了使用原来索引的语句不用改;  我将新的索引别名设置成原来旧的索引名,

然后旧的查询直接无缝切换到新建的索引

//  为admin_regin2 创建别名admin_regin

PUT  admin_regin2/_aliases
{
  "actions":[
    {"add":
      {"index":"admin_regin2", "alias": "admin_regin"}
    }
  ]
}

3,reindex,将旧索引数据迁移到新的索引,   但如果新的index中有数据,并且可能发生冲突,那么可以设置version_type"version_type": "internal"或者不设置,则Elasticsearch强制性的将文档转储到目标中,覆盖具有相同类型和ID的任何内容:

POST _reindex
{
  "source": {
    "index": "admin_regin"
  },
  "dest": {
    "index": "admin_regin2",
     "version_type": "internal"
  }
}

后面我就可以删除掉原来的索引,使用admin_regin 或者admin_regin2都能访问新的索引库了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值