如何為logstash+elasticsearch配置索引模板?
在使用logstash收集日誌的時候,我們一般會使用logstash自帶的動態索引模板,雖然無須我們做任何定製操作,就能把我們的日誌資料推送到elasticsearch索引叢集中,但是在我們查詢的時候,就會發現,預設的索引模板常常把我們不需要分詞的欄位,給分詞了,這樣以來,我們的比較重要的聚合統計就不準確了:
舉個例子,假如有10臺需要的監控的機器,他們的機器名如下:
-
search-0-170
-
search-1-171
-
search-2-172
-
search-3-173
-
search-4-174
-
search-5-175
-
search-6
-
search-7-177
-
search-8-178
-
search-9-179
如果使用的是logstash的預設模板,它會按-切分機器名,這樣以來想統計那臺機器上的收集日誌最多就有問題了,所以這時候,就需要我們自定義一些索引模板了:
在logstash與elasticsearch整合的時候,總共有如下幾種使用模板的方式:
(1)使用預設自帶的索引模板 ,大部分的欄位都會分詞,適合開發和時候快速驗證使用
(2)在logstash收集端自定義配置模板,因為分散在收集機器上,維護比較麻煩
(3)在elasticsearc服務端自定義配置模板,由elasticsearch負責載入模板,可動態更改,全域性生效,維護比較容易
以上幾種方式:
使用第一種,最簡單,無須任何配置
使用第二種,適合小規模叢集的日誌收集,需要在logstash的output外掛中使用template指定本機器上的一個模板json路徑, 例如 template => "/tmp/logstash.json"
使用第三種,適合大規模叢集的日誌收集,如何配置,主要配置logstash的output外掛中兩個引數:
-
manage_template => false//關閉logstash自動管理模板功能
-
template_name => "crawl"//對映模板的名字
如果使用了,第三種需要在elasticsearch的叢集中的config/templates路徑下配置模板json,在elasticsearch中索引模板可分為兩種:
(一):靜態模板
適合索引欄位資料固定的場景,一旦配置完成,不能向裡面加入多餘的欄位,否則會報錯
優點:scheam已知,業務場景明確,不容易出現因欄位隨便對映從而造成元資料撐爆es記憶體,從而導致es叢集全部宕機
缺點:欄位數多的情況下配置稍繁瑣
一個靜態索引模板配置例子如下:
-
{
-
"crawl" : {
-
"template": "crawl-*",
-
"settings": {
-
"index.number_of_shards": 3,
-
"number_of_replicas": 0
-
},
-
"mappings" : {
-
"logs" : {
-
"properties" : {
-
"@timestamp" : {
-
"type" : "date",
-
"format" : "dateOptionalTime",
-
"doc_values" : true
-
},
-
"@version" : {
-
"type" : "string",
-
"index" : "not_analyzed",
-
"doc_values" : true
-
},
-
"cid" : {
-
"type" : "string",
-
"index" : "not_analyzed"
-
},
-
"crow" : {
-
"type" : "string",
-
"index" : "not_analyzed"
-
},
-
"erow" : {
-
"type" : "string",
-
"index" : "not_analyzed"
-
},
-
"host" : {
-
"type" : "string",
-
"index" : "not_analyzed"
-
},
-
"httpcode" : {
-
"type" : "string",
-
"index" : "not_analyzed"
-
},
-
"message" : {
-
"type" : "string"
-
},
-
"path" : {
-
"type" : "string"
-
},
-
"pcode" : {
-
"type" : "string",
-
"index" : "not_analyzed"
-
},
-
"pro" : {
-
"type" : "string",
-
"index" : "not_analyzed"
-
},
-
"ptype" : {
-
"type" : "string",
-
"index" : "not_analyzed"
-
},
-
"save" : {
-
"type" : "string",
-
"index" : "not_analyzed"
-
},
-
"t1" : {
-
"type" : "string",
-
"index" : "not_analyzed"
-
},
-
"t2" : {
-
"type" : "string",
-
"index" : "not_analyzed"
-
},
-
"t3" : {
-
"type" : "string",
-
"index" : "not_analyzed"
-
},
-
"url" : {
-
"type" : "string"
-
}
-
}
-
}
-
}
-
}
-
}
(二):動態模板
適合欄位數不明確,大量欄位的配置型別相同的場景,多加欄位不會報錯
優點:可動態新增任意欄位,無須改動scheaml,
缺點:如果新增的欄位非常多,有可能造成es叢集宕機
如下的一個logstash的動態索引模板,只設置message欄位分詞,其他的欄位預設都不分詞
-
{
-
"template" : "crawl-*",
-
"settings" : {
-
"index.number_of_shards": 5,
-
"number_of_replicas": 0
-
},
-
"mappings" : {
-
"_default_" : {
-
"_all" : {"enabled" : true, "omit_norms" : true},
-
"dynamic_templates" : [ {
-
"message_field" : {
-
"match" : "message",
-
"match_mapping_type" : "string",
-
"mapping" : {
-
"type" : "string", "index" : "analyzed", "omit_norms" : true,
-
"fielddata" : { "format" : "disabled" }
-
}
-
}
-
}, {
-
"string_fields" : {
-
"match" : "*",
-
"match_mapping_type" : "string",
-
"mapping" : {
-
"type" : "string", "index" : "not_analyzed", "doc_values" : true
-
}
-
}
-
} ],
-
"properties" : {
-
"@timestamp": { "type": "date" },
-
"@version": { "type": "string", "index": "not_analyzed" },
-
"geoip" : {
-
"dynamic": true,
-
"properties" : {
-
"ip": { "type": "ip" },
-
"location" : { "type" : "geo_point" },
-
"latitude" : { "type" : "float" },
-
"longitude" : { "type" : "float" }
-
}
-
}
-
}
-
}
-
}
-
}
總結:
定製索引模板,是搜尋業務中一項比較重要的步驟,需要注意的地方有很多,比如:
(1)欄位數固定嗎
(2)欄位型別是什麼
(3)分不分詞
(4)索引不索引
(5)儲存不儲存
(6)排不排序
(7)是否加權
除了這些還有其他的一些因素,比如,詞庫的維護改動,搜尋架構的變化等等。
如果前提沒有充分的規劃好,後期改變的話,改動其中任何一項,都需要重建索引,這個代價是非常大和耗時的,尤其是在一些資料量大的場景中。