关于elasticsearch:实践004elasticsearch之Index-Template和Dynamic-Template

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

能够看到起作用了。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理