為什么需要索引模板?
在實際工作中針對一批大量數據存儲的時候需要使用多個索引庫,如果手工指定每個索引庫的配置信息(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" } } ] }'