es 重建索引

218 阅读1分钟

前言

1、当你的数据量过大,此时想要扩大分片的数量,可以使用Reindex
2、当数据的mapping需要修改,你不能直接修改mapping,只能使用Reindex
3、当你想要修改副本数量时,可以使用Reindex

重建索引 reindex

POST _reindex
{
  "source": {
    "index": "my-index-000001"
  },
  "dest": {
    "index": "my-new-index-000001"
  }
}

实践

  • 1 获取索引映射
   GET index_abc/_mapping
  • 2 创建索引
PUT /index_abc_new
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 1
    }
  },
  "mappings": {
    "properties": {
      "applyDate": {
        "type": "date",
        "store": true,
        "format": "date_hour_minute_second"
      },
      "id": {
        "type": "long"
      },
      "name": {
        "type": "text",
        "store": true,
        "analyzer": "ik_max_word"
      }
    }
  }
}
  • 3 重建索引
POST _reindex?slices=5&refresh
{
  "source": {
    "index": "index_abc",
    "size": 5000
  },
  "dest": {
    "index": "index_abc_new"
  }
}
  • 4 检查数据是否在新的索引里面
POST index_abc_new/_search
{
  "query": {
    "match_all": {}
  }
}
  • 5 删除原索引(最好有重建别名
DELETE index_abc
  • 6 创建索引别名
POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "index_abc_new",
        "alias": "index_abc",
        "is_write_index" : true
      }
      
    }
  ]
}

参考文献