Logstash中配置默认索引映射
ES中使用自动检测对索引字段进行索引,例如IP、日期自动检测(默认开启)、数字自动检测(默认关闭)进行动态映射自动为文档设定索引,当需要为字段指定特定的类型时,可能使用Mapping在索引生成定义映射,
Logstash中默认索引的设置是基于模板的,对于indexer角色的logstash。首先我们需要指定一个默认的映射文件,文件的内容大致如下
(我们将它命名为logstash.json,存放在/home/apps/logstash/template/logstash.json):
{
"template" : "logstash*",
"settings" : {
"index.number_of_shards" : 5,
"number_of_replicas" : 1,
"index.refresh_interval" : "60s"
},
"mappings" : {
"_default_" : {
"_all" : {"enabled" : true},
"dynamic_templates" : [ {
"string_fields" : {
"match" : "*",
"match_mapping_type" : "string",
"mapping" : {
"type" : "string", "index" : "not_analyzed", "omit_norms" : true, "doc_values": true,
"fields" : {
"raw" : {"type": "string", "index" : "not_analyzed", "ignore_above" : 256,"doc_values": true}
}
}
}
} ],
"properties" : {
"@version": { "type": "string", "index": "not_analyzed" },
"geoip" : {
"type" : "object",
"dynamic": true,
"path": "full",
"properties" : {
"location" : { "type" : "geo_point" }
}
}
}
}
}
}
其中template定义了匹配的索引模式,如果针对于特定的某个索引,则直接写成索引的名字即可。下面定义了映射的相关信息,与API的内容相同。
有了上面的配置文件,就可以在Logstash中配置output插件了:
output { elasticsearch { host => "localhost" #ES的服务器地址 protocol => "http" #使用的协议,默认可能会使用Node,具体还要看机器的环境 index => "logstash-%{+YYYY.MM.dd}" #匹配的索引模式 document_type => "test" #索引的类型,旧的配置会使用index_type,但是这个字段在新版本中已经被舍弃了,推荐使用document_type manage_template => true #注意默认为true,一定不能设置为false template_overwrite => true #如果设置为true,模板名字一样的时候,新的模板会覆盖旧的模板 template_name => "myLogstash" #注意这个名字是用来查找映射配置的,尽量设置成全局唯一的 template => "/home/apps/logstash/template/logstash.json" #映射配置文件的位置 } }