如何在创建之前检测 Elasticsearch 将使用哪个索引模板

作者:来自 Elastic Musab Dogan

概述

Elasticsearch 提供两种类型的索引模板:旧(legacy)索引模板和可组合 (composable) 索引模板。 Elasticsearch 7.8 中引入的可组合模板旨在替换旧模板,两者仍然可以在 Elasticsearch 8 中使用。

本文探讨了这些模板之间的差异以及它们的交互方式。 特别是,我们将重点关注如何在创建索引时检测将使用哪个模板。 让我们首先了解如何创建不同类型的索引模板。

Elasticsearch 中的索引模板

可以使用以下 API 创建旧模板:

PUT _template/t1
{"order": 1,"index_patterns": [...],"mappings": {...},"settings": {...},"alias": {...}
}

可以使用此 API 创建可组合模板:

PUT _index_template/ct1
{"priority": 1,"index_patterns": [...],"template": {"mappings": {...},"settings": {...},"alias": {...}}
}

组件模板是第三种类型,通常用于管理具有相似结构的多个模板。 例如,如果你需要创建数百个具有相似结构的模板,则可以创建具有通用设置、映射和别名的组件模板,然后将其包含在索引模板中。 可以使用此 API 创建组件模板:

PUT _component_template/template_1
{"template": {"mappings": {...},"settings": {...},"alias": {...}}
}

重要:当旧模板和可组合模板同时存在并且它们匹配相同的索引模式时,旧模板将被忽略。 如果两个可组合模板指向相同的索引模式,则将使用具有最高优先级的模板。 如果两个旧模板指向相同的索引模式,则模板将被合并,高阶模板将覆盖低阶模板。 如果顺序相同,则模板按名称排序并相应合并。

确定创建索引时将使用哪个模板

要确定索引在创建时将使用哪个模板,你可以使用 _simulate_index API。 此 API 将返回将使用的模板以及任何重叠的模板。 但是,如果不存在可组合模板,则 API 将返回空正文。 在这种情况下,你可以创建一个虚拟索引并检查所选主节点的日志以确定将使用哪个模板。

如果你同时拥有旧模板和可组合模板,会发生什么情况?

如上所述,如果你同时拥有旧模板和可组合模板,则旧模板将被忽略,就好像它不存在一样。

PUT _template/t1
{"index_patterns": ["test_index-*"],"mappings": {"properties": {"field_1": {"type": "integer"},"field_2": {"type": "integer"}}}
}

在这种情况下,当你运行命令时,你会收到如下警告消息:

legacy template [t1] has index patterns [test_index-] matching patterns from existing composable templates [ct1] with patterns (ct1 => [test_index-]); this template [t1] may be ignored in favor of a composable template at index creation time

PUT _index_template/ct1
{"index_patterns": ["test_index-*"],"template": {"mappings": {"properties": {"field_1": {"type": "integer"}}},"settings": {"number_of_shards": 1,"number_of_replicas": 0}}
}

如果新创建的可组合模板与现有旧模板匹配或包含索引模式,你将收到如下警告消息:

index template [ct1] has index patterns [test_index-] matching patterns from existing older templates [t1] with patterns (t1 => [test_index-]); this template [ct1] will take precedence during new index creation

POST _index_template/_simulate_index/test_index-1
#response:
{"template": {"settings": {"index": {"number_of_shards": "1","number_of_replicas": "0","routing": {"allocation": {"include": {"_tier_preference": "data_content"}}}}},"mappings": {"properties": {"field_1": {"type": "integer"}}},"aliases": {}},"overlapping": [{"name": "t1","index_patterns": ["test_index-*"]}]
}

如果你想测试它,请使用此命令:

PUT test_index-1
GET test_index-1

更多阅读 “Elasticsearch:Simulate index API”。

现实生活场景的笔记

冲突可能很烦人,并且可能导致应用程序崩溃。 想象一下,你有 logstash-dev-*、logstash-prd-*、logstash-stg-* 旧模板都可以正常工作。 如果有人添加一个包含索引模式(如 logstash-*)的单个可组合模板,所有旧模板都将被忽略,字段类型可能会被更改,最终可能会破坏应用程序。 因此,如果你使用 Elasticsearch 7 及更高版本,建议从旧版模板切换到可组合模板。

另一个需要记住的好点是,如果你在 Elasticsearch 8 或更高版本中运行 Logstash,Logstash 默认情况下会将其模板添加为可组合模板。 因为对于 Elasticsearch 8 及更高版本,manage_template 默认设置为 true,并且 Logstash template_api 设置为可组合。 如果可组合模板不存在,它将创建一个具有 logstash-* 索引模式的 Logstash 可组合模板。 是的,它将忽略所有覆盖 logstash-* 的旧模板并重叠它们。

模板重叠

1. 如果你有两个指向相同索引模式的可组合模板,会发生什么情况?

如前所述,如果你有两个指向相同索引模式的可组合模板,则具有最高优先级的可组合模板将优先。

PUT _index_template/ct1
{"priority": 0,"index_patterns": ["test_index-*"],"template": {"mappings": {"properties": {"field_1": {"type": "integer"}}},"settings": {"number_of_shards": 1,"number_of_replicas": 0}}
}
PUT _index_template/ct2
{"priority": 1,"index_patterns": ["test_index-*"],"template": {"mappings": {"properties": {"field_1": {"type": "keyword"},"field_2": {"type": "integer"}}},"settings": {"number_of_shards": 2,"number_of_replicas": 0}}
}
POST _index_template/_simulate_index/test_index-1
#response:
{"template": {"settings": {"index": {"number_of_shards": "2","number_of_replicas": "0","routing": {"allocation": {"include": {"_tier_preference": "data_content"}}}}},"mappings": {"properties": {"field_1": {"type": "keyword"},"field_2": {"type": "integer"}}},"aliases": {}},"overlapping": [{"name": "ct1","index_patterns": ["test_index-*"]}]
}

在此示例中,你有两个模板 - ct1 和 ct2 - 都针对相同的索引模式 test_index-。 但是,ct2 的优先级 (1) 高于 ct1 (0)。 因此,当你创建与模式 test_index- 匹配的索引时,ct2 中定义的设置和映射将在 ct1 之前应用。 如果 ct1 和 ct2 模板中有相同的设置,则 ct2 模板将被覆盖。

2. 如果你有两个指向相同索引模式的旧模板,会发生什么情况?

如上所述,如果你有多个指向相同索引模式的模板,则首先合并具有较低阶值的模板。 具有较高阶值的模板稍后会合并,覆盖具有较低值的模板。

如果两个旧模板具有相同的顺序值,它们将按名称排序。 例如,在 [t2, t1] 的情况下,t1 将首先合并,t2 将稍后合并,如果存在任何相同的映射/设置/别名,则 t2 将覆盖 t1。

PUT _template/t1
{"index_patterns": ["test_index-*"],"mappings": {"properties": {"field_1": {"type": "integer"}}},"settings": {"number_of_shards": 1,"number_of_replicas": 0}
}
PUT _template/t2
{"index_patterns": ["test_index-*"],"mappings": {"properties": {"field_1": {"type": "geo_point"},"field_2": {"type": "long"}}},"settings": {"number_of_shards": 2,"number_of_replicas": 0}
}
POST _index_template/_simulate_index/test_index-1
#response
{}

不幸的是,如果你没有可组合模板,则此 API 调用将返回空正文。 那么如何检查呢?

答案是创建一个虚拟索引并检查 Elasticsearch elected-master 日志。

PUT test_index-test
2023-11-14 14:14:27 {"@timestamp":"2023-11-14T11:14:27.535Z", "log.level": "WARN",  "data_stream.dataset":"deprecation.elasticsearch","data_stream.namespace":"default","data_stream.type":"logs","elasticsearch.event.category":"templates","event.code":"index_template_multiple_match","message":"index [test_index-1] matches multiple legacy templates [t1, t2], composable templates will only match a single template" , "ecs.version": "1.2.0","service.name":"ES_ECS","event.dataset":"deprecation.elasticsearch","process.thread.name":"elasticsearch[elasticsearch][masterService#updateTask][T#3]","log.logger":"org.elasticsearch.deprecation.cluster.metadata.MetadataCreateIndexService","trace.id":"85e0a432ec11e2f2d3c7883f510376ac","elasticsearch.cluster.uuid":"Jc-a46VUSjOwuxWmbnSDZQ","elasticsearch.node.id":"MTX1x5-OTlWhiGa9lwUJPw","elasticsearch.node.name":"elasticsearch","elasticsearch.cluster.name":"elasticsearch-cluster1"}2023-11-14 14:14:27 {"@timestamp":"2023-11-14T11:14:27.605Z", "log.level": "INFO", "message":"[test_index-1] creating index, cause [api], templates [t2, t1], shards [2]/[0]", "ecs.version": "1.2.0","service.name":"ES_ECS","event.dataset":"elasticsearch.server","process.thread.name":"elasticsearch[elasticsearch][masterService#updateTask][T#3]","log.logger":"org.elasticsearch.cluster.metadata.MetadataCreateIndexService","trace.id":"85e0a432ec11e2f2d3c7883f510376ac","elasticsearch.cluster.uuid":"Jc-a46VUSjOwuxWmbnSDZQ","elasticsearch.node.id":"MTX1x5-OTlWhiGa9lwUJPw","elasticsearch.node.name":"elasticsearch","elasticsearch.cluster.name":"elasticsearch-cluster1"}

从日志中,我们可以看到 “[test_index-1] creating index, cause [api], templates [t2, t1]”。

GET _cat/templates/t*?v
name index_patterns order version composed_of
t2   [test_index-*] 0
t1   [test_index-*] 0

如你所见,旧版模板 t1 和 t2 具有相同的顺序; 那么,哪一个会覆盖另一个呢?

在这种情况下,Elasticsearch 将根据名称对旧索引模板进行排序并应用它们。 将应用两个模板,并且列表中的第一个模板(本例中为 t2)将覆盖该模板。

奖励:如果你有两个旧模板指向具有相同字段名称但类型不合适的相同索引模式,会发生什么情况?

尝试在旧模板中合并属性,无论顺序如何,都可能会失败,因为字段定义应保持原子性。 这个问题是引入新的可组合模板的主要动机。 请参阅下面的示例。 我们感谢 Philipp Krenn 在本文中添加这些评论。

PUT _template/test1
{"order": 3,"index_patterns": ["test-*"],"mappings": {"properties": {"my_field": {"type": "integer","ignore_malformed": true}}}
}
PUT _template/test2
{"order": 2,"index_patterns": ["test-*"],"mappings": {"properties": {"my_field": {"type": "keyword","ignore_above": 1024}}}
}
PUT test-1/_doc/1
{"my_field": "a string..."
}
#response:
{"error": {"root_cause": [{"type": "mapper_parsing_exception","reason": "unknown parameter [ignore_above] on mapper [my_field] of type [integer]"}],"type": "mapper_parsing_exception","reason": "Failed to parse mapping: unknown parameter [ignore_above] on mapper [my_field] of type [integer]","caused_by": {"type": "mapper_parsing_exception","reason": "unknown parameter [ignore_above] on mapper [my_field] of type [integer]"}},"status": 400
}

注意和最好需要知道的事情

  1. 以相同的顺序使用旧模板可能会导致很多混乱。 这就是为什么建议向模板添加 order 的原因。
  2. 具有较低 order 的模板首先被合并。 具有较高顺序值的模板稍后会被合并,覆盖具有较低 order 的模板。
  3. 你无法创建两个具有相同优先级的可组合模板。
{"type": "illegal_argument_exception","reason": "index template [ct2] has index patterns [test_index-*] matching patterns from existing templates [ct1] with patterns (ct1 => [test_index-*]) that have the same priority [0], multiple index templates may not match during index creation, please use a different priority"
}

结论

总之,了解 Elasticsearch 索引模板的工作原理对于有效的索引管理至关重要。 通过了解如何确定索引在创建时将使用哪个模板,你可以确保使用正确的设置、映射和别名创建索引。

原文:Elasticsearch Index Template - Detect which template will be used before creation — Elastic Search Labs

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/11752.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深入理解MVCC与Read View:并发控制的关键要素

MVCC MVCC的几个问题1.update、insert、select和delete如何在MVCC中维护版本链?2.select读取,是读取最新的版本呢?还是读取历史版本?3.当前读和快照读4.那为什么要有隔离级别呢?5.如何保证,不同的事务&…

【go从入门到精通】golang单元测试

单元测试 随着软件开发变得越来越复杂,确保服务按预期运行至关重要。实现这一目标的方法之一是通过测试。在Go中,我们可以进行单元测试和集成测试,以确保我们的代码正确并满足要求。单元测试的最终目标是断言一小部分逻辑“单元”的行为符合预期。我们通过在测试中调用该函数…

Nginx - 配置文件结构(一)

安装Nginx 以 Ubuntu 为例,安装命令为 sudo apt install nginx常用指令 # 检查配置文件是否有问题 nginx -t# 热加载配置文件 nginx -s reload# 等待处理完当前请求并退出 nginx -s quit# 快速退出 nginx -s stop目录结构 nginx 默认安装位置一般在 /etc/nginx …

组件通信总结

组件通信是前端开发中的一个重要概念,它指的是组件之间通过某种方式来传递信息以达到某个目的。以下是对组件通信的总结: 一、组件间通信的分类 父子组件间通信:这是最常见的组件通信场景,主要使用自定义属性(props&…

Automa:一键自动化,网页数据采集与工作流程优化专家

Automa:解锁自动化浏览器潜能,赋能工作效率,让复杂任务变得简单- 精选真开源,释放新价值。 概览 Automa是一款创新的网页自动化工具,专为寻求提升工作效率、简化数据收集过程的现代工作者设计。它融合了先进的数据抓取…

模板:vector(顺序表容器)

1.构造函数 explicit vector (const allocator_type& alloc allocator_type()); //默认构造函数explicit vector (size_type n, const value_type& val value_type(),const allocator_type& alloc allocator_type()); //n个重复的valtemplate <class Input…

Angular入门

Angular版本&#xff1a;Angular 版本演进史概述-天翼云开发者社区 - 天翼云 安装nodejs&#xff1a;Node.js安装与配置环境 v20.13.1(LTS)-CSDN博客 Angular CLI是啥 Angular CLI 是一个命令行接口(Angular Command Line Interface)&#xff0c;是开发 Angular 应用的最快、最…

大模型时代下两种few shot高效文本分类方法

介绍近年(2022、2024)大语言模型盛行下的两篇文本分类相关的论文&#xff0c;适用场景为few shot。两种方法分别是setfit和fastfit&#xff0c;都提供了python的包使用方便。 论文1&#xff1a;Efficient Few-Shot Learning Without Prompts 题目&#xff1a;无需提示的高效少…

深入了解 MyBatis 插件:定制化你的持久层框架

序言 MyBatis 是一个流行的 Java 持久层框架&#xff0c;它提供了简单而强大的数据库访问功能。然而&#xff0c;有时候我们需要在 MyBatis 中添加一些自定义的功能或行为&#xff0c;来满足特定的需求。这时&#xff0c;MyBatis 插件就发挥了重要作用。本文将深入探讨 MyBati…

如何创建 Django 模型

简介 在上一篇教程“如何创建 Django 应用程序并将其连接到数据库”中&#xff0c;我们介绍了如何创建一个 MySQL 数据库&#xff0c;如何创建和启动一个 Django 应用程序&#xff0c;以及如何将其连接到一个 MySQL 数据库。 在本教程中&#xff0c;我们将创建 Django 模型&a…

孩子如何备考编程竞赛

为了帮助孩子更好地备考编程竞赛&#xff0c;家长和老师还可以采取以下一些措施&#xff1a; 制定合理的学习计划&#xff1a;家长和孩子可以一起制定一个合理的学习计划&#xff0c;包括每天的学习时间安排、学习内容和目标等。通过制定学习计划&#xff0c;可以帮助孩子更有条…

An 2024下载

An2024下载&#xff1a; 百度网盘下载https://pan.baidu.com/s/1cQQCFL16OUY1G6uQWgDbSg?pwdSIMS Adobe Animate 2024&#xff0c;作为Flash技术的进化顶点&#xff0c;是Adobe匠心打造的动画与交互内容创作的旗舰软件。这款工具赋予设计师与开发者前所未有的创意自由&#x…

HIVE卡口流量需求分析

HIVE卡口流量需求分析 目录 HIVE卡口流量需求分析 1.创建表格 插入数据 2.需求 3.总结&#xff1a; 1.创建表格 插入数据 CREATE TABLE learn3.veh_pass( id STRING COMMENT "卡口编号", pass_time STRING COMMENT "进过时间", pass_num int COMMENT …

huggingface 笔记:AutoClass (quick tour 部分)

AutoClass 是一个快捷方式&#xff0c;它可以自动从模型的名称或路径检索预训练模型的架构。只需要为任务选择适当的 AutoClass 及其关联的预处理类。 1 AutoTokenizer 分词器负责将文本预处理成模型输入的数字数组。控制分词过程的规则有多种&#xff0c;包括如何分割单词以…

【iOS】架构模式

文章目录 前言一、MVC二、MVP三、MVVM 前言 之前写项目一直用的是MVC架构&#xff0c;现在来学一下MVP与MVVM两种架构&#xff0c;当然还有VIPER架构&#xff0c;如果有时间后面会单独学习 一、MVC MVC架构先前已经详细讲述&#xff0c;这里不再赘述&#xff0c;我们主要讲一…

Golang | Leetcode Golang题解之第87题扰乱字符串

题目&#xff1a; 题解&#xff1a; func isScramble(s1, s2 string) bool {n : len(s1)dp : make([][][]int8, n)for i : range dp {dp[i] make([][]int8, n)for j : range dp[i] {dp[i][j] make([]int8, n1)for k : range dp[i][j] {dp[i][j][k] -1}}}// 第一个字符串从 …

【SAP ABAP学习资料】通过RFC接口上传图片至SAP 图片格式转换 图片大小调整

SAP图片相关&#xff1a; 链接: 【SAP ABAP学习资料】图片上传SAP 链接: 【SAP ABAP学习资料】屏幕图片预览 链接: 【SAP ABAP学习资料】smartforms打印图片&#xff0c;动态打印图片 需求&#xff1a; SAP上传图片只能本地电脑选择图片通过SE78或PERFORM IMPORT_BITMAP_BDS上…

linux程序分析命令(三)

linux程序分析命令(三) **ldd&#xff1a;**用于打印共享库依赖。这个命令会显示出一个可执行文件所依赖的所有共享库&#xff08;动态链接库&#xff09;&#xff0c;这对于解决运行时库依赖问题非常有用。**nm&#xff1a;**用于列出对象文件的符号表。这个命令可以显示出定…

Milvus入门初探

引言 Milvus 是一款开源的向量数据库&#xff0c;专为处理向量搜索任务而设计。它支持多种类型的向量&#xff0c;如浮点向量、二进制向量等&#xff0c;并且可以处理大规模的向量数据。Milvus 在 AI 应用中非常流行&#xff0c;尤其是在需要执行相似性搜索或最近邻搜索的场景…

【超详细】跑通YOLOv8之深度学习环境配置3-YOLOv8安装

环境配置3下载安装内容如下&#xff1a; 1、配置清华等镜像源 2、创建环境 3、下载安装Pytorch 4、下载安装YOLOv8运行环境 版本&#xff1a;Python3.8&#xff08;要求>3.8&#xff09;&#xff0c;torch1.12.0cu113&#xff08;要求>1.8&#xff09; 1、配置清华等镜…