Elasticsearch-参考指南索引模板

11次阅读

共计 1988 个字符,预计需要花费 5 分钟才能阅读完成。

索引模板

索引模板允许你定义在创建新索引时自动应用的模板,模板包括设置和映射,以及一个简单的模式模板,该模板控制是否应该将模板应用于新索引。

模板只在创建索引时应用,更改模板不会对现有索引产生影响,当使用创建索引 API 时,作为创建索引调用的一部分定义的设置 / 映射将优先于模板中定义的任何匹配设置 / 映射。

例如:

PUT _template/template_1
{"index_patterns": ["te*", "bar*"],
  "settings": {"number_of_shards": 1},
  "mappings": {
    "_source": {"enabled": false},
    "properties": {
      "host_name": {"type": "keyword"},
      "created_at": {
        "type": "date",
        "format": "EEE MMM dd HH:mm:ss Z yyyy"
      }
    }
  }
}

索引模板提供 c 样式 /* */ 块注释,在 JSON 文档中,除了初始左大括号之前,其他地方都允许使用注释。

定义一个名为 template_1 的模板,模板模式为 te*bar*,设置和映射将应用于任何匹配 te*bar*模式的索引名称。

也可以在索引模板中包含以下别名:

PUT _template/template_1
{"index_patterns" : ["te*"],
    "settings" : {"number_of_shards" : 1},
    "aliases" : {"alias1" : {},
        "alias2" : {
            "filter" : {"term" : {"user" : "kimchy"}
            },
            "routing" : "kimchy"
        },
        "{index}-alias" : {}}
}

别名中的 {index} 占位符将被替换为模板在创建索引期间应用到的实际索引名。

删除一个模板

索引模板由一个名称标识(在上面的例子中是template_1),也可以删除:

DELETE /_template/template_1

获取模版

索引模板由一个名称标识(在上面的例子中是template_1),可以使用以下方法检索:

GET /_template/template_1

还可以使用通配符匹配多个模板,如:

GET /_template/temp*
GET /_template/template_1,template_2

获取可运行的所有索引模板的列表:

GET /_template

模板存在

用于检查模板是否存在,例如:

HEAD _template/template_1

HTTP 状态码指示具有给定名称的模板是否存在,状态码 200 表示存在,404表示不存在。

在 7.0.0 之前,映射定义用于包含类型名称,虽然默认情况下映射不再包含类型名称,但是仍然可以通过设置参数 include_type_name 使用旧格式。

多模板匹配

多个索引模板可能匹配一个索引,在本例中,设置和映射都合并到索引的最终配置中,可以使用 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}
    }
}

上面的操作将禁用存储 _source,但是对于以te* 开头的索引,仍然启用_source,注意,对于映射,合并是“深度”的,这意味着基于对象 / 属性的映射可以很容易地在高阶模板上添加 / 覆盖,而低阶模板提供了基础。

具有相同顺序值的多个匹配模板将导致不确定的合并顺序。

模板版本控制

为了简化外部系统对模板的管理,模板可以选择添加一个 version 号,版本号可以是任何整数,version字段是完全可选的,仅用于模板的外部管理,要取消 version 设置,只需替换模板而不用指定另一个。

PUT /_template/template_1
{"index_patterns" : ["*"],
    "order" : 0,
    "settings" : {"number_of_shards" : 1},
    "version": 123
}

要检查版本,可以使用 filter_path 过滤响应,将响应限制为只有version

GET /_template/template_1?filter_path=*.version

这应该会给出一个小的响应,使解析既简单又便宜:

{
  "template_1" : {"version" : 123}
}

正文完
 0