笔记四十四:Update By Query & Reindex API

使用场景

  • 一般在以下几种情况时,我们需要重建索引:
    • 索引的 Mappings 发生变更:字段类型更改,分词器及字典更新
    • 索引的 Setting 发生变更:索引的主分片数发生改变
    • 集群内,集群间需要做数据迁移
  • ElastiicSearch 的内置提供的API
    • Update By Query :在现有索引上重建
    • Reindex:在其他索引上重建索引

案例一: 为索引增加子字段

  • 改变 Mapping , 增加子字段,使用英文分词器
  • 此时尝试对子字段进行查询
  • 虽然有数据已经存在,但是没有返回结果

ES笔记四十四:Update By Query & Reindex API

Update By Query

  • 执行 Update By Query
  • 尝试对 Multi-Fields 查询查询
  • 返回结果

ES笔记四十四:Update By Query & Reindex API

# 写入文档
PUT blogs/_doc/1
{
  "content":"Hadoop is cool",
  "keyword":"hadoop"
}
# 修改 Mapping,增加子字段,使用英文分词器
PUT blogs/_mapping
{
  "properties" : {
    "content" : {
      "type" : "text",
      "fields" : {
        "english" : {
          "type" : "text",
          "analyzer":"english"
        }
      }
    }
  }
}

    # 写入文档
PUT blogs/_doc/2
{
  "content":"Elasticsearch rocks",
  "keyword":"elasticsearch"
}

# 查询新写入文档
POST blogs/_search
{
  "query": {
    "match": {
      "content.english": "Elasticsearch"
    }
  }
}

# 查询 Mapping 变更前写入的文档
POST blogs/_search
{
  "query": {
    "match": {
      "content.english": "hadoop"
    }
  }
}

# Update所有文档
POST blogs/_update_by_query
{

}

案例二:更改已有字段类型的Mappings

  • ES 不允许在原有 Mapping 上对字段类型进行修改
  • 只能创建新的索引,并设定正确的字段类型,在重新导入数据

ES笔记四十四:Update By Query & Reindex API

# 创建新的索引并且设定新的Mapping
PUT blogs_fix/
{
  "mappings": {
    "properties": {
      "content": {
        "type": "text",
        "fields": {
          "english": {
            "type": "text",
            "analyzer": "english"
          }
        }
      },
      "keyword": {
        "type": "keyword"
      }
    }
  }
}

# Reindx API
POST  _reindex
{
  "source": {
    "index": "blogs"
  },
  "dest": {
    "index": "blogs_fix"
  }
}

GET  blogs_fix/_doc/1

# 测试 Term Aggregation
POST blogs_fix/_search
{
  "size": 0,
  "aggs": {
    "blog_keyword": {
      "terms": {
        "field": "keyword",
        "size": 10
      }
    }
  }
}

Reindex API

  • Reindex API 支持把文档从一个索引拷贝到另外一个索引
  • 使用 Reindex API 的一些场景
    • 修改索引的主分片数
    • 改变字段的Mapping 中的字段类型
    • 集群中数据迁移、跨集群的数据迁移
# Reindx API,version Type Internal
POST  _reindex
{
  "source": {
    "index": "blogs"
  },
  "dest": {
    "index": "blogs_fix",
    "version_type": "internal"
  }
}

# 文档版本号增加
GET  blogs_fix/_doc/1

# Reindx API,version Type Internal
POST  _reindex
{
  "source": {
    "index": "blogs"
  },
  "dest": {
    "index": "blogs_fix",
    "version_type": "external"
  }
}

# Reindx API,version Type Internal
POST  _reindex
{
  "source": {
    "index": "blogs"
  },
  "dest": {
    "index": "blogs_fix",
    "version_type": "external"
  },
  "conflicts": "proceed"
}

ES笔记四十四:Update By Query & Reindex API

两个注意点

  • 索引的 mapping _source 要开启
  • 先创建一个新索引,然后在执行 reindex
    ES笔记四十四:Update By Query & Reindex API

OP Type

  • _reindex 指挥创建不存在的文档
  • 文档如果存在,会导致版本冲突
    # Reindx API,version Type Internal
      POST  _reindex
      {
        "source": {
          "index": "blogs"
        },
        "dest": {
          "index": "blogs_fix",
          "op_type": "create"
        }
      }

ES笔记四十四:Update By Query & Reindex API

跨集群 ReIndex

  • 需要修改 elasticsearch.yml ,并且重启节点

ES笔记四十四:Update By Query & Reindex API

查看 Task API

  • Reindex API 支持一步操作,执行只返回 Task Id
  • POST _reindex?wait_for_completion=false

ES笔记四十四:Update By Query & Reindex API

回顾

  • Update By Query 使用场景: 为字段新增子字段;字段更改分词器;更新分词器词库
  • Reindex API 使用场景:修改字段类型
    • 需要先对新索引设置 Mapping,索引的设置和映射关系不会被复制
  • 通过查看 Task API,了解 Reindex 的状况
  • Remote ReIndex ,需要修改 elasticsearch.yml 配置并且重启
  • 一定要尽量使用Index Alias 读写数据。即便发生 Reindex,也能实现零停机维护
本作品采用《CC 协议》,转载必须注明作者和本文链接
快乐就是解决一个又一个的问题!
CrazyZard
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!