Elasticsearch 使用reindex进行数据同步或索引重构

1、批量复制优化

复制代码
POST _reindex
{
  "source": {
    "index": "source",
    "size": 5000
  },
  "dest": {
    "index": "dest"
  }
}

2、提高scroll的并行度优化

复制代码
POST _reindex?slices=5&refresh
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  }
}

slices大小设置注意事项:

1)slices大小的设置可以手动指定,或者设置slices设置为auto,auto的含义是:针对单索引,slices大小=分片数;针对多索引,slices=分片的最小值。

2)当slices的数量等于索引中的分片数量时,查询性能最高效。slices大小大于分片数,非但不会提升效率,反而会增加开销。

3)如果这个slices数字很大(例如500),建议选择一个较低的数字,因为过大的slices 会影响性能。

效果

实践证明,比默认设置reindex速度能提升10倍+。

3、条件查询以及部分字段同步

复制代码
{
  "source": {
    "index": "maindata",
    "_source": [ //查询字段
      "dataId",
      "website"
    ],
    "query": {
      "match_phrase": {
        "teamId": 3
      }
    },
    "excludes": [ "column1","column2" ] //排除字段
  },
  "dest": {
    "index": "maindatagroup",
    "version_type": "internal"
  }
}

说明:

"version_type": "internal",internal表示内部的,省略version_type或version_type设置为 internal 将导致 Elasticsearch 盲目地将文档转储到目标中,覆盖任何具有相同类型和 ID 的文件。

这也是最常见的重建方式。

4、从远程中重建索引

复制代码
POST _reindex
{
  "source": {
    "remote": {
      "host": "http://otherhost:9200",
      "username": "user",
      "password": "pass",
      "socket_timeout": "1m",
      "connect_timeout": "10s"
    },
    "index": "source",
    "query": {
      "match": {
        "test": "data"
      }
    }
  },
  "dest": {
    "index": "dest"
  }
}

注:需要给新的es配置白名单:reindex.remote.whitelist: "172.16.76.147:9200"

5、重构数据之取余

将publicsentimenthot 数据通过organId 取余2 ,把数据分配到相应的索引上

复制代码
POST  _reindex
{
  "source": {
    "index": "publicsentimenthot",
    "size": 1000
  },
  "dest": {
    "index": "pubtest_0",
    "op_type": "create"
  },
  "script": {
    "lang": "painless",
    "source": "ctx._index = 'pubtest_' + (ctx._source.organId ?: 0) % 2;"
  }
}

6、查询reindex任务

(1)获取reindex任务列表
复制代码
GET _tasks?detailed=true&actions=*reindex
(2)根据任务id查看任务
复制代码
GET _tasks/r1A2WoRbTwKZ516z6NEs5A:36619

注: r1A2WoRbTwKZ516z6NEs5A:36619 为任务列表的id

(2)取消任务
复制代码
POST _tasks/r1A2WoRbTwKZ516z6NEs5A:36619/_cancel

7、logstash 按照数据id重构索引

复制代码
input {
  elasticsearch {
    hosts => ["第一个集群地址"]
    index => "源索引名称"
    query => '{"query": {"match_all": {}}}'
    size => 1000
    scroll => "5m"
    docinfo => true
  }
}

filter {
  ruby {
    code => "
      organ_id = event.get('organId').to_i rescue 0
      target_index = '目标索引前缀_' + (organ_id % 10).to_s
      event.set('[@metadata][target_index]', target_index)
    "
  }
}

output {
  elasticsearch {
    hosts => ["第二个集群地址"]
    index => "%{[@metadata][target_index]}"
    document_id => "%{[@metadata][_id]}"
  }
}
相关推荐
明航咨询—张老师9 小时前
软考软件评测师技术深度剖析:2026年考试改革后的知识体系与备考架构
大数据·架构
千桐科技10 小时前
qData 数据中台开源版从 MySQL 到 Doris完成一次数据同步
大数据·mysql·开源·doris·数据中台·qdata
ACP广源盛1392462567312 小时前
GSV6155@ACP# 搭配 AI 服务器、AI PC 完整适配方案
大数据·服务器·人工智能·分布式·单片机·嵌入式硬件
stonewl259914 小时前
2026化工行业 GHS/CLP 国产标签打印软件合规打印方案
大数据·人工智能·物联网
Wzx19801214 小时前
Redis&ES——Retriever的抽象实现
数据库·人工智能·redis·elasticsearch
xywww16814 小时前
AWS 账号权限怎么分:根用户和 IAM 用户区别及日常使用建议
大数据·开发语言·人工智能·python·gpt·云计算·aws
Quincy_Freak15 小时前
SQLiteGo V2.3 版本更新说明
大数据·数据库·sqlite·数据库管理·大数据分析·sqlitego
牛奶咖啡1315 小时前
大数据Hadoop运维应用实践——双NameNode高可用Hadoop集群架构(下)
大数据·hadoop·hadoop集群配置文件解析·hadoop高可用集群安装部署·配置指定多台服务器相互免密·配置hadoop服务开机自启·ansible部署hadoop
在水一缸15 小时前
深度解析 GPT-5.6:大模型架构演进与复杂任务实战指南
大数据·人工智能·gpt·架构·大模型·架构演进·gpt-5.6
Henrii_历小海15 小时前
WhatsApp Business API 2026 计费架构重构:从“对话计费“到“按消息计费“的技术实现与工程应对
java·重构·架构