乐途乐途
主页
  • 计算机基础

    • TCP/IP
    • Linux
    • HTTP
  • 数据库

    • SQL
    • MySQL 5.7
  • 编程语言

    • C
    • C++
    • Java SE
    • Python2
    • Python3
  • 数据格式

    • JSON
    • XML
  • 认证与安全

    • JWT
  • 工具

    • Markdown
  • Git

    • GitFlow
  • Quartz

    • Quartz
  • Java

    • Maven 入门
    • Maven 进阶
    • MyBatis
    • Spring
    • Spring MVC
  • Java

    • Spring Boot
    • Spring Cloud
    • Spring Cloud Alibaba
    • Spring Security
    • Spring AI
    • Spring Batch
    • Kafka
    • Java 设计模式
  • 缓存

    • Redis
  • 搜索引擎

    • Elasticsearch
  • 分布式协调

    • ZooKeeper
联系
阿里云
主页
  • 计算机基础

    • TCP/IP
    • Linux
    • HTTP
  • 数据库

    • SQL
    • MySQL 5.7
  • 编程语言

    • C
    • C++
    • Java SE
    • Python2
    • Python3
  • 数据格式

    • JSON
    • XML
  • 认证与安全

    • JWT
  • 工具

    • Markdown
  • Git

    • GitFlow
  • Quartz

    • Quartz
  • Java

    • Maven 入门
    • Maven 进阶
    • MyBatis
    • Spring
    • Spring MVC
  • Java

    • Spring Boot
    • Spring Cloud
    • Spring Cloud Alibaba
    • Spring Security
    • Spring AI
    • Spring Batch
    • Kafka
    • Java 设计模式
  • 缓存

    • Redis
  • 搜索引擎

    • Elasticsearch
  • 分布式协调

    • ZooKeeper
联系
阿里云
  • 学习路径
  • 全文搜索与Elasticsearch概述

    • 全文搜索与搜索引擎概述
    • Elasticsearch 与 Lucene 的关系
    • Elasticsearch 核心架构
  • 安装与快速上手

    • 安装 Elasticsearch
    • 快速上手
  • 索引、文档与映射

    • 索引
    • 文档与 CRUD
    • 元数据字段
    • 字段映射
    • 动态映射
    • Reindex 操作
  • 文本分析与分词

    • 倒排索引的构建与原理
    • 分词器组成
    • IK 分词器与自定义词典
    • 近实时搜索原理
    • Ingest Pipeline 数据预处理
  • 搜索基础(Query DSL)

    • match 查询
    • multi_match 多字段查询
    • match_phrase 短语匹配
    • term 与 terms 查询
    • range 范围查询
    • bool 查询
    • 高亮显示与分页
    • 深度分页与遍历
  • 相关性评分与排序

    • BM25 相关性评分
    • 排序与自定义评分
    • Painless 脚本基础
  • 聚合分析(Aggregations)

    • 指标聚合
    • 桶聚合
    • 管道聚合
  • 分布式集群原理

    • 节点与角色
    • 分片与副本
    • 路由
    • 选举与脑裂
    • 快照与恢复
  • 数据建模与最佳实践

    • 索引设计原则与模板
    • 嵌套对象与父子文档
  • 性能优化与运维监控

    • 写入性能优化
    • 搜索性能优化
    • ILM 索引生命周期管理
    • 集群监控与故障排查
  • 面试考点与实战场景

    • 实战案例:课程搜索引擎
    • Elasticsearch 面试考点精选

索引设计原则与模板

本章定位:索引模板(Index Template)让新创建的索引自动应用统一的 Settings 和 Mappings。别名(Alias)实现了索引的零停机切换。二者是生产环境索引管理的基石。


定义与作用

索引模板(Index Template):预定义一组 Settings 和 Mappings 规则,匹配该模板的索引在创建时自动应用这些规则。 别名(Alias):为索引或索引组创建一个逻辑名称,客户端通过别名读写,底层索引可以无缝切换。


完整示例:飞翔科技的索引管理

索引模板

PUT /_index_template/logs_template
{
  "index_patterns": ["logs-*"],
  "priority": 200,
  "template": {
    "settings": {
      "number_of_shards": 3,
      "number_of_replicas": 1,
      "refresh_interval": "30s"
    },
    "mappings": {
      "dynamic": "strict",
      "properties": {
        "timestamp":  { "type": "date" },
        "level":      { "type": "keyword" },
        "message":    { "type": "text" }
      }
    }
  }
}

别名切换

POST /_aliases
{
  "actions": [
    { "remove": { "index": "courses_v1", "alias": "courses" } },
    { "add":    { "index": "courses_v2", "alias": "courses" } }
  ]
}

Component Template 复用

PUT /_component_template/common_settings
{ "template": { "settings": { "number_of_replicas": 1 } } }

PUT /_index_template/course_template
{
  "index_patterns": ["course-*"],
  "composed_of": ["common_settings", "ik_mapping"],
  "priority": 100
}

小结

Index Template 确保索引创建一致性,Component Template 实现组件化复用。Alias 是零停机部署的核心武器——原子切换实现无感知索引迁移。

下一页
嵌套对象与父子文档