Logstash中配置默認索引映射(_default_屬性)

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" }
             }
         }
       }
    }
  }
}

例如假設有一字段存儲內容爲IP,不希望被自動檢測識別爲string類型,則可以在_default_中定義ip字段的type爲IP,如下:

$ curl -XPUT localhost:9200/test?pretty -d '{"mappings":{"_default_":{"properties":{"ip":{"type":"ip"}}}}}'

其中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" #映射配置文件的位置
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章