基於 IK 分詞器的 ES 通用索引模板

1、索引模板

一、模板簡述

ES 容許用戶定義一系列模板,當索引被建立的時候,模板的設置會自動被應用到新建立的索引中,這一系列模板被稱做Index Templates。模板的設置包括 settingsmappings,經過模式匹配(匹配索引名)的方式控制模板是否應用於新索引。html

索引模板只在索引被建立時候生效,一旦索引建立後,對索引模板的修改不會對舊索引的設置形成任何影響。node

二、模板結構

簡單來講,索引模板是一種複用機制,省去了大量重複性勞動,索引模板的基本結構以下所示:編程

{
  "order": 0,                // 模板優先級
  "template": "logstash_*",  // 模板匹配的方式
  "settings": {...},         // 索引設置
  "mappings": {...},         // 索引中各字段的映射定義
  "aliases": {...}           // 索引的別名
}
複製代碼

三、模板說明

對於模板內部更加細節的配置,本文不作介紹,具體可參考文章:www.jianshu.com/p/1f67e4436…bash

2、通用的索引模板

  1. 下面的通用模板適合大多數狀況,僅供參考,實際應用請務必根據實際狀況進行優化調整;
  2. 使用模板前,請務必安裝 IK 分詞器;
  3. 對於索引配置的優化,能夠參考文章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!app

掃碼關注『AnyCode』,編程路上,一塊兒前行。async

相關文章
相關標籤/搜索