Elasticsearch template學習


Elasticsearch template

  • Elasticsearch存在一個關鍵問題就是索引的設置及字段的屬性指定,最常見的問題就是,某個字段我們並不希望ES對其進行分詞,但如果使用自動模板創建索引,那么默認是所有string類型的字段都會被分詞的,因此必須要顯式指定字段的not_analyzed屬性,其它的比如控制索引的備份數,分片數等,也可以通過模板的套用來實現,並且模板可以通過通配符進行模糊匹配,即可對滿足某一通配符的所有新建索引均套用統一模板,不需要為每個索引都建立模板。但也有一點局限性需要注意:模板在設置生效后,僅對ES集群中新建立的索引生效,而對已存在的索引及時索引名滿足模板的匹配規則,也不會生效,因此如果需要改變現有索引的mapping信息,仍需要在正確的mapping基礎上建立新的索引,並將數據從原索引拷貝至新索引,變更新索引別名為原索引這種方式來實現(改方法適用當前ES版本(1.7+~2.4+)),也許未來會有索引的直接遷移方案。

  • 參考文章:

相關api的使用

  • 創建
curl -XPUT localhost:9200/_template/template_1 -d
     "template"  "te*" ,
     "order" :1   //就是 elasticsearch 在創建一個索引的時候,如果發現這個索引同時匹配上了多個 template ,那么就會先應用 order 數值小的 template 設置,然后再應用一遍 order 數值高的作為覆蓋,最終達到一個 merge 的效果 
     "settings"  : { 
         "number_of_shards"  : 1 
     }, 
     "mappings"  : { 
         "type1"  : { 
             "_source"  : { "enabled"  false 
        
    
}
  • 刪除模板
curl -XDELETE localhost:9200/_template/template_1
  • 查看定義的模板
curl -XGET localhost:9200/_template/template_1
  • 注:大家也可以使用一些es的管理工具去添加修改查看template

一些重要的字段

  • _source:_source字段是自動生成的,以JSON格式存儲索引文件。_source字段沒有建索引,所以不可搜索。當執行“get”或者“search”操作時,默認會返回_source字段。_source字段消耗性能,所以可以屏蔽disable掉。enabale:false的情況下,默認檢索只返回ID。例如:"_source":{"enabled":false}

  • _all:主要指的是All Field字段,我們可以將一個或都多個包含進去,在進行檢索時無需指定字段的情況下檢索多個字段。前提是你得開啟All Field字段 “_all” : {“enabled” : true}。好處是你可以在_all里搜索那些你不在乎在哪個字段找到的東西。另一面是在創建索引和增大索引大小的時候會使用額外更多的CPU。所以如果你不用這個特性的話,關掉它。即使你用,最好也考慮一下定義清楚限定哪些字段包含進_all里。

  • "index":"analyzed":

    1) analyzed -- 使用分詞器將域值分解成獨立的語匯單元流,並使每個語匯單元能被搜到,適用於普通文本域(如正文、標題、摘要等),通常需要設置“index_analyzer
    2) not_analyzed -- 對域進行索引,但不對String值進行分析,實際上將域值作為 單一語匯單元並使之能本搜索,適用於不能被分解的域值,如URL、文件路徑、日期、電話等。
    3) no -- 使用對應的域值不被搜索

  • "null_value":"none":為空添加的默認值

  • store:域存儲選項store,用來確定是否需要存儲域的真實值,以便后續搜集時能恢復這個值

    1. yes -- 指定存儲域值。該情況下,原始的字符串全部被保存在索引中,並可以由IndexReader類恢復。該選項對於需要展示搜索結果的一些域很有用(如URL、標題等)。如果索引的大小在搜索程序考慮之列的話,不要存儲太大的域值,因為這些域值會消耗掉索引的存儲空間
    2. no -- 指定不存儲域值。該選項通常跟Index.ANALYZED選項共同用來索引大的文本域值,這些域值不用恢復初始格式,如文本正文
  • omit_norms:norms記錄了索引中index-time boost信息,但是當你進行搜索時可能會比較耗費內存。omit_norms = true則是忽略掉域加權信息,這樣在搜索的時候就不會處理索引時刻的加權信息了

一個nginx-ccess日志解析后寫的template

{
   "order" : 1,
   "template" "logstash-app-trace-*" ,
   "settings" : {
     "index" : {
       "number_of_shards" "3" ,
       "number_of_replicas" "1" ,
       "refresh_interval" "5s"
     }
   },
   "mappings" : {
     "_default_" : {
       "properties" : {
         "request" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "span_name" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "body_bytes_sent" : {
           "type" "integer"
         },
         "type" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "http_user_agent" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "uid" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "protocol" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "request_time" : {
           "type" "long"
         },
         "node_type" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "pspan_id" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "remote_addr" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "trace_id" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "device_id" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "span_id" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "time_local" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "params" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "server_addr" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "url" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "request_body" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "http_referer" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "http_x_forwarded_for" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "upstream_response_time" : {
           "type" "integer"
         },
         "response_time" : {
           "type" "long"
         },
         "http_status" : {
           "type" "integer"
         },
         "result_code" : {
           "index" "not_analyzed" ,
           "type" "string"
         },
         "status" : {
           "type" "integer"
         },
         "node_id" : {
           "index" "not_analyzed" ,
           "type" "string"
         }
       },
       "_all" : {
         "enabled" false
       }
     }
   },
   "aliases" : {
     "app-trace-template" : {}
   }
}


免責聲明!

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



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