--- title: "模板名称必须设置映射或运行时字段 - 如何解决此 Elasticsearch 异常" date: 2026-01-28 lastmod: 2026-01-28 description: "在 Elasticsearch 中创建索引模板时未指定映射或运行时字段会导致异常。本文介绍该错误的原因及解决方案。" tags: ["索引模板", "映射配置", "运行时字段", "动态模板"] summary: "版本: 7.11-8.9 简要来说,当创建 Elasticsearch 索引模板时未指定映射(mapping)或运行时(runtime)字段时,就会出现此错误。Elasticsearch 中的索引模板至少需要其中之一来定义索引中的字段应如何存储和搜索。要解决此问题,您可以在索引模板中添加映射或运行时字段。映射定义了字段的数据类型,而运行时字段允许您定义将在查询时计算的字段。 日志上下文 # 日志 “template [” + name + “] must have either mapping or runtime set” 的类名是 DynamicTemplate.java。我们从 Elasticsearch 源代码中提取了以下内容,供那些寻求深入上下文的人参考: // so we need to ignore them at parsing time for old indices throw new IllegalArgumentException("Illegal dynamic template parameter: [" + propName + "]"); } } if (mapping == null) { throw new MapperParsingException("template [" + name + "] must have either mapping or runtime set"); } final XContentFieldType[] xContentFieldTypes; if ("*"." --- > **版本:** 7.11-8.9 简要来说,当创建 Elasticsearch 索引模板时未指定映射(mapping)或运行时(runtime)字段时,就会出现此错误。Elasticsearch 中的索引模板至少需要其中之一来定义索引中的字段应如何存储和搜索。要解决此问题,您可以在索引模板中添加映射或运行时字段。映射定义了字段的数据类型,而运行时字段允许您定义将在查询时计算的字段。 ## 日志上下文 日志 "template [" + name + "] must have either mapping or runtime set" 的类名是 [DynamicTemplate.java](https://www.geeksforgeeks.org/java-lang-class-class-java-set-1/)。我们从 Elasticsearch 源代码中提取了以下内容,供那些寻求深入上下文的人参考: ```java // so we need to ignore them at parsing time for old indices throw new IllegalArgumentException("Illegal dynamic template parameter: [" + propName + "]"); } } if (mapping == null) { throw new MapperParsingException("template [" + name + "] must have either mapping or runtime set"); } final XContentFieldType[] xContentFieldTypes; if ("*".equals(matchMappingType) || (matchMappingType == null && matchPatternsAreDefined(match; pathMatch))) { if (runtime) { ```