[toc]


一、Index Template简介

1. 什么是index模板?

帮助你设定mappingssettings; 并按照一定的规则,自动匹配到新建的索引上;

2. 关于index模板的几个问题

Q1: 已新建了索引A使用了模板M,修改了M后会影响A吗?

A: 不会! 模板仅仅在一个索引被创建时,才产生作用。修改模板不会影响已经创建的索引。

Q2: 可否设定使用多个index模板?

A: 可以! 设定多个索引模板,这些模板会被merge在一起。

Q3: 设定多个index模板,起作用是按什么顺序?

A: 你可以指定order的数值,控制merging的过程。order大的有限起作用!

二、 index template案例

2.1 两个模板定义和创建索引的作用优先级

2.1.1 模板1设定所有索引创建时->分片数1,副本数1

# 1. 所有索引:分片数1,副本数1
PUT _template/my_template
{
  "index_patterns": ["*"],
  "order": 10, // order大的优先
  "version": 1,
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

2.1.2 模板2设定test开头的索引设置主分片1,副本数2; 关闭date识别,开启数字识别

  • date识别(date_detection)就是:字符串的日期,识别为date(默认是开启的)
  • 数字识别(numeric_detection)就是:字符串的数字,识别为数字类型(默认是关闭的);
#2.test开头的索引设置主分片1,副本数2; 关闭date识别,开启数字识别
PUT _template/my_template_test
{
  "index_patterns": ["test*"],
  "order": 1,
  "settings":{
    "number_of_shards": 1,
    "number_of_replicas": 2
  },
  "mappings":{
    "date_detection": false,
    "numeric_detection": true
  }
}

2.1.3 创建test开头的索引,验证

PUT test_template_index/_doc/1
{
  "someNumber": "1",
  "somDate": "2019/01/01"
}

看看作用效果:

GET test_template_index/_mapping

看看结果:

{
  "test_template_index" : {
    "mappings" : {
      "date_detection" : false,
      "numeric_detection" : true,
      "properties" : {
        "somDate" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "someNumber" : {
          "type" : "long"
        }
      }
    }
  }
}

可见模板2起作用了: somDate被识别为text而非date; someNumber被识别为long而非text了;

默认的话,somDate是会被识别为date, someNumber会被识别为text的!

我们再看看关于shardsreplicas的数量,谁的起作用了:

GET test_template_index/_settings

看看结果:两个都是1

{
  "test_template_index" : {
    "settings" : {
      "index" : {
        "creation_date" : "1650989910135",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "HdvbZrC8SwCH9xA3CbzFxg",
        "version" : {
          "created" : "7050299"
        },
        "provided_name" : "test_template_index"
      }
    }
  }
}
看来这个是模板1起作用了,order大的被选择了!

所以结论:

  • 多个模板匹配的话,都会启用
  • 最后将所有配置merge, order大的会覆盖小的中的配置;

三、Dynamic Template简介

3.1 什么是动态模板?

主要用来动态设定字段类型:根据ES识别的数据类型,结合字段名称来动态设定字段类型。

举例:

  • 设置所有字符串类型都为keyword, 或者关闭text类型的keyword子字段;
  • is开头的字段都设定为boolean;
  • long_开头的字段都设定为long类型;

3.2 动态模板注意

  • 是定义在某个索引mappings中--> 区别于index template;
  • template有一个名称;
  • 匹配规则为一个数组;
  • 为匹配字段设置 mapping;

3.3 动态模板的例子

指定某个索引的is*识别为boolean;字符串都设为keyword

例1-动态模板设定:

PUT my_dynamic_template_index
{
    "mappings": {
        "dynamic_templates": [
            {
                "is_x_to_boolean": { // 自定义名称
                    "match_mapping_type": "string",
                    "match": "is*",
                    "mapping": {
                        "type": "boolean"
                    }
                }
            },
            {
                "string_to_keyword": { // 自定义名称
                    "match_mapping_type": "string",
                    "mapping": {
                        "type": "keyword"
                    }
                }
            }
        ]
    }
}

注意: dynamic_templates是个数组,其中每个对象是一个动态模板,每个模板有一个自定义的名字:

例1-动态模板测试:

PUT my_dynamic_template_index/_doc/1
{
  "firstName":"Nie", "isVip":"true"
}

查看mapping:

GET my_dynamic_template_index/_mapping

结果:

{
  "my_dynamic_template_index" : {
    "mappings" : {
      "dynamic_templates" : [
        {
          "is_x_to_boolean" : {
            "match" : "is*",
            "match_mapping_type" : "string",
            "mapping" : {
              "type" : "boolean"
            }
          }
        },
        {
          "string_to_keyword" : {
            "match_mapping_type" : "string",
            "mapping" : {
              "type" : "keyword"
            }
          }
        }
      ],
      "properties" : {
        "firstName" : {
          "type" : "keyword"
        },
        "isVip" : {
          "type" : "boolean"
        }
      }
    }
  }
}

可以看到起作用了。


丰木
322 声望19 粉丝

遇见超乎想象的自己!