索引模板:Index Template


# 索引模板:Index Template

# 前言

索引模板:就是把已经创建好的某个索引的参数设置(settings)和索引映射(mappings)保存下来作为模板,在创建新索引时,ES 根据 index 名称去匹配相应的模板,就可以直接重用已经定义好的模板中的设置和映射。

  • 模板仅在一个索引被新创建时,才会产生作用,修改模板不会影响已创建的索引。
  • 可以设定多个索引模板,这些设置会被 merge 在一起。
  • 可以指定 order 的数值,控制 merging 的过程。
  • create index API (opens new window) 请求中指定的设置和映射会覆盖索引模板中指定的配置。

索引模板一般用在时间序列相关的索引中,也就是说,如果你需要每间隔一定的时间就建立一次索引,那么只需要配置好索引模板,以后就可以直接使用这个模板中的设置,不用每次都设置 settings 和 mappings。

客户端是时候为另外一个月生成 index 了syslog-2022.03syslog-2022.02syslog-2022.01使用一个index template 将简化这个过程

(时序索引很适合用索引模板)

# 工作方式

当一个索引被创建时,会依次经历大致如下四个过程:

  • 使用 ES 默认的 settings 和 mappings
  • 使用 order 数值低的 Index Template 中的设定
  • 使用 order 数值高的 Index Template 中的设定,之前的设定会被覆盖
  • 使用创建索引时,用户所指定的 settings 和 mappings,并覆盖之前模板的设定

# 创建索引模板

可以使用如下的接口来定义一个 index template:

PUT /_template/<index-template>
1

实际例子:

PUT _template/syslog_template
{
  "index_patterns": "syslog-*",
  "order": 1, 
  "settings": {
    "number_of_shards": 4,
    "number_of_replicas": 1
  },
  "mappings": { 
    "properties": {
      "@timestamp": {
        "type": "date"
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

注意 order 的值越大,权重越大,最后更有可能使用它(除非被 create index API 覆盖)。

# 索引匹配多个模板

一个索引可能同时匹配上了索引模板,在这种情况下,设置和映射都合并到索引的最终配置中。可以使用 order 参数控制合并的顺序,首先应用较低的顺序,并且覆盖它们的较高顺序。例如:

PUT /_template/template_1
{
  "index_patterns": ["*"],
  "order": 0,
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_source": {
      "enabled": false
    }
  }
}

PUT /_template/template_2
{
  "index_patterns": ["te*"],
  "order": 1,
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "_source": {
      "enabled": true
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

以上的 template_1 将禁用存储 _source,但对于以 te* 开头的索引,仍将启用 _source

创建一个例子看看:










 















PUT test10

GET test10


# 显示结果
{
  "test10" : {
    "aliases" : { },
    "mappings" : { },  # 空白就是 true 的意思
    "settings" : {
      "index" : {
        "creation_date" : "1658469249000",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "iEwaQFl9RAKyTt79PduN-Q",
        "version" : {
          "created" : "7030099"
        },
        "provided_name" : "test10"
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

再创建一个不是以 te 开头的 index,就是另外一种情况了:












 

















PUT my_test_index

GET my_test_index


# 显示结果
{
  "my_test_index" : {
    "aliases" : { },
    "mappings" : {  # 显然在 mappings 里显示 source 是被禁止的
      "_source" : {
        "enabled" : false
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1658469249000",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "aSsIZMT2RyWKT44G2dF2zg",
        "version" : {
          "created" : "7030099"
        },
        "provided_name" : "my_test_index"
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

如果对于两个 templates 来说,如果 order 是一样的话,我们可能陷于一种不可知论的合并状态,在实际的使用中必须避免。

# 查看索引模板

GET _template                # 查看所有模板
GET _template/temp*          # 查看与通配符相匹配的模板
GET _template/temp1,temp2    # 查看多个模板
GET _template/my_template    # 查看指定模板
1
2
3
4

# 判断模板是否存在

HEAD _template/my_template
1
  • 如果存在, 响应结果是: 200 - OK
  • 如果不存在, 响应结果是: 404 - Not Found

# 删除索引模板

DELETE _template/my_template
1

# 参考资料

(完)