AI自媒体写作平台推荐:按每天交稿量选写作和排期工具
2026-11-28
2026-11-27 0
重复提交 _reindex 时,Elasticsearch 会重新执行源查询。源查询未设置过滤条件时,所有对查询可见的源文档都可能进入本轮,目标端才会根据 ID 决定如何写入。
在 Elasticsearch 8.19.0 的 Kibana Dev Tools → Console 中逐段运行。实验使用两个尚未创建的普通索引,源与目标都显式声明日期映射。先准备两条固定 ID 的样本,写入时用 refresh=true 让后续搜索可见。
PUT reindex_lab_src
{"mappings":{"properties":{"updated_at":{"type":"date"}}}}
PUT reindex_lab_dst
{"mappings":{"properties":{"updated_at":{"type":"date"}}}}
PUT reindex_lab_src/_doc/1?refresh=true
{"name":"tea","updated_at":"2025-01-01T00:00:00Z"}
PUT reindex_lab_src/_doc/2?refresh=true
{"name":"coffee","updated_at":"2025-01-02T00:00:00Z"}先向空目标复制一次,然后原样提交同一请求。此时两轮的源查询条件完全相同。
POST _reindex?refresh=true
{
"source": {"index": "reindex_lab_src"},
"dest": {"index": "reindex_lab_dst"}
}
POST _reindex?refresh=true
{
"source": {"index": "reindex_lab_src"},
"dest": {"index": "reindex_lab_dst"}
}
默认使用内部版本和索引写入语义。目标中缺少该 ID 时计为 created;已有该 ID 时覆盖并计为 updated,即使源 JSON 完全相同,也会执行写入。这里两轮的 total 都是 2。这个计数说明任务处理的文档数量,不能用来衡量底层访问了多少索引记录。
沿用已有两条文档的目标索引,把目标写操作改成 create。再用 conflicts: proceed 让任务记录版本冲突并继续处理;映射错误等其他失败仍需查看 failures。
POST _reindex?refresh=true
{
"source": {"index": "reindex_lab_src"},
"dest": {"index": "reindex_lab_dst", "op_type": "create"},
"conflicts": "proceed"
}
两条源文档仍然进入本轮处理,并在目标端分别触发一次版本冲突。若遗漏 conflicts: proceed,默认遇版本冲突会中止,已完成的写入仍可能留在目标端。需要更新已有 ID 时,应选用允许覆盖的写入策略。
先更新源文档 2,再只选择 1 月 3 日的 UTC 时间窗口。用 gte 包含窗口起点,用 lt 排除下一窗口起点。完成后直接读取目标文档核对内容。
PUT reindex_lab_src/_doc/2?refresh=true
{"name":"iced coffee","updated_at":"2025-01-03T00:00:00Z"}
POST _reindex?refresh=true
{
"source": {
"index": "reindex_lab_src",
"query": {
"range": {
"updated_at": {
"gte": "2025-01-03T00:00:00Z",
"lt": "2025-01-04T00:00:00Z"
}
}
}
},
"dest": {"index": "reindex_lab_dst"}
}
GET reindex_lab_dst/_doc/2
这次两条源记录中只有一条符合 source.query。定时同步时,可把上次成功窗口的上界保存为下一次下界,并在本轮成功且核对失败列表后推进检查点。时间字段需要在创建和更新时维护;存在迟到写入时,可重叠窗口并用稳定 ID 重放。源端删除需要另行同步。
先确认源查询命中范围,再解释写入计数。按 ID 补缺可以容忍预期的版本冲突,按时间迁移则要把窗口写进 source.query。