#####################20200527dynamicTemplate&indexTemplate####################
es中会有很多的模板,当我们创建新的索引的时候,如果我们之前指定了模板,那么他会按照模板进行对索引的mapping 或者 settings 进行设置,如果我们在创建索引的时候,有自定义设置mapping和settings 那么他就不会使用模板,设置模板可以让我们不必每次创建索引的时候,都指定一下固定的设置
创建一个模板,模板名称为template_default 他的匹配路径是* 意味着所有的索引在创建时,都会额外增加一个settings
PUT _template/template_default
{
"index_patterns": ["*"],
"order" : 0,
"version": 1,
"settings": {
"number_of_shards": 3,
"number_of_replicas":1
}
}
直接添加一条数据,因为索引不存在,所以他会使用es的dynamic mapping 自动的创建索引
PUT test_index/_doc/1
{
"number": "1",
"date": "2020/01/01"
}
在我们添加数据的时候,他会把我们的number数据 映射为text date 映射为日期类型,但如果我们想要他进行dynamic mapping 映射的时候,更加准确的映射,例如 将字符串的数字映射为long 将字符串的日期作为字符串保存等等。那么我们也可以通过indexTemplate完成
GET test_index/_mapping
查看模板列表
GET _cat/templates
GET _template/logstash
indexTemplate的工作流程
当一个索引被创建时,他会应用es默认的settings和mappings(匹配模板),先应用order(顺序)数值低的indexTemplate中的设定,然后应用order高indexTemplate 之前的设定会被覆盖,如果我们在创建索引时,有单独指定settings 和mappings 就会覆盖模板中的设置
创建一个模板
模板含义: 模板的名字为template_test 他会匹配以test开头的所有模板,他的order(顺序)值为1,比他分低的模板会先被应用,他会覆盖设置地方模板的设置,该模板设定 新增的test* index 中 主分片为1.每个主分片都有两副本分片。 并且关闭了date 类型的dynamic设置,字符串类型的日期不会被转换,打开了日期类型的设置,字符串类型的数字,会被映射为long类型
PUT _template/template_test
{
"index_patterns": ["test*"],
"order": 1,
"settings": {
"number_of_shards": 1,
"number_of_replicas": 2
},
"mappings": {
"date_detection": false,
"numeric_detection": true
}
}
GET _template/template_test
新增一个索引,测试testTemplate
PUT test_index2/_doc/1
{
"number": "1",
"date": "2020/01/01"
}
查看映射发现,我们的新索引中date被映射为字符串,number被映射为long
GET test_index2/_mapping
GET test_index2/_search
并且test_index2 中的settings 是textTemplate中的设置,他以及将*的template进行了覆盖
GET test_index2/_settings
查看template的信息
GET _template/template_default
查看template的信息时也可以通过通配符匹配查询
GET _template/temp*
创建一个索引,并且指定他的settings
PUT test_index3
{
"settings": {
"number_of_replicas": 5
}
}
PUT test_index3/_doc/1
{
"key": "value"
}
因为我们有单独指定settings 所以即使他匹配了模板,也会按照我们的settings进行覆盖设置
GET test_index3/_settings
GET test_index3/_mapping
dynamic mapping在映射的时候往往仍然会不尽人意,所以我们可以通过 dynamicTemplate 进行设置,让dynamicMapping在进行映射的时候更加满足我们的预期 dynamicTemplate 是定义在某个具体的索引的mappings中的 每一个template都又一个名词,匹配规则是一个数组,他会为匹配到的字段设置mapping
创建一个索引 设置 name下的所有 不以middle结尾的字段 的mapping都为text 并且copy一份到 full_name下
PUT my_test_index
{
"mappings": {
"dynamic_templates": [
{
"full_name": {
"path_match": "name.*",
"path_unmatch": "*.middle",
"mapping": {
"type": "text",
"copy_to": "full_name"
}
}
}
]
}
}
根据我们的dynamicTemplate映射设置 所有符合条件的数据都设置为了 text 并且copy_to到full_name下
PUT my_test_index/_doc/1
{
"name": {
"first": "John",
"middle": "Winston",
"last": "Lennon"
}
}
GET my_test_index/_mapping
GET my_test_index/_settings
GET my_test_index/_search
创建一个索引,匹配所有的字符串中以is开头的字段,他们的mapping类型设置Boolean 其他的所有字符串的类型设置为keyword
PUT my_index
{
"mappings": {
"dynamic_templates": [
{
"strings_as_boolean": {
"match_mapping_type": "string",
"match": "is*",
"mapping": {
"type": "boolean"
}
}
},
{
"strings_as_keywords": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
}
## 测试
PUT my_index/_doc/1
{
"firstName":"raven",
"isVIP":"true"
}
因为我们设置了dynamic_templates 所以 字符串格式的数据的type为keyword 而is开头的字符串格式的数据为 Boolean
GET my_index/_mapping