Elasticsearch--- 索引的模板

Elasticsearch--- 索引的模板

索引的主要参数:设置(settings)和映射(mappings)、aliases(别名),索引模板是在索引创建时,匹配上了应用于索引的配置,索引里的setting、mappings可以另外自己设置、修改。

创建一个索引,这个索引的名称最好带上版本号,比如my_index_v1,my_index_v2等。

6.x中创建的索引只允许单个索引中存在单一的类型。任意的名字都可以作为类型,但是只能有一个。

索引模板:

es内部维护了template,template定义好了mapping,只要index的名称被template匹配到,那么该index的mapping就按照template中定义的mapping自动创建。而且template中定义了index的shard分片数量、replica副本数量等等属性。
 

1. 模板样例

API springboot Document注解方式
{
  "order": 0,                               // 模板优先级(越大越高)
  "template": "sample_info*",     // 模板匹配的名称方式
  "settings": {...},                        // 索引设置
  "mappings": {...},                     // 索引中各字段的映射定义
  "aliases": {...}                          // 索引的别名
}

public @interface Document {

String indexName();

String type() default "";

boolean useServerConfiguration() default false;

short shards() default 5;

short replicas() default 1;

String refreshInterval() default "1s";

String indexStoreType() default "fs";

boolean createIndex() default true;

  • settings:定义了索引的属性,包括分片数量、副本数量、写入flush时间间隔 。
  • template:index的名称被template匹配,决定使用哪个模板。

2. Setting参数结构:

"settings": {
    "index": {
      "analysis": {                     // 1. 自定义的分析器
            "char_filter": { ... },             // 1.1 用户自定义字符过滤器
            "tokenizer":   { ... },             // 1.2 用户自定义分词器
            "filter":      { ... },             // 1.3 用户自定义标记过滤器
            "analyzer":    { ... }              // 1.4 用户自定义分析器
       },                
      "number_of_shards": "32",         // 2. 主分片的个数
      "number_of_replicas": "1",        // 3. 主分片的拷贝分片个数
      "refresh_interval": "5s"          // 4. 刷新时间
    }
  }

默认及自定义分析器: 

"settings": {
    "index": {
      "analysis": {
           "char_filter": {     // 1. 自定义字符过滤器列表
                "&_to_and": {
                    "type":       "mapping",
                    "mappings": [ "&=> and "]
                },
                "replace_dot": {
                    "pattern": "\\.",
                    "type": "pattern_replace",
                    "replacement": " "
                }
            },
           "tokenizer":{       //2.自定义分词器
                "myTokenizer1":{
                "type":"standard",
                "max_token_length":900
                }
            },
            "filter":{     //3.自定义分词过滤器
                "my_stop": {
                    "type":        "stop",
                    "stopwords": _spanish_
                },
                "my_stopwords": {
                    "type":        "stop",
                    "stopwords": [ "the", "a" ]
                }
            },
            "analyzer": {  // 4. 自定义分析器列表
                "my_analyzer": { //4.1 analyzer是字符过滤器、分词器、标记过滤器的组合。
                    "type":           "custom",
                    "char_filter":  [ "html_strip", "&_to_and", "replace_dot" ],
                    "tokenizer":      "standard",
                    "filter":       [ "lowercase", "my_stopwords", "my_stop" ]
                }
            }
      },
      ...
    }
  }

3. Mapping参数结构:

"mappings": {
    "my_type": {                       // 1. 索引下的 my_type 类型应用该映射。
      "dynamic_templates": [ ... ],    // 2.动态映射部分,用于未定义的 my_type 下字段
      "properties": { ... }            // 3. 自定义字段的响应映射
    },
    “default":{        //4.索引下默认mappings,
      "dynamic_templates": [ ... ],    
      "properties": { ... } 
    }
}

default: 默认Mappings,因为一个索引(index)下有多个类型(type),没定义具体的type的Mappings可以使用default定义下的映射

一、模板操作常用命令:

1. 删除 curl -X DELETE "localhost:9200/_template/template_1"

2. 查看:

  • GET /_template/template_1
  • GET /_template/temp*
  • GET /_template/template_1,template_2
  • GET /_template

3. 判断是否存在 HEAD _template/template_1

4. 添加:PUT _template/my_template  (模板写入es) 

三、参考:

https://www.jianshu.com/p/1f67e4436c37

发布了18 篇原创文章 · 获赞 1 · 访问量 3908

猜你喜欢

转载自blog.csdn.net/silmeweed/article/details/98161135