索引重建API
源索引必须开启_source
- _reindex:重建API
- source:指定原有索引名称
dest:指定新索引名称
POST /_reindex
{
"source": {
"index": "ckiss-company-001"
},
"dest": {
"index": "ckiss-company-002"
},
"script":{
"source": "SCRIPT",
"lang": "painless",
"params":{}
}
}
URL参数
refresh:新建索引是否立即刷新,可查询
- timeout:超时时间,默认1m
- wait_for_active_shards:是否立即响应,默认等待主分片重建完成,默认是1
- wait_for_completion:是否阻塞重建任务请求,默认阻塞,可设置为异步
- scroll:设置快照查询持续时间
重建索引任务操作 task
GET _cat/tasks?v
GET _tasks?actions=*reindex
GET _tasks/node:taskId
# 终止任务
POST _tasks/node:taskId/_cancel
重建索引的参数设置
单秒数据阈值控制 requests_per_second
- request_per_second:每秒处理数据条数,默认-1,建议设置500-1000左右,防止瞬间IO过大
- 可动态调整
- 一旦设置,只能通过后台任务查看
POST _reindex/node:id/_rethrottle?request_per_second=500
数据切片 slice
sclice:默认是1,可以按照索引数据进行切片设置,切成多块,提高索引重建的并行度
max:切片数量
id:切片编号,默认从0开始,不能大于max案例:人工分片
POST /_reindex
{
"source": {
"index": "ckiss-company-001",
"slice": {
"id": 0,
"max": 3
}
},
"dest": {
"index": "ckiss-company-002"
}
}
POST /_reindex
{
"source": {
"index": "ckiss-company-001",
"slice": {
"id": 1,
"max": 3
}
},
"dest": {
"index": "ckiss-company-002"
}
}
POST /_reindex
{
"source": {
"index": "ckiss-company-001",
"slice": {
"id": 2,
"max": 3
}
},
"dest": {
"index": "ckiss-company-002"
}
}
案例:自动分片, 设置自动分片,slices=2
POST /_reindex?slices=2&refresh=true
{
"conflicts": "proceed",
"source": {
"index": "ckiss-company-001"
},
"dest": {
"index": "ckiss-company-002"
}
}
重建索引数据路由 routing
POST /_reindex
{
"conflicts": "proceed",
"source": {
"index": "ckiss-company-001"
},
"dest": {
"index": "ckiss-company-002",
"routing": "man"
}
}
#查询
GET ckiss-company-002/_search?routing=man&track_total_hits=true&size=0
限制查询范围 max_docs
- query基于查询过滤,限制重建范围
max_docs 限制查询数据数量
POST /_reindex
{
"conflicts": "proceed",
"max_docs": 10,
"source": {
"index": "ckiss-company-001",
"query": {
xxx
}
},
"dest": {
"index": "ckiss-company-002"
}
}
多索引重建合并 index
多索引合并,有这样的场景,原因可能是历史设计不合理
- 默认采用覆盖更新
- op_type:设定重建索引方式, index/create. 默认index:可覆盖 ```json
POST /_reindex { “source”: { “index”: [ “ckiss-company-001”, “ckiss-company-002” ] }, “dest”: { “index”: “ckiss-company-0010”, “op_type”: “index” } }
<a name="MuTYX"></a>
### 限制重建索引字段 _source
```json
POST /_reindex
{
"source": {
"index": [
"ckiss-company-001"
],
"_source":[xxxx]
},
"dest": {
"index": "ckiss-company-0010",
"op_type": "index"
}
}
字段重命名
POST /_reindex
{
"source": {
"index": [
"ckiss-company-001"
],
"_source": [
"filedName"
]
},
"dest": {
"index": "ckiss-company-0010"
},
"script": {
"source": "ctx._source.filedNewName=ctx._source;ctx._source.remove('filedName')",
"lang": "painless"
}
}
异步执行重建索引
wait_for_completion : 取值范围 true/false. 默认true, 若要异步执行则设置为false