天天看点

ElasticSearch 学习笔记:Reindex

本文目录

1 创建新的索引(Index)

2 导入数据(Reindex)

3 批量更新(Update by Query)

4 新建/删除别名(Alias)

5 相关文章

1 创建新的索引(Index)

注意:若需要向已存在的索引迁移数据,则不需要创建新的索引。

PUT /<indexName>
{
  "settings": {
    "index": {
      "number_of_shards": 5,
      "number_of_replicas": 1
    }
  }
}
           

2 导入数据(Reindex)

POST /_reindex
{
  "source": {
    "index": "<sourceIndexName>",
    "type": "<sourceTypeName>",
    "query": {
      ...
    },
    "sort": {
      "<fieldName>": "asc/desc"
    },
    "size": <sizeNum>,
    "_source": [
      "<fieldName>",
      "<fieldName>",
      ...
    ]
  },
  "dest": {
    "index": "<destIndexName>",
    "version_type": "internal(default)/external",
    "op_type": "create"
  }
}
           

说明:

(1)version_type:

internal 表示迁移全部数据 且 完全覆盖冲突文档(即使目标索引文档版本新于源索引文档)。

external 表示迁移全部数据 且 更新旧版本冲突文档。

(2)op_type:

create 表示仅创建目标索引不存在的文档。

3 批量更新(Update by Query)

POST /<indexName>/<typeName>/_update_by_query
{
  "query": {
    "match_all": {}
  },
  "script": {
    "inline/source": "ctx._source.<fieldName> = ctx._source.<fieldName> + ''"
  }
}
           

说明:

inline:ES 5.6版本之前;

source:ES 5.6版本及以后

4 新建/删除别名(Alias)

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "<indexName>",
        "alias": "<alias>"
      }
    }
  ]
}

{
  "actions": [
    {
      "remove": {
        "index": "<indexName>",
        "alias": "<alias>"
      }
    }
  ]
}
           

5 相关文章

《ElasticSearch 学习笔记:常用内容》

《ElasticSearch 学习笔记:Mapping》

《ElasticSearch 学习笔记:Multi Search》

《ElasticSearch 学习笔记:聚合(Aggregation) - Top Hits》

继续阅读