带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(7)

简介: 带你读《Elastic Stack 实战手册》之23:——3.4.2.8.Index template(7)

《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.8.Index template(6) https://developer.aliyun.com/article/1230795


本例我们创建了名为bar-test-old的索引,其索引名以bar开头,匹配索引模板old_template的筛选规则,我们在创建时仅指定了副本数和 1 个ip属性,而使用 GET 请求查看索引信息时可以看出,索引模板中的别名配置、host_name属性和分片数的配置被使用了。但是关于_source的设置,由于创建索引时明确指出了,所以模板中的false配置被覆盖为了true

 

当匹配到多个老版索引模板时,最终配置为多个模板的组合,当不同模板配置了相同字段时,那么以order高的模板配置为准。示例如下:

 

# 创建新版索引模板   
PUT /_index_template/new_template   #1
{
   "index_patterns": ["template*"],
   "priority" : 100, 
   "template": {
    "mappings": {
      "dynamic": "true"
    },
    "aliases": {
      "new-template": {}
    }
  }
}
# 创建老版索引模板,不配置 order
PUT /_template/old_template_1     #2
{
  "index_patterns": ["temp*"],
   "mappings": {
      "dynamic": "false"
    },
  "aliases": {
      "old_template_1": {}
    }
}
# 创建老版本索引模板,配置高 order
PUT /_template/old_template_2?order=10  #3
{
  "index_patterns": ["temp-*"],
   "mappings": {
      "dynamic": "strict"
    },
  "aliases": {
      "old_template_2": {}
    }
}
# 创建索引,名称会匹配 new_template 模板和 old_template_1 模板
PUT /template-test    #4
{
  "settings": {
    "number_of_shards": 2
  },
  "mappings": {
    "properties": {
      "ip":{
        "type": "ip"
      }
    }
  }
}
# 查看索引配置
GET  /template-test  #5
# 返回
{
  "template-test" : {
    "aliases" : {
      "new-template" : { }  #  仅有新版本模板的别名设置
    },
    "mappings" : {
      "dynamic" : "true",
      "properties" : {
        "ip" : {
          "type" : "ip"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "2",
        "provided_name" : "template-test",
        "creation_date" : "1620617781794",
        "number_of_replicas" : "1",
        "uuid" : "0PKYANozRya9zG3LdMG1UA",
        "version" : {
          "created" : "7100099"
        }
      }
    }
  }
}
# 创建索引,名称会匹配 old_template_1 模板和 old_template_1 模板
PUT /temp-test #6
{
  "settings": {
    "number_of_shards": 2
  },
  "mappings": {
    "properties": {
      "ip":{
        "type": "ip"
      }
    }
  }
}
# 查看索引配置
GET /temp-test
# 返回
{
  "temp-test" : {
    "aliases" : {
      "old_template_1" : { },  # old_template_1 的配置
      "old_template_2" : { }    # old_template_2 的配置
    },
    "mappings" : {
      "dynamic" : "strict",  # old_template_2 的配置
      "properties" : {
        "ip" : {
          "type" : "ip"
        }
      }
    },
    "settings" : {
        "refresh_interval" : "10s",
        "number_of_shards" : "2",
        "provided_name" : "temp-test",
        "creation_date" : "1620617979932",
        "number_of_replicas" : "1",
        "uuid" : "RwvrdGziT7iVmVutXYPwqA",
        "version" : {
          "created" : "7100099"
        }
      }
    }
  }
}

1、#1 处我们创建了 1 个新版本的索引模板匹配名字,以template开头的索引,设置可以动态增加属性并设置别名new-template


2、#2 处我们创建了 1 个老版本的索引模板匹配名字,以temp开头的索引,设置不动态增加属性并设置别名old_template_1

3、#3 处我们创建了 1 个老版本的索引模板匹配名字,以temp-开头的索引,设置有新增属性时报错并设置别名old_template_2,同时配置order为10

4、#4 处我们创建了 1 个名为template-test的索引

5、#5 处查看索引template-test的配置,该名称会匹配新版new_template模板,和老版

old_template_1模板,根据索引的别名信息,只有新版模板配置的别名可以看出,该索引仅仅应用了new_template模板

6、#6 处创建了 1 个名为temp-test的索引

7、#7 处查看索引temp-test的配置,该名称会匹配老版old_template_1old_template_2模板,根据索引的别名信息有old_template_1old_template_1可以看出,2 个模板的配置都应用了。通过dynamic字段为strict我们可以判断该字段使用了order较高的模板old_template_2的配置

 


《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.8.Index template(8) https://developer.aliyun.com/article/1230793

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
相关文章
|
13天前
|
人工智能 自然语言处理 文字识别
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
Qwen3.7-Max是阿里云百炼面向智能体时代推出的新一代旗舰模型,对标GPT-5.5、Claude Opus 4.7等闭源旗舰。该模型支持百万级token上下文窗口,具备顶级推理能力、多模态搜索与视觉理解增强、流式输出低延迟响应等核心优势,覆盖编程、办公、长周期自主执行等复杂场景。同时支持OpenAI接口兼容,便于系统快速迁移。用户可通过Token Plan团队或节省计划等订阅方式灵活调用,适合企业级高要求场景使用。
5025 23
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
|
7天前
|
存储 定位技术 数据库
CodeGraph 如何让 Claude Code减少 7 成工具调用?
CodeGraph 为 Coding Agent 提供本地代码知识图谱,把函数、类、调用链和框架路由提前整理成“项目地图”,减少盲目搜索和文件读取。它不是新 Agent,而是上下文基础设施,让 Agent 更快找到正确代码路径,平均减少 7 成工具调用。
996 0
|
14天前
|
人工智能 自然语言处理 供应链
|
5天前
|
人工智能 安全 定位技术
CodeGraph深度解析 让Claude Code工具调用直降七成的核心原理与实操教程
如今以Claude Code为代表的AI编程智能体已经成为开发者日常编码、项目重构、漏洞修复的必备工具。但在长期使用过程中,几乎所有开发者都会遇到同一个明显痛点:AI虽然具备强大的代码生成与分析能力,却常常陷入盲目探索的循环中。
670 1
|
20天前
|
人工智能 开发工具 iOS开发
Claude Code 新手完全上手指南:安装、国产模型配置与常用命令全解
Claude Code 是一款运行在终端环境中的 AI 编程助手,能够直接在命令行中完成代码生成、项目分析、文件修改、命令执行、Git 管理等开发全流程工作。它最大的特点是**任务驱动、终端原生、轻量高效、多模型兼容**,无需图形界面、不依赖 IDE 插件,能够深度融入开发者日常工作流。
3762 15
|
17天前
|
人工智能 Linux BI
国内用 Claude Code 终于不用翻墙了:一行命令搞定,自动接 DeepSeek
JeecgBoot AI专题研究 一键脚本:Claude Code + JeecgBoot Skills + DeepSeek 全平台接入 一行命令装好 Claude Code + JeecgBoot Skills + DeepSeek 接入,无需翻墙使用 Claude Code,支持 Wind
3404 10
国内用 Claude Code 终于不用翻墙了:一行命令搞定,自动接 DeepSeek

热门文章

最新文章