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

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

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


查看

 

创建完索引模板或组件模板后,我们可以使用 GET 请求再次查看其内容,请求是可以指定名称表示精准找,也可以使用通配符*进行范围查找,如果不指定名称则会返回全部。

 

GET /_component_template/template_1?local=false&master_timeout=30s #1

GET /_component_template/template_* #2

GET /_component_template #3

GET /_index_template/test_template #4

GET /_index_template/test_* #5

GET /_index_template #6

 

1、#1 是查看名为template_1的组件模板

2、#2 是查看名字以template_开头的所有组件模板

3#3 是查看所有组件模板,通过该请求我们可以发现 Elasticsearch 默认创建了很多组件模板,使用时应尽量避免冲突

4#4 是查看名字为test_template的索引模板

5#5 是查看名字以test_开头的所有索引模板

6、#6 是查看所有索引模板,通过该请求我们可以发现 Elasticsearch 默认创建了很多索引模板,使用时应尽量避免冲突

 

如 #1 所示,上述所有请求都可以增加 2 个可选的查询参数:

 

1、local,如果为true模板的配置仅从本地节点中获取,默认为false表示此次查询结果是

master节点返回的

2、master_timeout,表示可以容忍的连接elasticsearch主节点的时间,默认是30s,如果超时则请求报错

 

使用

 

关于组件模板如何使用我们在创建索引模板时已经提及,增加composed_of字段声明索引模板

引用的组件模板,这样组件模板的配置,就自动增加到了索引模板中。

 

索引模板的使用主要发生在创建索引的时候,如果创建的索引名与索引模板的index_patterns

配置相匹配,那么该索引将会在此模板的基础上创建。

 

示例如下:


# 创建名为test-my-template的索引
PUT /test-my-template
{
  "settings": {
    "index": {
      "number_of_replicas": 3
    }
  },
  "mappings": {
    "properties": {
      "desc":{
        "type": "keyword"
      }
    }
  }
}
# 查看索引信息
GET /test-my-template
# 返回
{
  "test-my-template" : {
    "aliases" : {          # 组件模板 template_1 的配置
      "test-index" : { }
    },
"mappings" : {
      "_source" : {         # 组件模板 template_1 的配置
        "enabled" : false
      },
      "properties" : {
        "desc" : {          # 创建索引时的配置
          "type" : "keyword"
        }, 
        "name" : {          # 组件模板 template_1 的配置
          "type" : "keyword"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "3",   # 覆盖了 索引模板 number_of_shards=2 的配置
        "provided_name" : "test-my-template",
        "creation_date" : "1620550940095",
        "number_of_replicas" : "1",
        "uuid" : "7UZKxK56SZ-bAwBeDL-Jvg",
        "version" : {
          "created" : "7100099"
        }
      }
    }
  }
}

本例我们创建了名为test-my-template的索引,其索引名以te开头,匹配索引模板test_template的筛选规则。我们在创建时仅指定了分片数和一个desc属性,而在我们使用 GET 请求查看索引信息时可以看出,索引模板中的别名配置、_source配置和name属性的配置被使用了。但是关于分片的设置,由于创建索引时明确指出了,所以模板中的配置number_of_shards=2被覆盖为了 3。

 

由于在向一个不存在的索引中插入数据时,Elasticsearch 会自动创建索引,如果新的索引名匹配某个索引模板的话,也会以模板配置来创建索引。在使用该功能前,请确保 Elasticsearch集群的action.auto_create_index配置是允许你自动创建目标索引的。

 

示例如下:


# 向不存在的 test 索引中保存1条数据
PUT /test/_doc/1
{
  "name":"tom",
  "age": 18
}
# 查看 test 索引信息
GET /test
{
  "test" : {
    "aliases" : {         # 组件模板 template_1 的配置
      "test-index" : { }
    },
    "mappings" : {
      "_source" : {       # 组件模板 template_1 的配置
        "enabled" : false 
      },
      "properties" : {
        "age" : {         # ES 自动推断创建的属性
          "type" : "long"
        },
        "name" : {       # 组件模板 template_1 的配置
          "type" : "keyword"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "2",     # 使用索引模板的配置
        "provided_name" : "test",
        "creation_date" : "1620551933756",
        "number_of_replicas" : "1",
        "uuid" : "Oop40MPySjKbSKvs0OQwTA",
        "version" : {
          "created" : "7100099"
        }
      }
    }
  }
}

如上例所示,我们向不存在的test索引中保存 1 条数据,该索引名与索引模板test_template的规则相匹配。因此索引的配置与模板配置完全契合,除了模板中没有配置的age字段,是通过 Elasticsearch 属性自动创建功能生成的。


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

 

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