带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(7)

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(7)

《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.8.Index template(6) https://developer.aliyun.com/article/1230795


本例我们创建了名为bar-test-old的索引,其索引名以bar开头,匹配索引模板old_template的筛选规则,我们在创建时仅指定了副本数和 1 个ip属性,而使用 GET 请求查看索引信息时可以看出,索引模板中的别名配置、host_name属性和分片数的配置被使用了。但是关于_source的设置,由于创建索引时明确指出了,所以模板中的false配置被覆盖为了true

 

当匹配到多个老版索引模板时,最终配置为多个模板的组合,当不同模板配置了相同字段时,那么以order高的模板配置为准。示例如下:

 

# 创建新版索引模板   
PUT /_index_template/new_template   #1
{
   "index_patterns": ["template*"],
   "priority" : 100, 
   "template": {
    "mappings": {
      "dynamic": "true"
    },
    "aliases": {
      "new-template": {}
    }
  }
}
# 创建老版索引模板,不配置 order
PUT /_template/old_template_1     #2
{
  "index_patterns": ["temp*"],
   "mappings": {
      "dynamic": "false"
    },
  "aliases": {
      "old_template_1": {}
    }
}
# 创建老版本索引模板,配置高 order
PUT /_template/old_template_2?order=10  #3
{
  "index_patterns": ["temp-*"],
   "mappings": {
      "dynamic": "strict"
    },
  "aliases": {
      "old_template_2": {}
    }
}
# 创建索引,名称会匹配 new_template 模板和 old_template_1 模板
PUT /template-test    #4
{
  "settings": {
    "number_of_shards": 2
  },
  "mappings": {
    "properties": {
      "ip":{
        "type": "ip"
      }
    }
  }
}
# 查看索引配置
GET  /template-test  #5
# 返回
{
  "template-test" : {
    "aliases" : {
      "new-template" : { }  #  仅有新版本模板的别名设置
    },
    "mappings" : {
      "dynamic" : "true",
      "properties" : {
        "ip" : {
          "type" : "ip"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "2",
        "provided_name" : "template-test",
        "creation_date" : "1620617781794",
        "number_of_replicas" : "1",
        "uuid" : "0PKYANozRya9zG3LdMG1UA",
        "version" : {
          "created" : "7100099"
        }
      }
    }
  }
}
# 创建索引,名称会匹配 old_template_1 模板和 old_template_1 模板
PUT /temp-test #6
{
  "settings": {
    "number_of_shards": 2
  },
  "mappings": {
    "properties": {
      "ip":{
        "type": "ip"
      }
    }
  }
}
# 查看索引配置
GET /temp-test
# 返回
{
  "temp-test" : {
    "aliases" : {
      "old_template_1" : { },  # old_template_1 的配置
      "old_template_2" : { }    # old_template_2 的配置
    },
    "mappings" : {
      "dynamic" : "strict",  # old_template_2 的配置
      "properties" : {
        "ip" : {
          "type" : "ip"
        }
      }
    },
    "settings" : {
        "refresh_interval" : "10s",
        "number_of_shards" : "2",
        "provided_name" : "temp-test",
        "creation_date" : "1620617979932",
        "number_of_replicas" : "1",
        "uuid" : "RwvrdGziT7iVmVutXYPwqA",
        "version" : {
          "created" : "7100099"
        }
      }
    }
  }
}

1、#1 处我们创建了 1 个新版本的索引模板匹配名字,以template开头的索引,设置可以动态增加属性并设置别名new-template


2、#2 处我们创建了 1 个老版本的索引模板匹配名字,以temp开头的索引,设置不动态增加属性并设置别名old_template_1

3、#3 处我们创建了 1 个老版本的索引模板匹配名字,以temp-开头的索引,设置有新增属性时报错并设置别名old_template_2,同时配置order为10

4、#4 处我们创建了 1 个名为template-test的索引

5、#5 处查看索引template-test的配置,该名称会匹配新版new_template模板,和老版

old_template_1模板,根据索引的别名信息,只有新版模板配置的别名可以看出,该索引仅仅应用了new_template模板

6、#6 处创建了 1 个名为temp-test的索引

7、#7 处查看索引temp-test的配置,该名称会匹配老版old_template_1old_template_2模板,根据索引的别名信息有old_template_1old_template_1可以看出,2 个模板的配置都应用了。通过dynamic字段为strict我们可以判断该字段使用了order较高的模板old_template_2的配置

 


《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.8.Index template(8) https://developer.aliyun.com/article/1230793

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
12月前
|
API 索引
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(3)
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(3)
|
12月前
|
索引
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(5)
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(5)
|
12月前
|
索引
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(4)
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(4)
|
12月前
|
Java 大数据 索引
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(8)
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(8)
|
12月前
|
索引
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(1)
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(1)
|
12月前
|
JSON 数据格式 索引
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(6)
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(6)
|
12月前
|
JSON 安全 API
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(2)
带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(2)
|
12月前
|
JSON 数据格式
带你读《Elastic Stack 实战手册》之24:——3.4.2.9.Search template(4)
带你读《Elastic Stack 实战手册》之24:——3.4.2.9.Search template(4)
|
12月前
|
编解码 JSON Java
带你读《Elastic Stack 实战手册》之24:——3.4.2.9.Search template(5)
带你读《Elastic Stack 实战手册》之24:——3.4.2.9.Search template(5)
|
12月前
|
JSON 安全 API
带你读《Elastic Stack 实战手册》之24:——3.4.2.9.Search template(2)
带你读《Elastic Stack 实战手册》之24:——3.4.2.9.Search template(2)