刚开始的时候,每次实验都去改/etc/elasticsearch/elasticsearch.yml配置文件。事实上在template里修改settings更方便而且灵活!当然最主要的,还是调节里面的properties设定,合理的控制store和analyze了。
template设定也有多种方法。最简单的就是和存储数据一样POST上去。长期的办法,就是写成json文件放在配置路径里。其中,default配置放在/etc/elasticsearch/下,其他配置放在/etc/elasticsearch/templates/下。举例我现在的一个templates/template-logstash.json内容如下:
- {
- "template-logstash" : {
- "template" : "logstash*",
- "settings" : {
- "index.number_of_shards" : 5,
- "number_of_replicas" : 1,
- "index" : {
- "store" : {
- "compress" : {
- "stored" : true,
- "tv": true
- }
- }
- }
- },
- "mappings" : {
- "_default_" : {
- "properties" : {
- "dynamic" : "true",
- },
- },
- "loadbalancer" : {
- "_source" : {
- "compress" : true,
- },
- "_ttl" : {
- "enabled" : true,
- "default" : "10d"
- },
- "_all" : {
- "enabled" : false
- },
- "properties" : {
- "@fields" : {
- "dynamic" : "true",
- "properties" : {
- "client" : {
- "type" : "string",
- "index" : "not_analyzed"
- },
- "domain" : {
- "type" : "string",
- "index" : "not_analyzed"
- },
- "oh" : {
- "type" : "string",
- "index" : "not_analyzed"
- },
- "responsetime" : {
- "type" : "double",
- },
- "size" : {
- "type" : "long",
- "index" : "not_analyzed"
- },
- "status" : {
- "type" : "string",
- "index" : "not_analyzed"
- },
- "upstreamtime" : {
- "type" : "double",
- },
- "url" : {
- "type" : "string",
- "index" : "not_analyzed"
- }
- }
- },
- "@source" : {
- "type" : "string",
- "index" : "not_analyzed"
- },
- "@timestamp" : {
- "type" : "date",
- "format" : "dateOptionalTime"
- },
- "@type" : {
- "type" : "string",
- "index" : "not_analyzed",
- "store" : "no"
- }
- }
- }
- }
- }
- }
注意: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”开头的索引。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
{
"test"
:
{
"template"
:
"test*"
,
"settings"
:
{
"index.number_of_shards"
:
5
,
"number_of_replicas"
:
1
}
,
"mappings"
:
{
"_default_"
:
{
"_source"
:
{
"enabled"
:
false
}
}
,
"news"
:
{
"_source"
:
{
"enabled"
:
true
}
,
"_all"
:
{
"enabled"
:
false
}
,
"properties"
:
{
"site_id"
:
{
"type"
:
"integer"
,
"index"
:
"not_analyzed"
}
,
"title"
:
{
"type"
:
"string"
,
"index"
:
"analyzed"
,
"analyzer"
:
"whitespace"
}
,
"media_type"
:
{
"type"
:
"short"
,
"index"
:
"not_analyzed"
}
}
}
}
}
}
|
3. _source字段
_source字段是自动生成的,以JSON格式存储索引文件。_source字段没有建索引,所以不可搜索。当执行“get”或者“search”操作时,默认会返回_source字段。
_source字段消耗性能,所以可以屏蔽(disable)掉。例如:
1
2
3
4
5
|
{
"tweet"
:
{
"_source"
:
{
"enabled"
:
false
}
}
}
|
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