文章目录
- 前言
- 一、 创建一个新的索引模板
- 二、 查看索引模板
- 1、 查看所有模板
- 2、 查看指定的模板
- 3、 模糊匹配
- 4、 批量查询
- 三、 删除指定模板
- 四、修改模板(相当于整体替换,重置)
- 五、根据索引模板创建索引
前言
首先这里是关于ES2中对于索引模板的操作记录,对于es6及以上对于索引模板的操作和结构可以看下面的的链接
elastic中es6教程关于索引模板的部分
一、 创建一个新的索引模板
put _template/模板名称
{
"template": "索引模板前缀*", //匹配的索引名字
"order": 3, //代表权重,如果有多个模板的时候,优先进行匹配,值越大,权重越高
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"_default_": { //默认的配置
"_source": { //只关心度量结果,不是原始文件内容
"enabled": false
},
"_all": { //你确切地知道你对哪个 field 做查询操作
"enabled": false
},
"dynamic": "strict" //数据是结构化数据。字段设置严格,避免脏数据注入
},
"doc": {
"_source": {
"enabled": false
},
"properties": {
"字段名": {
"type": "字段类型"
},
"字段名": {
"type": "字段类型"
},
"字段名": {
"type": "字段类型"
}
}
}
}
}
上面索引模板结构的解释可以看下面两个,结构都是差不多的
elastic中es2教程关于索引模板的部分es5中关于索引模板的介绍
索引模板还有别名,但是我是一般不用,所以就不写了
二、 查看索引模板
1、 查看所有模板
get _template
{}
2、 查看指定的模板
get _template/模板名
{}
3、 模糊匹配
get _template/模板名前缀 + *
{}
4、 批量查询
get /_template/模板名1,模板名2
{}
三、 删除指定模板
delete _template/模板名
{}
四、修改模板(相当于整体替换,重置)
post _template/模板名
{
"template": "索引模板前缀*",
"order": 3,
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"_default_": {
"_source": {
"enabled": false
},
"_all": {
"enabled": false
},
"dynamic": "strict"
},
"doc": {
"_source": {
"enabled": false
},
"properties": {
"字段名": {
"type": "字段类型"
},
"字段名": {
"type": "字段类型"
},
"字段名": {
"type": "字段类型"
}
}
}
}
五、根据索引模板创建索引
首先创建一个 example* 的索引模板,再根据上面创建索引的方式创建一个 example_2010 索引,这样如果不设置字段,则字段就会和索引模板上的一致,如果在创建索引时加了字段,则字段会合并,取并集