一、模板简述:

索引可使用预定义的模板进行创建,这个模板称作Index templates。模板设置包括settings和mappings,通过模式匹配的方式使得多个索引重用一个模板。

template大致分成settingmappings两部分:

  1. settings主要作用于index的一些相关配置信息,如分片数、副本数,tranlog同步条件、refresh等。

  2. mappings主要是一些说明信息,大致又分为_all、_source、properties 这三部分。

    _all:主要指的是AllField字段,我们可以将一个或多个都包含进来,在进行检索时无需指定字段的情况下检索 多个字段。设置"_all" : {"enabled" : true} 就是一个超级字段全部连起来而已。占用索引空间大小。_all字段默认是关闭的。

    _source:主要指的是SourceField字段,Source可以理解为ES除了将数据保存在索引文件中,另外还有一份源数据。_source字段在我们进行检索时相当重要,如果在{"enabled" : false}情况下默认检索只会返回 ID, 你需要通过 Fields字段去到索引中去取数据,效率不是很高。但是enabled设置为true时,索引会比较大,这时可以通过Compress进行压缩和inclueds、excludes来在字段级别上进行一些限制,自定义哪些字段允许存储。_source 字段默认是存储的。

    properties:咱们通常在 elasticsearch 中 post mapping信息,每重新创建索引便到设置 mapping,分片,副本信息。非常繁琐。强烈建议大家通过设置template方式设置索引信息。设置索引名,通过正则匹配的方式匹配到相应的模板。(ps:直接修改 mapping 的优先级 > 索引 template)。索引匹配了多个 template,当属性等配置出现不一致的,以 order 的最大值为准,order 默认值为0

下面贴出一个常用的模板属性简单介绍:
{
  "template": "pmall*",
  "settings": {
    "index.number_of_shards": 1,
    "number_of_replicas": 4,
    "similarity": {
      "IgnoreTFSimilarity": {
        "type": "IgoreTFSimilarity"
      }
    },
"analysis": {    
      "normalizer": {  //解析前的标准化配置,比如把所有的字符转化为小写等。
        "my_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": ["lowercase", "asciifolding"]
        }
      }
    }
  },
  "mappings": {
    "_default_": {  //默认的配置,没有指定type,过时了
      "_source": {
        "enabled": false
      }"my_type": {
      "dynamic": "strict",   //动态调整增加doc数据是有非指定字段是怎么办。
						    //默认true添加到新字段里面,false忽略字段,strict严格模式,报错拒绝文档
     }
    },
    "commodity": {
      "properties": {
        "sold": {
          "type": "long"
        },
        "online_time": {
          "type": "long""format": "yyyy-MM-dd HH:mm:ss.SSS"  
        },
        "price": {
          "type": "long"
        },
        "publish_time": {
          "type": "long"
        },
        "userId": {
          "type": "integer",
          "ignore_malformed": true //对于账号userid字段,有人可能填写的是 整数类型,也有人填写的是邮件格式。
        },
        "id": {
          "type": "long"
        },
        "catecode": {
          "type": "integer",
		"coerce": false  // coerce属性用于清除脏数据,coerce的默认值是true,默认会强制转化为int,设置为false就不转化,导致报错无法插入
        },
        "title": {
          "search_analyzer": "ikSmart",
          "similarity": "IgnoreTFSimilarity",
          "analyzer": "ik",
          "type": "text"
        },
        "content": {
          "index": false,
          "store": true,
          "type": "keyword",
		"ignore_above":"20"  //表示当内容超过20个字符后就不索引了。
		  "field"{  //表示建立多个索引,以conten.raw 构建关键字索引。
            "raw"{
              "type""keyword" 
            } 
          }
        },
        "status": {
          "type": "integer""enabled": false  // es不索引这个字段了,放弃它,搜索是搜不到的,只能从_source里面获取。
        }
      }
    }
  }
}

二、动态模板

动态模板使您可以基于以下内容定义可应用于动态添加的字段的自定义映射。

#S默认string类型字段会被映射成text和keyword(sub_field),如果我们想只映射成keyword(用来过滤、排序、统计等)。同时date格式的都设置格式化
put _template/my_ynamic_template

{
    "order": 0,
    "index_patterns": ["ssouth_*"],
    "mappings": {
        "dynamic_templates": [{
                "strings": {
                    "match_mapping_type": "string",
                    "mapping": {
                        "type": "keyword"
                    }
                }
            }, {
                "dates": {
                    "match_mapping_type": "date",
                    "mapping": {
                        "format": "yyyy-MM-dd HH:mm:ss.SSS||date_time||strict_date_time",
                        "type": "date"
                    }
                }
            }
        ],
        "date_detection": false,
        "properties": {
            "byteoffset": {
                "type": "long"
            },
            "createTime": {
                "format": "yyyy-MM-dd HH:mm:ss.SSS||date_time||strict_date_time",
                "type": "date"
            },
            "dataTime": {
                "format": "yyyy-MM-dd HH:mm:ss.SSS||date_time||strict_date_time",
                "type": "date"
            },
            "agentIp": {
                "type": "keyword"
            },
            "rawMessage": {
                "analyzer": "you_analyzer",
                "type": "text"
            }
        }
    }
}

三、重建索引(reindex)

索引变成的场景:

  • 索引的 Mappings 发生变更:字段类型更改,分词器及字典更新
  • 索引的 Settings 发⽣变更:索引的主分⽚数发⽣生改变 – 集群内,集群间需要做数据迁移

索引变更的方式

  • Update By Query:在现有索引上重建
  • Reindex:在其他索引上重建索引

Reindex

使用场景:

  • 修改字段类型. 需要先设置索引 mapping, 索引的设置和映射关系不会被复制
  • 备份一下索引,后面可以随意操作原来的索引
  • 修改索引的主分片数 – 集群内迁移数据、跨集群迁移数据 reindex 一般是一个比较耗时的操作,但是直接调用 reindex api 的接口超时时间比较端,我们可以使用 wait_for_completion,这样不必等待 reindex 执行完成才返回。结合 _tasks 可以查看任务执行的进度。

案例:修改字段类型

# 修改索引:修改索引类型
# 创建被迁移的索引
PUT blog_reindex
{
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "createTime": {
        "type": "date"
      },
      "lastTime": {
        "type": "date"
      },
      "content": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 10240
          }
        }
      },
      "title": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "status": {
        "type": "keyword"
      },
      "type": {
        "type": "keyword"
      },
      "content_filtered": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 10240
          }
        }
      },
      "comment_count": {
        "type": "integer"
      },
      "new_field": {
        "type": "integer"
      }
    }
  },
  "settings": {
    "index": {
      "number_of_shards": "1",
      "number_of_replicas": "0"
    }
  }
}
# 重建索引
POST _reindex?wait_for_completion=false
{
  "source": {
    "index": "blog"
  },
  "dest": {
    "index": "blog_reindex",
    "op_type": "create"
  }
}
# 查看迁移状态
GET _tasks?detailed=true&actions=*reindex

尽量使用 Index Alias 读写数据,这样即使需要 Reindex, 也可以实现零停机维护

上次更新时间: 2024/5/7 05:59:02