索引重建API

源索引必须开启_source

  • _reindex:重建API
  • source:指定原有索引名称
  • dest:指定新索引名称

    1. POST /_reindex
    2. {
    3. "source": {
    4. "index": "ckiss-company-001"
    5. },
    6. "dest": {
    7. "index": "ckiss-company-002"
    8. },
    9. "script":{
    10. "source": "SCRIPT",
    11. "lang": "painless",
    12. "params":{}
    13. }
    14. }

    URL参数

  • refresh:新建索引是否立即刷新,可查询

  • timeout:超时时间,默认1m
  • wait_for_active_shards:是否立即响应,默认等待主分片重建完成,默认是1
  • wait_for_completion:是否阻塞重建任务请求,默认阻塞,可设置为异步
  • scroll:设置快照查询持续时间

重建索引任务操作 task

  1. GET _cat/tasks?v
  2. GET _tasks?actions=*reindex
  3. GET _tasks/node:taskId
  4. # 终止任务
  5. POST _tasks/node:taskId/_cancel

重建索引的参数设置

单秒数据阈值控制 requests_per_second

  • request_per_second:每秒处理数据条数,默认-1,建议设置500-1000左右,防止瞬间IO过大
  • 可动态调整
  • 一旦设置,只能通过后台任务查看
    1. POST _reindex/node:id/_rethrottle?request_per_second=500

    数据切片 slice

    sclice:默认是1,可以按照索引数据进行切片设置,切成多块,提高索引重建的并行度
    max:切片数量
    id:切片编号,默认从0开始,不能大于max

    案例:人工分片

  1. POST /_reindex
  2. {
  3. "source": {
  4. "index": "ckiss-company-001",
  5. "slice": {
  6. "id": 0,
  7. "max": 3
  8. }
  9. },
  10. "dest": {
  11. "index": "ckiss-company-002"
  12. }
  13. }
  14. POST /_reindex
  15. {
  16. "source": {
  17. "index": "ckiss-company-001",
  18. "slice": {
  19. "id": 1,
  20. "max": 3
  21. }
  22. },
  23. "dest": {
  24. "index": "ckiss-company-002"
  25. }
  26. }
  27. POST /_reindex
  28. {
  29. "source": {
  30. "index": "ckiss-company-001",
  31. "slice": {
  32. "id": 2,
  33. "max": 3
  34. }
  35. },
  36. "dest": {
  37. "index": "ckiss-company-002"
  38. }
  39. }

案例:自动分片, 设置自动分片,slices=2

  1. POST /_reindex?slices=2&refresh=true
  2. {
  3. "conflicts": "proceed",
  4. "source": {
  5. "index": "ckiss-company-001"
  6. },
  7. "dest": {
  8. "index": "ckiss-company-002"
  9. }
  10. }

重建索引数据路由 routing

  1. POST /_reindex
  2. {
  3. "conflicts": "proceed",
  4. "source": {
  5. "index": "ckiss-company-001"
  6. },
  7. "dest": {
  8. "index": "ckiss-company-002",
  9. "routing": "man"
  10. }
  11. }
  12. #查询
  13. GET ckiss-company-002/_search?routing=man&track_total_hits=true&size=0

限制查询范围 max_docs

  • query基于查询过滤,限制重建范围
  • max_docs 限制查询数据数量

    1. POST /_reindex
    2. {
    3. "conflicts": "proceed",
    4. "max_docs": 10,
    5. "source": {
    6. "index": "ckiss-company-001",
    7. "query": {
    8. xxx
    9. }
    10. },
    11. "dest": {
    12. "index": "ckiss-company-002"
    13. }
    14. }

    多索引重建合并 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” } }

  1. <a name="MuTYX"></a>
  2. ### 限制重建索引字段 _source
  3. ```json
  4. POST /_reindex
  5. {
  6. "source": {
  7. "index": [
  8. "ckiss-company-001"
  9. ],
  10. "_source":[xxxx]
  11. },
  12. "dest": {
  13. "index": "ckiss-company-0010",
  14. "op_type": "index"
  15. }
  16. }

字段重命名

  1. POST /_reindex
  2. {
  3. "source": {
  4. "index": [
  5. "ckiss-company-001"
  6. ],
  7. "_source": [
  8. "filedName"
  9. ]
  10. },
  11. "dest": {
  12. "index": "ckiss-company-0010"
  13. },
  14. "script": {
  15. "source": "ctx._source.filedNewName=ctx._source;ctx._source.remove('filedName')",
  16. "lang": "painless"
  17. }
  18. }

异步执行重建索引

wait_for_completion : 取值范围 true/false. 默认true, 若要异步执行则设置为false