1.模版组件
可以把常规的索引设置,映射等内容写成可复用的模板组件,然后在索引模板中引用这些组件。
这样模板中的配置内容就会非常简洁。
2.使用模板组件
#创建一个组件模板:
curl -u elastic:elastic -k -XPUT "http://192.168.1.800:9200/_component_template/comp1 " -H 'Content-Type: application/json' -d '
{
"template":{
"mappings":{
"properties":{
"content":{"type":"text"}
}
}
}
}'
#再创建一个组件模板,配置了别名 loginfo,主分片数为3,每个主分片的副本数2;
curl -u elastic:elastic -k -XPUT "http://192.168.1.800:9200/_component_template/comp2" -H 'Content-Type: application/json' -d '
{
"template":{
"settings":{
"number_of_shards":"3",
"number_of_replicas":"2"
},
"aliases":{"loginfo":{}}
}
}
}'
#创建一个索引模板引用组件模板。
#curl -u elastic:elastic -k -XDELETE "http://192.168.1.800:9200/_index_template/info_model"
curl -u elastic:elastic -k -XPUT "http://192.168.1.800:9200/_index_template/info_model" -H 'Content-Type: application/json' -d '
{
"index_patterns":["loginfo*"],
"priority":200,
"composed_of":["comp1","comp2"]
}'
#创建索引
curl -u elastic:elastic -k -XDELETE "http://192.168.1.800:9200/loginfo1"
curl -u elastic:elastic -k -XPUT "http://192.168.1.800:9200/loginfo1"
curl -u elastic:elastic -k -XGET "http://192.168.1.800:9200/loginfo1"
{
"loginfo1":{
"aliases":{
"loginfo":{
}
},
"mappings":{
"properties":{
"content":{
"type":"text"
}
}
},
"settings":{
"index":{
"routing":{
"allocation":{
"include":{
"_tier_preference":"data_content"
}
}
},
"number_of_shards":"3",
"provided_name":"loginfo1",
"creation_date":"1743661458187",
"number_of_replicas":"2",
"uuid":"6A2UqFGYT2eNHHG0_g52XQ",
"version":{
"created":"7170099"
}
}
}
}
}
由此可见:comp1,comp2两个组件模板在内容都继承了
#删除索引模版
curl -u elastic:elastic -k -XDELETE "http://192.168.1.800:9200/_index_template/info_model"
#删除组件模版:
curl -u elastic:elastic -k -XDELETE "http://192.168.1.800:9200/_component_template/comp1"
curl -u elastic:elastic -k -XDELETE "http://192.168.1.800:9200/_component_template/comp2"
注意:
当多个组件模版中存在重复的配置内容时,后面的组件模版会覆盖前面的组件模版内容。
如果一个组件模版正在被某个索引模版引用,则这个组件模版不可以被删除。