Elasticsearch——Templates 模板

Elasticsearch——Templates 模板

刚开始的时候,每次实验都去改/etc/elasticsearch/elasticsearch.yml配置文件。事实上在template里修改settings更方便而且灵活!当然最主要的,还是调节里面的properties设定,合理的控制store和analyze了。 

template设定也有多种方法。最简单的就是和存储数据一样POST上去。长期的办法,就是写成json文件放在配置路径里。其中,default配置放在/etc/elasticsearch/下,其他配置放在/etc/elasticsearch/templates/下。举例我现在的一个templates/template-logstash.json内容如下: 

Java代码   收藏代码
  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. }  


注意:POST 发送的 json 内容比存储的 json 文件内容要少最外层的名字,因为名字是在 url 里体现的。 

 

Elasticsearch可以预先定义索引模板,当创建新索引时,可以自动匹配模板。模板包括settings和mappings,以及一个匹配索引的正则。

1. 使用curl方式操作templates

详细查阅:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html

2. 配置文件方式

在config目录下创建目录templates,所有模板文件都放在config/templates目录下。

例如:test.json,模板匹配所有以“test”开头的索引。 

 

3. _source字段

_source字段是自动生成的,以JSON格式存储索引文件。_source字段没有建索引,所以不可搜索。当执行“get”或者“search”操作时,默认会返回_source字段。

_source字段消耗性能,所以可以屏蔽(disable)掉。例如:

enabale:false的情况下,默认检索只返回ID。

如果觉得enabale:true时,索引的膨涨率比较大的情况下可以通过下面一些辅助设置进行优化:

Compress:是否进行压缩,建议一般情况下将其设为true

“includes” : ["author", "name"],

“excludes” : ["sex"]

上面的includes和 excludes主要是针对默认情况下面_source一般是保存全部Bulk过去的数据,我们可以通过include,excludes在字段级别上做出一些限索。

详细请查阅:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-source-field.html#mapping-source-field

4. _all字段

主要指的是All Field字段,我们可以将一个或都多个包含进去,在进行检索时无需指定字段的情况下检索多个字段。前提是你得开启All Field字段 “_all” : {“enabled” : true}。好处是你可以在_all里搜索那些你不在乎在哪个字段找到的东西。另一面是在创建索引和增大索引大小的时候会使用额外更多的CPU。所以如果你不用这个特性的话,关掉它。即使你用,最好也考虑一下定义清楚限定哪些字段包含进_all里。

 

 

from http://blog.csdn.net/july_2/article/details/27551739

猜你喜欢

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