Elasticsearch模块功能之-索引模板(Index templates)

Elasticsearch模块功能之-索引模板(Index templates)

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

定义模板:

 

[html]  view plain copy
 
  1. curl -XPUT localhost:9200/_template/template_1 -d '  
  2. {  
  3.     "template" : "te*",  
  4.     "settings" : {  
  5.         "number_of_shards" : 1  
  6.     },  
  7.     "mappings" : {  
  8.         "type1" : {  
  9.             "_source" : {"enabled" : false }  
  10.         }  
  11.     }  
  12. }  
  13. '  



 

上述定义的模板template_1将对用te开头的新索引都是有效。

 

模板中也可以包含别别名的定义,如下:

 

[html]  view plain copy
 
  1. curl -XPUT localhost:9200/_template/template_1 -d '  
  2. {  
  3.     "template" : "te*",  
  4.     "settings" : {  
  5.         "number_of_shards" : 1  
  6.     },  
  7.     "aliases" : {  
  8.         "alias1" : {},  
  9.         "alias2" : {  
  10.             "filter" : {  
  11.                 "term" :{"user" : "kimchy" }  
  12.             },  
  13.             "routing" :"kimchy"  
  14.         },  
  15.         "{index}-alias" : {}   
  16.     }  
  17. }  



 

 

删除模板:

         使用模板名称对模板进行删除.

[html]  view plain copy
 
  1. curl -XDELETE localhost:9200/_template/template_1  

 

同样也可以查看定义的模板

 

[html]  view plain copy
 
  1. curl -XGET localhost:9200/_template/template_1  



 

 

多个索引模板:

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

 

[html]  view plain copy
 
  1. curl -XPUT localhost:9200/_template/template_1 -d '  
  2. {  
  3.     "template" : "*",  
  4.     "order" : 0,  
  5.     "settings" : {  
  6.         "number_of_shards" : 1  
  7.     },  
  8.     "mappings" : {  
  9.         "type1" : {  
  10.             "_source" : {"enabled" : false }  
  11.         }  
  12.     }  
  13. }  
  14. '  
  15. ==================================================================  
  16. curl -XPUT localhost:9200/_template/template_2 -d '  
  17. {  
  18.     "template" : "te*",  
  19.     "order" : 1,  
  20.     "settings" : {  
  21.         "number_of_shards" : 1  
  22.     },  
  23.     "mappings" : {  
  24.         "type1" : {  
  25.             "_source" : {"enabled" : true }  
  26.         }  
  27.     }  
  28. }  
  29. '  

 

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

 

模板配置文件:

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

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

 

[java]  view plain copy
 
  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. }  



 

 

【参考】:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html

 

 

from  http://blog.csdn.net/changong28/article/details/38423339

猜你喜欢

转载自aoyouzi.iteye.com/blog/2144723