--- title: "期望找到指定字段但未找到 - 如何解决此 Elasticsearch 异常" date: 2026-01-04 lastmod: 2026-01-04 description: "当 Elasticsearch 期望在请求体中找到特定字段但未找到任何字段时,会出现此错误。通常是由于请求格式不正确或字段名称错误导致。" tags: ["脚本错误", "字段解析", "请求格式"] summary: "版本: 6.8-7.15 概述 # 简而言之,当 Elasticsearch 期望在请求体中找到特定字段但未找到任何字段时,会出现此错误。这可能是由于请求格式不正确或字段名称错误导致的。要解决此问题,首先应该验证请求的结构,确保它符合预期的格式。其次,检查请求中的字段名称以确保它们是正确的。如果错误仍然存在,请检查您的 Elasticsearch 版本,因为某些字段在旧版本中可能不可用。 日志上下文 # 日志 “Expected one of [{}] or [{}] fields; but found none”(期望找到 [{}] 或 [{}] 字段,但未找到任何字段)的类名是 Script.java。我们从 Elasticsearch 源代码中提取了以下内容,供那些需要深入了解上下文的用户参考: deprecationLogger.deprecate(DeprecationCategory.SCRIPTING, "script_unsupported_fields", "script section does not support [" + parameterName + "]"); } } if (script == null) { throw new ElasticsearchParseException("Expected one of [{}] or [{}] fields; but found none", ScriptType.INLINE.getParseField().getPreferredName(), ScriptType.STORED.getParseField().getPreferredName()); } assert type != null : "if script is not null, type should definitely not be null"; if (type == ScriptType." --- > **版本:** 6.8-7.15 ## 概述 简而言之,当 Elasticsearch 期望在请求体中找到特定字段但未找到任何字段时,会出现此错误。这可能是由于请求格式不正确或字段名称错误导致的。要解决此问题,首先应该验证请求的结构,确保它符合预期的格式。其次,检查请求中的字段名称以确保它们是正确的。如果错误仍然存在,请检查您的 Elasticsearch 版本,因为某些字段在旧版本中可能不可用。 ## 日志上下文 日志 "Expected one of [{}] or [{}] fields; but found none"(期望找到 [{}] 或 [{}] 字段,但未找到任何字段)的类名是 [Script.java](https://www.geeksforgeeks.org/java-lang-class-class-java-set-1/)。我们从 Elasticsearch 源代码中提取了以下内容,供那些需要深入了解上下文的用户参考: ```java deprecationLogger.deprecate(DeprecationCategory.SCRIPTING, "script_unsupported_fields", "script section does not support [" + parameterName + "]"); } } if (script == null) { throw new ElasticsearchParseException("Expected one of [{}] or [{}] fields; but found none", ScriptType.INLINE.getParseField().getPreferredName(), ScriptType.STORED.getParseField().getPreferredName()); } assert type != null : "if script is not null, type should definitely not be null"; if (type == ScriptType.STORED) { ``` ## 解决方案 ### 1. 检查脚本请求格式 在使用脚本功能时,必须指定以下字段之一: - **inline** 或 **source**:内联脚本 - **id** 或 **stored**:存储的脚本 正确的请求格式示例: ```json { "script": { "inline": "ctx._source.views += params.inc", "lang": "painless", "params": { "inc": 1 } } } ``` 或使用存储的脚本: ```json { "script": { "id": "my_increment_script", "params": { "inc": 1 } } } ``` ### 2. 验证字段名称 确保使用正确的字段名称。在不同版本的 Elasticsearch 中,字段名称可能有所变化: - 旧版本使用 `inline` 和 `stored` - 较新版本推荐使用 `source` 替代 `inline` ### 3. 检查 Elasticsearch 版本兼容性 某些字段在不同版本中可能有不同的名称或行为。确保您的请求与所使用的 Elasticsearch 版本兼容。 ### 4. 常见错误示例 以下是一个会触发此错误的请求示例: ```json { "script": { "lang": "painless", "params": { "inc": 1 } } } ``` **问题:** 缺少 `inline`/`source` 或 `id`/`stored` 字段。 **修正:** 添加必需的脚本字段。 ## 总结 此错误通常在脚本相关操作中出现,确保您的脚本请求包含了必需的字段(`inline`/`source` 或 `id`/`stored`),并检查请求格式是否符合 Elasticsearch 的要求。