Elasticsearch 长工单分类怎么做?用 Percolator 对照整篇与分段
2026-11-26 3471917
2026-11-26 0
历史日志需要应用新映射时,先准备目标数据流,再从源流复制文档。以下请求已在 Elasticsearch 8.19.0 单节点环境执行,使用两条合成日志,把字符串形式的状态码重建为可按整数查询的字段。请在空白测试集群的 Kibana → Dev Tools → Console 中按顺序执行,每组都沿用同一批数据。
先把 status 定义为 keyword,写入两条日志,再为源流设置应用使用的读写别名。第二条写入带 refresh=true,便于后续搜索立即读取样本。
PUT /_index_template/rebuild-lab-old
{
"index_patterns": [
"rebuild-lab-old"
],
"data_stream": {},
"template": {
"settings": {
"number_of_replicas": 0
},
"mappings": {
"properties": {
"@timestamp": {
"type": "date"
},
"status": {
"type": "keyword"
}
}
}
}
}
# 实测摘录 {"acknowledged": true}
PUT /_data_stream/rebuild-lab-old
# 实测摘录 {"acknowledged": true}
PUT /rebuild-lab-old/_create/e1
{
"@timestamp": "2025-01-01T00:00:00Z",
"status": "200"
}
# 实测摘录 {"result": "created"}
PUT /rebuild-lab-old/_create/e2?refresh=true
{
"@timestamp": "2025-01-02T00:00:00Z",
"status": "500"
}
# 实测摘录 {"result": "created"}
POST /_aliases
{
"actions": [
{
"add": {
"index": "rebuild-lab-old",
"alias": "rebuild-lab-live",
"is_write_index": true
}
}
]
}
# 实测摘录 {"acknowledged": true, "errors": false}
先确认目标模板的匹配结果,再查看新流的文档计数。先模拟模板命中,再创建目标流并确认它为空。生产迁移时应事先核对组件模板、分片、副本、生命周期和写入管道。
PUT /_index_template/rebuild-lab-new
{
"index_patterns": [
"rebuild-lab-new"
],
"data_stream": {},
"template": {
"settings": {
"number_of_replicas": 0
},
"mappings": {
"properties": {
"@timestamp": {
"type": "date"
},
"status": {
"type": "integer"
}
}
}
}
}
# 实测摘录 {"acknowledged": true}
POST /_index_template/_simulate_index/rebuild-lab-new
# 实测摘录 {"status.type": "integer"}
PUT /_data_stream/rebuild-lab-new
# 实测摘录 {"acknowledged": true}
GET /rebuild-lab-new/_count
# 实测摘录 {"count": 0, "failed_shards": 0}
在生产环境中,执行前先暂停并排空源流的在途写入,必要时刷新源流,让最后一批已确认写入可搜索。按源数据流名称复制可以覆盖其所有后备索引。源文档要启用 _source,目标为数据流时必须指定 op_type 为 create。
POST /_reindex?refresh=true
{
"source": {
"index": "rebuild-lab-old"
},
"dest": {
"index": "rebuild-lab-new",
"op_type": "create"
}
}
# 实测摘录 {"timed_out": false, "total": 2, "created": 2, "version_conflicts": 0, "failures": []}
本次核对 total 和 created 均为 2,version_conflicts 为 0,failures 为空。生产迁移遇到部分失败时,应先检查目标已写入的范围和文档映射,再决定如何续跑。conflicts=proceed 只会继续处理版本冲突;数据流轮转后相同 ID 还可能落入不同后备索引,不能把重新执行当作全流去重。
分别查看两边的文档数和失败分片,再确认目标后备索引中的 status 类型,并读取按时间排序的字段值。字段映射负责按整数解析旧字符串;如果应用也要求 _source 中的 JSON 值变成数字,需要在迁移时显式转换。
GET /rebuild-lab-old/_count
# 实测摘录 {"count": 2, "failed_shards": 0}
GET /rebuild-lab-new/_count
# 实测摘录 {"count": 2, "failed_shards": 0}
GET /rebuild-lab-new/_mapping/field/status
# 实测摘录 {"status.type": "integer"}
GET /rebuild-lab-new/_search
{
"size": 2,
"sort": [
{
"@timestamp": "asc"
}
],
"_source": false,
"fields": [
"status"
]
}
# 实测摘录 {"status": [200, 500]}
生产数据建议再按时间窗口比较数量,抽查业务主键和关键查询。需要保留源后备索引的时间分段时,应单独规划分批与目标轮转,整流复制的样本只验证文档和映射。
用一次别名更新移除旧指向并添加新指向。通过别名写一条合成日志,核对响应中的 _index 是否属于目标流,再检查别名查询与旧流计数。直接使用数据流名称的应用需要同步修改写入目标、查询配置及权限。
POST /_aliases
{
"actions": [
{
"remove": {
"index": "rebuild-lab-old",
"alias": "rebuild-lab-live",
"must_exist": true
}
},
{
"add": {
"index": "rebuild-lab-new",
"alias": "rebuild-lab-live",
"is_write_index": true
}
}
]
}
# 实测摘录 {"acknowledged": true, "errors": false}
PUT /rebuild-lab-live/_create/e3?refresh=true
{
"@timestamp": "2025-01-03T00:00:00Z",
"status": 201
}
# 实测摘录 {"result": "created", "target_stream": true}
GET /rebuild-lab-live/_count
# 实测摘录 {"count": 3, "failed_shards": 0}
GET /rebuild-lab-old/_count
# 实测摘录 {"count": 2, "failed_shards": 0}
完成校验后再让生产采集写入新流,并在观察期保留旧流。若切换后已经产生新数据,回切前应暂停写入并处理新流独有的增量。仅需让未来数据采用新模板时,可评估更新模板后执行 rollover;历史文档需要按新映射查询时才走完整重建。TSDS 还需要单独规划目标后备索引可接收的时间窗口。