[toc]


1. Introduction to Index Template

1. What is an index template?

Help you to set mappings and settings ; and automatically match to 新建的索引 according to certain rules;

2. Several questions about the index template

Q1: The index A has been newly created and the template M is used. Will it affect A after modifying M?

A: No! Templates only work when an index is created. Modifying the template does not affect indexes that have already been created.

Q2: Can multiple index templates be used?

A: Yes! Set up multiple index templates, these templates will be merge together.

Q3: When setting multiple index templates, what is the order in which they work?

A: You can specify the value of order merging . The big order is limited and works!

Second, the index template case

2.1 The role of two template definitions and index creation priorities

2.1.1 Template 1 sets when all indexes are created -> number of shards 1, number of copies 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 Template 2 sets the index at the beginning of test to set the main shard 1, the number of copies 2; turn off date recognition and turn on number recognition

  • Date recognition (date_detection) is: the date of the string, recognized as date (default is enabled)
  • Number recognition (numeric_detection) is: the number of the string, recognized as a number type (the default is off);
 #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 Create an index at the beginning of test and verify

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

Take a look at the effect:

 GET test_template_index/_mapping

Check out the results:

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

It can be seen that template 2 works: somDate is recognized as text instead of date; someNumber is recognized as long instead of text;

By default, somDate will be recognized as date and someNumber will be recognized as text !

Let's take a look at the number of shards and replicas , who worked:

 GET test_template_index/_settings

Look at the result: both are 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"
      }
    }
  }
}
It seems that this is 模板1 works, the order is selected!

So conclusion:

  • If multiple templates match, they will be enabled
  • Finally, merge all the configurations, the ones with large order will cover the ones in the small ones;

3. Introduction to Dynamic Template

3.1 What is a dynamic template?

Mainly used to dynamically set the field type: According to the data type identified by ES, combine with 字段名称 to dynamically set the field type.

Example:

  • Set all string types to keyword , or close the keyword subfield of text type;
  • The fields at the beginning of is are set to boolean ;
  • The fields at the beginning of long_ are set to long type;

3.2 Note on dynamic templates

  • It is defined in 某个索引 of mappings --> different from index template;
  • template has a name;
  • The matching rules are an array;
  • set mapping for match field;

3.3 Examples of dynamic templates

Specifies that the is* of an index is recognized as boolean; all strings are set to keyword

Example 1 - Dynamic template settings:

 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"
                    }
                }
            }
        ]
    }
}

Note: dynamic_templates is an array, where each object is a dynamic template, and each template has a custom name:

Example 1 - Dynamic Template Test:

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

View mapping:

 GET my_dynamic_template_index/_mapping

result:

 {
  "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"
        }
      }
    }
  }
}

You can see it works.


丰木
322 声望19 粉丝

遇见超乎想象的自己!