ElasticSearch 修改mapping 迁移数据

1,152 阅读1分钟

记一次es mapping的迁移

原因

因为需要更改字段的type,因此需要对es的mapping进行修改。我准备用reindex来进行数据的迁移

版本

es 6.8 kibana 6.8

开始迁移

1.新建索引

原索引为old_index

mapping:

{"perproties":{
    "name":{
        "type":"text"
    }
}}

新建索引为new_index

使用kibana的Ded Tools来进行操作

新建索引

PUT new_index
{}

添加mapping

POST new_index/_doc/_mapping
{
    "perproties":{
        "name":{
            "type":"keywords"
        }
    }
}

迁移数据

POST _reindex
{
  "source": {
    "index": "old_index"
  },
  "dest": {
    "index": "new_index",
    "version_type": "internal"
  }
}

至此,我们已经将old_index的数据迁移到new_index,现在抱有两个索引,old_index和new_index。我们之前使用的是old_index。所以我们需要将new_index映射到old_index上。

索引映射

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "new_index",
        "alias": "old_index"
      }
    },
    {
      "remove_index": {
        "index": "old_index"
      }
    }
  ]
}

通过aliases方法我们可以修改映射

_remove_index会删除原索引。原索引只有删除了之后才能进行映射。

//TO DO 我发现在kibana中discover两个索引。当我们在new_index中添加了数据之后,old_index也会添加一份数据。

本文参考以下两篇文章,感谢两位作者的文章:

ES数据库重建索引——Reindex(数据迁移)

使用 Elasticsearch alias 功能切换 index