Elasticsearch 集群怎么新增独立的机器学习节点?
2026-11-25 3471708
2026-11-25 0
给 Elasticsearch 日志索引建模时,先定义固定字段的类型,再让 flattened 负责键名变化的标签。设为 dynamic: false 后,未声明的顶层字段不会自动进入映射。以下 Elasticsearch 8.19.0 请求可在 Kibana Dev Tools → Console 中逐条执行,两个索引名需要尚未使用。
PUT /mapping-demo-safe
{
"mappings": {
"dynamic": false,
"properties": {
"service": {"type": "keyword"},
"latency_ms": {"type": "long"},
"labels": {"type": "flattened"}
}
}
}
PUT /mapping-demo-safe/_doc/1?refresh=true
{
"service": "checkout",
"latency_ms": 38,
"labels": {"tenant_17": "gold"},
"debug_note": "retry"
}
PUT /mapping-demo-safe/_doc/2?refresh=true
{
"service": "checkout",
"latency_ms": 71,
"labels": {"tenant_81": "silver"},
"debug_note": "slow"
}在普通动态对象里,每个新租户键都可能成为一个新映射路径。这里两个键共用 labels 的一项映射。把 latency_ms 声明为 long,可以按真实数值进行区间查询和聚合。
GET /mapping-demo-safe/_mapping
GET /mapping-demo-safe/_search
{
"query": {"term": {"labels.tenant_17": "gold"}}
}
GET /mapping-demo-safe/_count
{
"query": {"exists": {"field": "debug_note"}}
}查询普通字段与标签后,映射仍只有三项。租户 17 的金级标签命中第一条日志,额外调试字段的存在查询计数为 0。不过该文档的 _source 仍可看到 debug_note。

flattened 适合按已知键做精确匹配。在这个映射里,标签叶值按关键字处理,范围比较采用字典序。需要按数值或日期范围检索的属性,应提取到各自显式声明的字段。
下面把实验索引的字段上限设为 2,两个显式字段刚好用完额度。第一次写入尝试为 debug_note 新建动态映射,返回 HTTP 400。打开超限忽略后重发同一条文档,返回 HTTP 201。最后的查询用 _ignored 定位受影响的文档。
PUT /mapping-demo-limit
{
"settings": {"index.mapping.total_fields.limit": 2},
"mappings": {
"properties": {
"service": {"type": "keyword"},
"latency_ms": {"type": "long"}
}
}
}
PUT /mapping-demo-limit/_doc/1
{
"service": "checkout",
"latency_ms": 38,
"debug_note": "retry"
}
PUT /mapping-demo-limit/_settings
{
"index.mapping.total_fields.ignore_dynamic_beyond_limit": true
}
PUT /mapping-demo-limit/_doc/1?refresh=true
{
"service": "checkout",
"latency_ms": 38,
"debug_note": "retry"
}
GET /mapping-demo-limit/_search
{
"query": {"term": {"_ignored": "debug_note"}}
}默认字段上限为 1000,统计包含对象层级、普通字段、多字段、字段别名和显式或动态映射的运行时字段。例如常见的 text 加 .keyword 子字段会占两项。打开超限忽略意味着部分数据无法走字段索引查询,应监控 _ignored,并区分超限与其他被忽略的原因。批量写入时还要检查每个条目的状态。
生产中把映射放进命中业务索引的索引模板,用 POST /_index_template/_simulate_index/索引名 核对实际生效规则。字段集合有强约束时用 dynamic: strict,让意外字段直接触发写入错误。确实只需留存额外字段时,在普通索引上用 dynamic: false。可变键需要精确搜索时用 flattened。
旧映射的字段要靠迁移缩减。准备目标索引和模板,通过 _reindex 复制旧数据,再安排持续同步或短时间停写。完成条数、查询和写入检查后,用原子别名更新切换。
Elasticsearch 集群怎么新增独立的机器学习节点?
2026-11-25 3471708
Elasticsearch 节点加入时 cluster UUID 冲突,如何离线执行 detach-cluster?
2026-11-25 3471707
Elasticsearch 机器学习怎么启用?专用 ML 节点配置与验证
2026-11-25 3471706
Elasticsearch 7 升级 8 时,ML 作业怎么停复,节点怎么排序?
2026-11-25 3471700
Elasticsearch ES|QL 怎么用 INLINE STATS 保留日志明细和分组均值?
2026-11-25 3471699