ElasticSearch 重建索引 _reindex

前言

何为重建索引

在原index的基础上copy一份数据在新index(字段相同,类型可不相同)。

为什么有重建索引的需求

当分词插件变更,数据类型改变等等。(当然也可以直接重跑一份数据)

如何重建索引

请借步参阅官网文档https://www.elastic.co/guide/en/elasticsearch/reference/6.5/docs-reindex.html

生产案例

需求

需要对现有数据内的一个字段进行排序。

问题

需要排序的字段存储类型是字符串,业务上值是数字 且并未开启fielddata1

解决方案

新建index mapping 使对应字段数据类型为long,其他字段保持不变。使用reindex进行数据重建

扩展

重建后的新索引,如何不停机的迁移到生产环境。在生产环境上建议使用 索引别名(Index Aliases2) 而不是直接使用真实的index name。(下文示例会说明)

示例

使用reindex API

POST _reindex?wait_for_completion=false//直接返回结果
{
    
    
  "source": {
    
    
    "index": "hot-search1" //目标索引
  },
  "dest": {
    
    
    "index": "hot-search2"//新索引
  }
}

API返回结果

{
    
    
  "task": "dOlIdAkxQEOpXmHOjb3e4A:385537"//任务结果
}

tasks API

查询任务详情 官网文档https://www.elastic.co/guide/en/elasticsearch/reference/6.5/tasks.html

GET _tasks/dOlIdAkxQEOpXmHOjb3e4A:385537

修改别名 新索引平滑迁移到生产环境

POST _aliases
{
    
    
  "actions": [{
    
    "add": {
    
    //新增别名
    "index": "hot-search2",
    "alias": "hot-search"
  }}, {
    
    "remove": {
    
    //移除别名
    "index": "hot-search1",
    "alias": "hot-search"
  }}]
}

别名hot-search 指向了新的索引hot-search2 ,生产环境使用别名hot-search,这样索引变更就不会影响到生产环境。


  1. fielddata:https://www.elastic.co/guide/en/elasticsearch/reference/6.5/fielddata.html ↩︎

  2. Index Aliaseshttps://www.elastic.co/guide/en/elasticsearch/reference/6.5/indices-aliases.html ↩︎

猜你喜欢

转载自blog.csdn.net/sinat_25926481/article/details/103196393