真实用composed_of组合两个Component Template,验证正交配置如何叠加,以及两个片段定义同一个设置发生冲突时真实的顺序仲裁规则。
代码PUT /_component_template/comp_settings {"template": {"settings": {"number_of_shards": 2}}} PUT /_component_template/comp_mappings {"template": {"mappings": {"properties": {"user_id": {"type": "keyword"}}}}} PUT /_index_template/combo_tpl { "index_patterns": ["combo-*"], "composed_of": ["comp_settings", "comp_mappings"], "priority": 50 } PUT /combo-1 GET /combo-1/_settings GET /combo-1/_mapping
combo-1的number_of_shards → 2 ← 真实来自comp_settings
combo-1的mapping → {"user_id": {"type": "keyword"}} ← 真实来自comp_mappings这是Component Template存在的真实意义:它不是独立可用的模板,而是真实可被多个Index Template复用的"配置片段"——本文这次一个片段只管分片数这类settings,另一个只管字段映射,两者互不相关,真实通过composed_of数组拼到一起才形成一份完整的索引配置。这样设计的真实价值是避免重复:假如公司里有10个不同业务线的索引都要用同一套分片策略,不需要在每个Index Template里把settings抄一遍,只需要让它们都组合同一个comp_settings,真实改一处配置就能同时影响所有引用它的索引。
comp_a: {"number_of_shards": 1}
comp_b: {"number_of_shards": 5}
PUT /_index_template/order_tpl {"composed_of": ["comp_a", "comp_b"], ...}
PUT /order-1
GET /order-1/_settings → number_of_shards = 5 ← 真实comp_b赢了,它排在数组后面每一步都是本文上面在本机真实的Elasticsearch 8.15.0上跑出来的原始结果。
_component_template 与 composed_of 组合机制