Elasticsearch重建索引

346 阅读1分钟

概述

很多情况下,Elasticsearch是需要重建索引的. 比如主分片数量想修改,加了keyword类型等.

操作步骤

1.首先创建一个新索引,结构一致
PUT /myindex_new
{
  "settings": {
    "number_of_shards": 2
    , "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "name":{
        "type": "keyword"
      },
      "age":{
        "type": "long"
      }
    }
  }
}

2.数据迁移
POST /_reindex?wait_for_completion=false
{
  "source": {
    "index": "myindex"
  },
  "dest": {
    "index": "myindex_new"
  }
}

3.删除老索引
DELETE myindex

3.给新索引加别名
POST _aliases 
{
  "actions": [
    {
      "add": {
        "index": "myindex_new",
        "alias": "myindex"
      }
    }
  ]
}

效果

image.png 索引名称已经变成myindex_new,为了不影响程序使用,使用别名myindex

image.png name字段改成keyword,已经支持折叠了(collapse)