基於 IK 分詞器的 ES 通用索引模板
ES 允許使用者定義一系列模板,當索引被建立的時候,模板的設定會自動被應用到新建立的索引中,這一系列模板被稱作 Index Templates
。模板的設定包括 settings
和 mappings
,通過模式匹配(匹配索引名)的方式控制模板是否應用於新索引。
索引模板只在索引被建立時候生效,一旦索引建立後,對索引模板的修改不會對舊索引的設定造成任何影響。
2、模板結構
簡單來說,索引模板是一種複用機制,省去了大量重複性勞動,索引模板的基本結構如下所示:
{ "order": 0,// 模板優先順序 "template": "logstash_*",// 模板匹配的方式 "settings": {...},// 索引設定 "mappings": {...},// 索引中各欄位的對映定義 "aliases": {...}// 索引的別名 } 複製程式碼
3、模板說明
對於模板內部更加細節的配置,本文不做介紹,具體可參考文章: ofollow,noindex">www.jianshu.com/p/1f67e4436… 。
二、通用的索引模板
- 下面的通用模板適合大多數情況,僅供參考,實際應用請務必根據實際情況進行優化調整;
- 使用模板前,請務必安裝 IK 分詞器;
- 對於索引配置的優化,可以參考文章 Elasitcsearch索引優化 。
{ "order": 0, "template": "*", "settings": { "index": { "refresh_interval": "5s", "number_of_shards": "3", "max_result_window": 10000, "translog": { "flush_threshold_size": "500mb", "sync_interval": "30s", "durability": "async" }, "merge": { "scheduler": { "max_merge_count": "100", "max_thread_count": "1" } }, "analysis": { "analyzer": { "hanlp_array": { "type": "pattern", "pattern": "[,;。??!!,、;::“”‘’《》【】()~〈〉「」『』…/\\[\\]<>\"\\`\\^*+]+", "lowercase": "true" } } }, "number_of_replicas": "0", "unassigned": { "node_left": { "delayed_timeout": "2m" } } } }, "mappings": { "doc": { "dynamic_date_formats": ["yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy/MM/dd||yyyy/MM||strict_date_optional_time||epoch_millis"], "_all": { "enabled": false }, "properties": { "html": { "index": "false", "doc_values": "false", "norms": "false", "fielddata": "false", "store": "false", "type": "text" } }, "dynamic_templates": [ { "id_field": { "mapping": { "type": "keyword", "store": "true" }, "match": "*id" } }, { "no_field": { "mapping": { "type": "keyword", "store": "true" }, "match": "*no" } }, { "code_field": { "mapping": { "type": "keyword", "store": "true" }, "match": "*code" } }, { "geo_field": { "mapping": { "type": "geo_point", "store": "true" }, "match": "*_geo" } }, { "ip_field": { "mapping": { "type": "ip", "store": "true" }, "match": "*_ip" } }, { "len_field": { "mapping": { "type": "integer", "store": "true" }, "match": "*_len" } }, { "num_field": { "mapping": { "type": "integer", "store": "true" }, "match": "*_num" } }, { "long_field": { "mapping": { "type": "long", "store": "true" }, "match": "*_long" } }, { "ft_field": { "mapping": { "type": "float", "store": "true" }, "match": "*_ft" } }, { "db_field": { "mapping": { "type": "double", "store": "true" }, "match": "*_db" } }, { "typ_field": { "mapping": { "type": "keyword", "store": "true" }, "match": "*_typ*" } }, { "sta_field": { "mapping": { "type": "keyword", "store": "true" }, "match": "*_sta" } }, { "lvl_field": { "mapping": { "type": "keyword", "store": "true" }, "match": "*_lvl" } }, { "flg_field": { "mapping": { "type": "keyword", "store": "true" }, "match": "*_flg" } }, { "dtm_field": { "mapping": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy||yyyy/MM/dd||yyyy/MM||strict_date_optional_time||epoch_millis", "store": "true" }, "match": "*_dtm" } }, { "ns_field": { "mapping": { "index": "false", "doc_values": "false", "norms": "false", "fielddata": "false", "store": "false" }, "match": "*_ns" } }, { "bin_field": { "mapping": { "type": "binary", "doc_values": "false", "norms": "false", "fielddata": "false", "store": "false" }, "match": "*_bin" } }, { "raw_field": { "mapping": { "type": "binary", "doc_values": "false", "norms": "false", "fielddata": "false", "store": "false" }, "match": "*_raw" } }, { "std_field": { "mapping": { "store": "true", "analyzer": "standard", "type": "text" }, "match": "*_std" } }, { "url_field": { "mapping": { "store": "true", "type": "keyword", "doc_values": "false", "norms": "false", "fielddata": "false" }, "match": "*_url" } }, { "tag_field": { "mapping": { "store": "true", "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "search_quote_analyzer": "ik_max_word", "fields": { "orginal": { "type": "keyword" }, "array": { "analyzer": "hanlp_array", "search_analyzer": "ik_max_word", "type": "text", "fielddata": "true" } } }, "match": "*tag" } }, { "file_field": { "mapping": { "type": "attachment", "fields": { "content": { "store": "false", "type": "text" }, "author": { "store": "true", "type": "text" }, "title": { "store": "true", "type": "text" }, "keywords": { "store": "true", "type": "text" }, "content_length": { "store": "true" }, "language": { "store": "true" }, "date": { "store": "true", "type": "date" }, "content_type": { "store": "true" } } }, "match": "*_file" } }, { "path_field": { "mapping": { "store": "true", "analyzer": "hanlp_array", "search_analyzer": "ik_max_word", "type": "text", "fielddata": "true", "fields": { "normal": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "search_quote_analyzer": "ik_max_word" }, "orginal": { "type": "keyword" } } }, "match": "*_path" } }, { "arr_field": { "mapping": { "store": "true", "analyzer": "hanlp_array", "search_analyzer": "ik_max_word", "type": "text", "fielddata": "true", "fields": { "normal": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "search_quote_analyzer": "ik_max_word" } } }, "match": "*_arr" } }, { "string_field": { "mapping": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "search_quote_analyzer": "ik_max_word", "term_vector": "with_positions_offsets", "fields": { "orginal": { "type": "keyword", "ignore_above": "36" } } }, "match_mapping_type": "string" } } ] } } } 複製程式碼
Any Code,Code Any!
掃碼關注『AnyCode』,程式設計路上,一起前行。
