Elasticsearch 长工单分类怎么做?用 Percolator 对照整篇与分段
2026-11-26 3471917
2026-11-26 0
打开文档能看到原值,查询却命不中?先区分 _source 与 mapping,再用四个新索引排查动态检测。 示例使用 Elasticsearch 8.19.0,可在 Kibana Dev Tools Console 分块运行。请在测试集群使用尚未占用的示例索引名。
先写入相同的日期、数量及编号值,另外保留空值和空数组作为对照。
PUT /b3523-default
{
"mappings": {}
}
PUT /b3523-default/_doc/1
{
"event_day": "2026-11-26",
"quantity": "42",
"batch_code": "0042",
"optional": null,
"items": []
}
GET /b3523-default/_mapping
# 实测摘要 event_day → date;quantity、batch_code → text,均带 keyword 子字段;optional、items 未出现。数字字符串默认沿用字符串推断,JSON 数值 42 则会推断为 long。null、空数组都没有可用于推断的具体值,数组在出现首个非 null 值时才有类型依据。
再建一个索引,只调整 date_detection 和 numeric_detection,文档保持一致。
PUT /b3523-flags
{
"mappings": {
"date_detection": false,
"numeric_detection": true
}
}
PUT /b3523-flags/_doc/1
{
"event_day": "2026-11-26",
"quantity": "42",
"batch_code": "0042",
"optional": null,
"items": []
}
GET /b3523-flags/_mapping
# 实测摘要 event_day → text;quantity、batch_code → long;optional、items 未出现。打开数字检测也会把带前导零的编号识别成数值。订单号等标识符适合显式配置为 keyword;日期字符串要参与日期查询时,显式声明 date 和上游约定的 format。
第三组把同一份文档放在 payload 下,并在该对象上关闭动态映射。
PUT /b3523-object
{
"mappings": {
"properties": {
"payload": {
"type": "object",
"dynamic": false
}
}
}
}
PUT /b3523-object/_doc/1
{
"payload": {
"event_day": "2026-11-26",
"quantity": "42",
"batch_code": "0042",
"optional": null,
"items": []
}
}
GET /b3523-object/_mapping
GET /b3523-object/_doc/1
# 实测摘要 mapping 中 payload 下没有新增字段;返回的 _source.payload 仍含完整输入。普通索引启用 _source 时,读取到原文只证明文档保留了该值。如果 mapping 里缺字段,沿完整路径看根级和各层对象的 dynamic。strict 遇到未知字段会拒绝写入,runtime 则要在 mapping 的 runtime 节点找运行时字段。
用同样的文档验证已知业务字段,再尝试将第一组索引中已有的 quantity 改成 long。
PUT /b3523-fixed
{
"mappings": {
"properties": {
"event_day": {
"type": "date",
"format": "strict_date"
},
"quantity": {
"type": "long"
},
"batch_code": {
"type": "keyword"
}
}
}
}
PUT /b3523-fixed/_doc/1
{
"event_day": "2026-11-26",
"quantity": "42",
"batch_code": "0042",
"optional": null,
"items": []
}
GET /b3523-fixed/_mapping
PUT /b3523-default/_mapping
{
"properties": {
"quantity": {
"type": "long"
}
}
}
# 实测摘要 新索引 event_day → date,quantity → long,batch_code → keyword;旧索引类型变更返回 HTTP 400。字段一旦有 mapping,后面的文档就沿用该类型。需要调整已有类型时,创建目标索引并设好 mapping,再用 _reindex 迁移。先确认历史值都能按新格式写入,核对迁移失败项、文档数和关键查询,再按现有读写方式切换别名。
实际排查时,先从写入响应和文档读取结果确认请求命中了哪个具体索引,再查该索引的 _mapping。字段缺失时看具体值和对象设置;类型意外时看首次写入的 JSON、date_detection、numeric_detection 和 dynamic_templates 的首条匹配规则。若索引由模板自动创建,可先用 POST /_index_template/_simulate_index/你的新索引名 查看待生效设置,并与现有索引比对。模板变更作用于之后创建的匹配索引。