Elasticsearch之索引模板index template与索引别名index alias

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介:

为什么需要索引模板?

      在实际工作中针对一批大量数据存储的时候需要使用多个索引库,如果手工指定每个索引库的配置信息(settings和mappings)的话就很麻烦了。

所以,这个时候,就存在创建索引模板的必要了!!1

  索引可使用预定义的模板进行创建,这个模板称作Index templates。模板设置包括settings和mappings通过模式匹配的方式使得多个索引重用一个模板。

  更多,请见

https://www.elastic.co/guide/en/elasticsearch/reference/2.4/indices-templates.html

 

索引别名的应用场景

  比如,公司使用es收集应用的运行日志,每个星期创建一个索引库,这样时间长了就会创建很多的索引库,操作和管理的时候很不方便。

  由于新增索引数据只会操作当前这个星期的索引库,所以就创建了两个别名。

    curr_week:这个别名指向这个星期的索引库,新增数据操作这个索引库。

    last_3_month:这个别名指向最近三个月的所有索引库,因为我们的需求是查询最近三个月的日志信息。

  后期只需要修改这两个别名和索引库之间的指向关系即可。应用层代码不需要任何改动。

  还要把三个月以前的索引库close掉,留存最近一年的日志数据,一年以前的数据删除掉。

   说明:可以类似,指定多个索引库查询。定义一个索引别名,如zhouls_all,将索引zhouls1映射到这个别名上,把索引zhouls2,把索引zhoulsn,也映射到这个别名上。

  那么,在通过别名来查询时,直接同查询别名zhouls_all,就可以把对应所有的索引zhouls,1,2,...n都一次性查询完了。

   但是,如果你是具体要插入和操作数据,则,就不方便使用别名了。而是具体到某个索引zhoulsn了。

 

 

 

 

 

 

 

 

 

 

 

 

 

 索引模板index template操作示例

  比如,我们会创建zhouls,zhouls1,zhouls2,,,等这样的索引库。那么,我们创建模板索引库指定是zhouls*。

那么,

  1创建自定义模板

  在es的安装目录下,输入,如下,回车

复制代码
curl -XPUT 192.168.80.10:9200/_template/template_1 -d '
{
    "template" : "zhouls*",
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false }
        }
    }
}
'
复制代码

   说明:  (1)模板template_1匹配所有的以zhouls开头的索引。

      (2)索引模板是template_1,索引是zhouls*。

    

 

 

 

 

 

 

[hadoop@HadoopMaster elasticsearch-2.4.3]$ curl -XPUT '192.168.80.10:9200/zhouls10/emp/1' -d '{"name":"zs"}'       (给索引zhouls10赋值)
{"_index":"zhouls10","_type":"emp","_id":"1","_version":1,"_shards":{"total":2,"successful":2,"failed":0},"created":true}[hadoop@HadoopMaster elasticsearch-2.4.3]$ 
[hadoop@HadoopMaster elasticsearch-2.4.3]$ 
[hadoop@HadoopMaster elasticsearch-2.4.3]$ curl -XGET http://192.168.80.10:9200/zhouls10/_settings?pretty    
{
"zhouls10" : {
"settings" : {
"index" : {
"creation_date" : "1488280491485",
"uuid" : "R4dWmru2T9uO1JFOE98r5w",
"number_of_replicas" : "1",
"number_of_shards" : "1",
"version" : {
"created" : "2040399"
}
}
}
}
}
[hadoop@HadoopMaster elasticsearch-2.4.3]$

 

 

   

 

 

 

 

  2、查看定义的模板

curl -XGET 192.168.80.10:9200/_template/template_1 

 

     我这里,创建定义模板temp*就省略了。

 

 

 

 

 

 

   3、删除定义模板

[hadoop@HadoopMaster elasticsearch-2.4.3]$ curl -XDELETE 192.168.80.10:9200/_template/template_1     (删除定义的模板)
{"acknowledged":true}[hadoop@HadoopMaster elasticsearch-2.4.3]$ 
[hadoop@HadoopMaster elasticsearch-2.4.3]$ curl -XGET http://192.168.80.10:9200/_template/template_1?pretty   (查看,可见删除模板成功
{ }
[hadoop@HadoopMaster elasticsearch-2.4.3]$

 

 

 

[hadoop@HadoopMaster elasticsearch-2.4.3]$ curl -XDELETE 192.168.80.10:9200/_template/temp*    (删除定义的模板)
{"acknowledged":true}[hadoop@HadoopMaster elasticsearch-2.4.3]$  
[hadoop@HadoopMaster elasticsearch-2.4.3]$ curl -XGET http://192.168.80.10:9200/_template/temp*?pretty    (查看下,删除成功)
{ }
[hadoop@HadoopMaster elasticsearch-2.4.3]$

 

 

 

 

 

 

 

 

 

 

  4、创建多个索引模板

         当存在多个索引模板时并且某个索引两者都匹配时,settings和mpapings将合成一个配置应用在这个索引上。合并的顺序可由索引模板的order属性来控制。

复制代码
curl -XPUT 192.168.80.10:9200/_template/template_1 -d '
{
    "template" : "*",
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false }
        }
    }
}
'
复制代码

   得到,

 

   然后,输入如下:再创建一个模板

复制代码
curl -XPUT 192.168.80.10:9200/_template/template_2 -d '
{
    "template" : "te*",
    "order" : 1,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : true }
        }
    }
}
'
复制代码

  得到,

 

 

[hadoop@HadoopMaster elasticsearch-2.4.3]$ curl -XGET 192.168.80.10:9200/_template/template_1       (查看模板template_1)
{"template_1":{"order":0,"template":"*","settings":{"index":{"number_of_shards":"1"}},"mappings":{"type1":{"_source":{"enabled":false}}},"aliases":{}}}[hadoop@HadoopMaster elasticsearch-2.4.3]$ 
[hadoop@HadoopMaster elasticsearch-2.4.3]$ curl -XGET 192.168.80.10:9200/_template/template_2        (查看模板template_1)
{"template_2":{"order":1,"template":"te*","settings":{"index":{"number_of_shards":"1"}},"mappings":{"type1":{"_source":{"enabled":true}}},"aliases":{}}}[hadoop@HadoopMaster elasticsearch-2.4.3]$ 
[hadoop@HadoopMaster elasticsearch-2.4.3]$

  上述order为1的配置将覆盖order为0的配置,最终索引的配置source的enabled为true。

  注意:order值大的模板内容会覆盖order值小的。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5、模板配置文件

         除了以上方式,索引模板也可以在文件中进行配置。索引模板的配置文件需要在每个

主节点的config目录下,目录结构为:config/templates/template_1.json,temp
late_1.json的样例如下:

复制代码
 1 {  
 2   "template-logstash" : {  
 3     "template" : "logstash*",  
 4     "settings" : {  
 5       "index.number_of_shards" : 5,  
 6       "number_of_replicas" : 1, 7 "index" : { 8 "store" : { 9 "compress" : { 10 "stored" : true, 11 "tv": true 12  } 13  } 14  } 15  }, 16 "mappings" : { 17 "_default_" : { 18 "properties" : { 19 "dynamic" : "true", 20  }, 21  }, 22 "loadbalancer" : { 23 "_source" : { 24 "compress" : true, 25  }, 26 "_ttl" : { 27 "enabled" : true, 28 "default" : "10d" 29  }, 30 "_all" : { 31 "enabled" : false 32  }, 33 "properties" : { 34 "@fields" : { 35 "dynamic" : "true", 36 "properties" : { 37 "client" : { 38 "type" : "string", 39 "index" : "not_analyzed" 40  }, 41 "domain" : { 42 "type" : "string", 43 "index" : "not_analyzed" 44  }, 45 "oh" : { 46 "type" : "string", 47 "index" : "not_analyzed" 48  }, 49 "responsetime" : { 50 "type" : "double", 51  }, 52 "size" : { 53 "type" : "long", 54 "index" : "not_analyzed" 55  }, 56 "status" : { 57 "type" : "string", 58 "index" : "not_analyzed" 59  }, 60 "upstreamtime" : { 61 "type" : "double", 62  }, 63 "url" : { 64 "type" : "string", 65 "index" : "not_analyzed" 66  } 67  } 68  }, 69 "@source" : { 70 "type" : "string", 71 "index" : "not_analyzed" 72  }, 73 "@timestamp" : { 74 "type" : "date", 75 "format" : "dateOptionalTime" 76  }, 77 "@type" : { 78 "type" : "string", 79 "index" : "not_analyzed", 80 "store" : "no" 81  } 82  } 83  } 84  } 85  } 86 }
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

二、索引别名index alias操作示例

1、增加索引别名

curl -XPOST 'http://192.168.80.10:9200/_aliases' -d '
{
    "actions" : [
        { "add" : { "index" : "zhouls", "alias" : "zhouls_all" } }
    ]
}'  

  即,zhouls是索引,zhouls_all是索引别名

  可以看到,成功啦!

 

 

 

 

 2、可以同时为多个索引映射到一个索引别名

 

复制代码
curl -XPOST 'http://192.168.80.10:9200/_aliases' -d '
{
    "actions" : [
        { "add" : { "index" : "zhouls", "alias" : "zhouls_all" } },
        { "add" : { "index" : "zhouls10", "alias" : "zhouls_all" } }
    ]
}'
复制代码

 

 

 

 

 

 

 

3、删除索引别名

  1、删除索引zhouls映射的索引别名zhouls_all

复制代码
curl -XPOST 'http://192.168.80.10:9200/_aliases' -d '
{
    "actions" : [
        { "remove" : { "index" : "zhouls", "alias" : "zhouls_all" } }
    ]
}'
复制代码

 

  2、删除索引zhouls10映射的索引别名zhouls_all

复制代码
curl -XPOST 'http://192.168.80.10:9200/_aliases' -d '
{
    "actions" : [
        { "remove" : { "index" : "zhouls10", "alias" : "zhouls_all" } }
    ]
}'
复制代码


本文转自大数据躺过的坑博客园博客,原文链接:http://www.cnblogs.com/zlslch/p/6478168.html,如需转载请自行联系原作者

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
2月前
|
存储 自然语言处理 关系型数据库
ElasticSearch索引 和MySQL索引那个更高效实用那个更合适
ElasticSearch索引 和MySQL索引那个更高效实用那个更合适
39 0
|
2月前
|
JSON 监控 数据管理
【Elasticsearch专栏 12】深入探索:Elasticsearch使用索引生命周期管理(ILM)自动化删除旧数据
Elasticsearch的ILM功能允许用户定义策略,自动管理索引从创建到删除的生命周期。用户可以设置策略,根据索引年龄或大小自动删除旧数据,节省存储空间。通过应用ILM策略于索引模板,新索引将遵循预定义的生命周期。用户还可以监控ILM状态,确保策略按预期执行。使用ILM,用户可以高效地管理数据,确保旧数据及时删除,同时保持数据完整性和安全性。
|
3月前
|
存储 自然语言处理 搜索推荐
【Elasticsearch专栏 01】深入探索:Elasticsearch的正向索引和倒排索引是什么?
正向索引根据文档ID直接查找文档内容,适用于精确匹配场景;而倒排索引则基于文档内容构建,通过关键词快速定位相关文档,适用于全文搜索,显著提高查询效率,是搜索引擎的核心技术。
|
9天前
|
安全 API 数据安全/隐私保护
Elasticsearch 通过索引阻塞实现数据保护深入解析
Elasticsearch 通过索引阻塞实现数据保护深入解析
11 0
|
9天前
|
安全 Java API
SpringBoot 实现 elasticsearch 索引操作(RestHighLevelClient 的应用)
SpringBoot 实现 elasticsearch 索引操作(RestHighLevelClient 的应用)
14 1
|
11天前
|
索引
Elasticsearch exception [type=illegal_argument_exception, reason=index [.1] is the write index for data stream [slowlog] and cannot be deleted]
在 Elasticsearch 中,你尝试删除的索引是一个数据流(data stream)的一部分,而且是数据流的写入索引(write index),因此无法直接删除它。为了解决这个问题,你可以按照以下步骤进行操作:
|
3月前
|
存储 自然语言处理 搜索推荐
【Elasticsearch专栏 02】深入探索:Elasticsearch为什么使用倒排索引而不是正排索引
倒排索引在搜索引擎中更受欢迎,因为它直接关联文档内容,支持全文搜索和模糊搜索,提高查询效率。其紧凑的结构减少了存储空间,并方便支持多种查询操作。相比之下,正排索引在搜索效率、存储和灵活性方面存在局限。
|
3月前
|
API 索引
Elasticsearch Index Shard Allocation 索引分片分配策略
Elasticsearch Index Shard Allocation 索引分片分配策略
77 1
|
3月前
|
存储 自然语言处理 关系型数据库
Elasticsearch创建一个索引怎么也这么复杂
Elasticsearch创建一个索引怎么也这么复杂
41 0
|
9天前
|
Java Maven 开发工具
【ElasticSearch 】IK 分词器安装
【ElasticSearch 】IK 分词器安装
16 1

热门文章

最新文章