ElasticSearch——自定義模板


 

output中配置

elasticsearch{
  action => "index"
  hosts => ["xxx"]
  index => "http-log-logstash"
  document_type => "logs"
  template => "opt/http-logstash.json"
  template_name => "http-log-logstash"
  template_overwrite => true
}

 

自定義模板示例

{ 
    "template" : "logstash-*",   -------------> 匹配的索引名字
    "order":1,  -------------> 代表權重,如果有多個模板的時候,優先進行匹配,值越大,權重越高 "settings" : { "index.refresh_interval" : "60s" }, 
    "mappings" : { 
        "_default_" : { 
            "_all" : { "enabled" : false }, 
       "_source" : { "enabled" : false },
       "dynamic": "strict",
"dynamic_templates" : [{ "message_field" : { "match" : "message", "match_mapping_type" : "string", "mapping" : { "type" : "string", "index" : "not_analyzed" } } }, { "string_fields" : { "match" : "*", "match_mapping_type" : "string", "mapping" : { "type" : "string", "index" : "not_analyzed" } } }], "properties" : { "@timestamp" : { "type" : "date"}, "@version" : { "type" : "integer", "index" : "not_analyzed" }, "path" : { "type" : "string", "index" : "not_analyzed" }, "host" : { "type" : "string", "index" : "not_analyzed" }, "record_time":{"type":"date","format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}, "method":{"type":"string","index" : "not_analyzed"}, "unionid":{"type":"string","index" : "not_analyzed"}, "user_name":{"type":"string","index" : "not_analyzed"}, "query":{"type":"string","index" : "not_analyzed"}, "ip":{ "type" : "ip"}, "webbrower":{"type":"string","index" : "not_analyzed"}, "os":{"type":"string","index" : "not_analyzed"}, "device":{"type":"string","index" : "not_analyzed"}, "ptype":{"type":"string","index" : "not_analyzed"}, "serarch_time":{"type":"date","format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}, "have_ok":{"type":"string","index" : "not_analyzed"}, "legal":{"type":"string","index" : "not_analyzed"} } } } }

 

關鍵設置

  • template for index-pattern

只有匹配 logstash-* 的索引才會應用這個模板。有時候我們會變更 Logstash 的默認索引名稱,記住你也得通過 PUT 方法上傳可以匹配你自定義索引名的模板。當然,我更建議的做法是,把你自定義的名字放在 "logstash-" 后面,變成 index => "logstash-custom-%{+yyyy.MM.dd}" 這樣。

 

  • refresh_interval for indexing

Elasticsearch 是一個實時搜索引擎。它實際上是每 1 秒鍾刷新一次數據。對於日志分析應用,我們用不着這么實時,所以 logstash 自帶的模板修改成了 5 秒鍾。你還可以根據需要繼續放大這個刷新間隔以提高數據寫入性能。

 

  • multi-field with not_analyzed

Elasticsearch 會自動使用自己的默認分詞器(空格,點,斜線等分割)來分析字段。分詞器對於搜索和評分是非常重要的,但是大大降低了索引寫入和聚合請求的性能。所以 logstash 模板定義了一種叫"多字段"(multi-field)類型的字段。這種類型會自動添加一個 ".raw" 結尾的字段,並給這個字段設置為不啟用分詞器。簡單說,你想獲取 url 字段的聚合結果的時候,不要直接用 "url" ,而是用 "url.raw" 作為字段名。

 

  • geo_point

Elasticsearch 支持 geo_point 類型, geo distance 聚合等等。比如說,你可以請求某個 geo_point 點方圓 10 千米內數據點的總數。在 Kibana 的 bettermap 類型面板里,就會用到這個類型的數據。

 

  • order

如果你有自己單獨定制 template 的想法,很好。這時候有幾種選擇:

  1. 在 logstash/outputs/elasticsearch 配置中開啟 manage_template => false 選項,然后一切自己動手;
  2. 在 logstash/outputs/elasticsearch 配置中開啟 template => "/path/to/your/tmpl.json" 選項,讓 logstash 來發送你自己寫的 template 文件;
  3. 避免變更 logstash 里的配置,而是另外發送一個 template ,利用 elasticsearch 的 templates order 功能。

這個 order 功能,就是 elasticsearch 在創建一個索引的時候,如果發現這個索引同時匹配上了多個 template ,那么就會先應用 order 數值小的 template 設置,然后再應用一遍 order 數值高的作為覆蓋,最終達到一個 merge 的效果。

比如,對上面這個模板已經很滿意,只想修改一下 refresh_interval ,那么只需要新寫一個:

{
  "order" : 1,
  "template" : "logstash-*",
  "settings" : {
    "index.refresh_interval" : "20s"
  }
}

然后運行以下命令即可:

curl -XPUT http://localhost:9200/_template/template_newid -d '@/path/to/your/tmpl.json'

 

  • set _source 設置為 false

假設你只關心度量結果,不是原始文件內容。比如,你可以把原始的數據存儲在 MySQL ,hbase 等其他地方,從 es 中得到 id 后,去相應的數據庫中進行取數據。

將節省磁盤空間並減少 IO。

“_source”:{“enabled”:false}

 

  • _all 設置為 false

假設你確切地知道你對哪個 field 做查詢操作?   

能實現性能提升,縮減存儲。

“_all”:{“enabled”:false }

 

  • dynamic設置為 strict

假設你的數據是結構化數據。
字段設置嚴格,避免臟數據注入。

dynamic”:”strict”

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM