ElasticSearch之Clone index API

发布时间 2023-12-02 15:42:53作者: jackieathome

使用已有的索引,复制得到一个索引。

关闭testindex_001的写入操作,命令样例如下:

curl -X PUT "https://localhost:9200/testindex_001/_settings?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "index.blocks.write": true
  }
}
' --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

通过复制testindex_001来创建新的index,命令样例如下:

curl -X POST "https://localhost:9200/testindex_001/_clone/cloned-testindex_001?pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

{
  "acknowledged" : true
}

或者使用PUT方法,命令样例如下:

curl -X PUT "https://localhost:9200/testindex_001/_clone/cloned-testindex_002?pretty" --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "cloned-testindex_002"
}

假如复制前没有关闭写入,复制操作将失败,提示信息,样例如下:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_state_exception",
        "reason" : "index testindex_001 must be read-only to resize index. use \"index.blocks.write=true\""
      }
    ],
    "type" : "illegal_state_exception",
    "reason" : "index testindex_001 must be read-only to resize index. use \"index.blocks.write=true\""
  },
  "status" : 500
}

恢复testindex_001的写入操作,命令样例如下:

curl -X PUT "https://localhost:9200/testindex_001/_settings?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "index.blocks.write": false
  }
}
' --cacert $ES_HOME/config/certs/http_ca.crt -u "elastic:ohCxPH=QBE+s5=*lo7F9"

执行结果的样例,如下:

{
  "acknowledged" : true
}

实现原理
复制的操作过程:

  • 依据源索引的参数和数据,使用目标索引的名称,创建一个新的索引对象。
  • 将源索引相关的segment的对象和文件,关联至目标索引。
    • 假如节点的运行平台的文件系统支持POSIX语义中的硬链接,则使用硬链接方式。
    • 假如节点的运行平台的文件系统不支持POSIX语义中的硬链接,则使用复制方式,相对要耗时,同时占用存储空间。
  • 更新目标索引的状态,类似关闭后重新打开的索引。

约束项:

  • 目标索引的名称需要符合命名规范。
  • 目标索引不存在存在,否则复制操作将失败。
  • 目标索引和源索引,主分片的数量需要一致。
  • 参与复制操作的节点,需要有充足的存储空间来容纳新的索引的分片和副本。

方法参数
参考Create index API
方法的请求消息体
参考Create index API

方法的响应消息体
参考Create index API

相关资料