基于 IK 分词器的 ES 通用索引模板

一、索引模板

1、模板简述

ES 允许用户定义一系列模板,当索引被创建的时候,模板的设置会自动被应用到新创建的索引中,这一系列模板被称作Index Templates。模板的设置包括 settingsmappings,通过模式匹配(匹配索引名)的方式控制模板是否应用于新索引。

索引模板只在索引被创建时候生效,一旦索引创建后,对索引模板的修改不会对旧索引的设置造成任何影响。

2、模板结构

简单来说,索引模板是一种复用机制,省去了大量重复性劳动,索引模板的基本结构如下所示:

{
  "order": 0,                // 模板优先级
  "template": "logstash_*",  // 模板匹配的方式
  "settings": {...},         // 索引设置
  "mappings": {...},         // 索引中各字段的映射定义
  "aliases": {...}           // 索引的别名
}
复制代码

3、模板说明

对于模板内部更加细节的配置,本文不做介绍,具体可参考文章:www.jianshu.com/p/1f67e4436…

二、通用的索引模板

  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!

扫码关注『AnyCode』,编程路上,一起前行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值