- A+
所属分类:全文检索
您可以通过为不同的索引名称指定不同的索引模板来实现这一点。例如,假设您有两种类型的客户数据:一种需要存储在单独的索引中,另一种需要存储在默认索引中。您可以创建两个索引模板:一个匹配需要单独索引的客户名称,另一个匹配其他客户名称。
以下是一个示例,展示如何使用两个索引模板来实现这个目标:
PUT _template/customer_template
{
"index_patterns": ["customer-*"],
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"timestamp": {
"type": "date"
}
}
},
"index_patterns": [
"customer-single-*"
],
"index": {
"routing": {
"required": true,
"path": "customer_id"
}
}
}
PUT _template/customer_template_default
{
"index_patterns": ["customer-*"],
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"timestamp": {
"type": "date"
}
}
},
"index_patterns": [
"customer-*"
]
}
在上面的示例中,第一个索引模板命名为 customer_template,匹配以 customer-single- 开头的索引名称,并定义了 routing 选项以便为每个索引设置路由。第二个索引模板命名为 customer_template_default,匹配所有以 customer- 开头的索引名称,但没有定义 routing 选项。因此,如果索引名称不以 customer-single- 开头,则会应用默认的索引模板。这样,您就可以为需要单独索引的客户数据和其他客户数据分别设置不同的索引模板。
index_patterns 是 Elasticsearch 索引模板中的一个参数,用于指定该模板应该匹配哪些索引名称模式。其规则如下:
- 支持通配符
*和?:*匹配零个或多个字符,?匹配任意单个字符。 - 支持使用逗号
,分隔多个模式,例如"index_patterns": ["logs-*", "app-*"]。 - 支持使用正则表达式匹配,例如
"index_patterns": ["logs-2019.*"]。
当创建索引时,Elasticsearch 会检查每个索引名称是否与所有已配置的索引模板的 index_patterns 参数匹配。如果匹配成功,则 Elasticsearch 将应用相应的索引设置、映射和别名等。
