--- title: "使用 fields 选项时无法禁用 stored_fields – 如何解决此 Elasticsearch 异常" date: 2026-03-21 lastmod: 2026-03-21 description: "在Elasticsearch中使用fields选项时尝试禁用stored_fields会导致异常,本文介绍如何解决这个问题。" tags: ["存储字段", "fields选项", "异常处理", "查询配置"] summary: "版本: 7.1-8.9 简要来说,当您在 Elasticsearch 中尝试在使用 [fields] 选项的同时禁用 [stored_fields] 时,会出现此错误。[fields] 选项依赖于 [stored_fields] 来检索数据,因此无法禁用它。要解决此问题,您可以启用 [stored_fields] 或避免使用 [fields] 选项。另外,如果您想检索索引时的原始数据,可以使用 [_source] 字段代替 [fields]。 日志上下文 # 日志"[stored_fields] cannot be disabled when using the [fields] option"的类名是 SearchService.java. 我们从 Elasticsearch 源代码中提取了以下内容,供那些寻求深入理解的人参考: if (source.storedFields().fetchFields() == false) { if (context.sourceRequested()) { throw new SearchException(shardTarget; "[stored_fields] cannot be disabled if [_source] is requested"); } if (context.fetchFieldsContext() != null) { throw new SearchException(shardTarget; "[stored_fields] cannot be disabled when using the [fields] option"); } } context." --- > **版本:** 7.1-8.9 简要来说,当您在 Elasticsearch 中尝试在使用 [fields] 选项的同时禁用 [stored_fields] 时,会出现此错误。[fields] 选项依赖于 [stored_fields] 来检索数据,因此无法禁用它。要解决此问题,您可以启用 [stored_fields] 或避免使用 [fields] 选项。另外,如果您想检索索引时的原始数据,可以使用 [_source] 字段代替 [fields]。 日志上下文 ----------- 日志"[stored\_fields] cannot be disabled when using the [fields] option"的类名是 [SearchService.java.](https://www.geeksforgeeks.org/java-lang-class-class-java-set-1/) 我们从 Elasticsearch 源代码中提取了以下内容,供那些寻求深入理解的人参考: ```java if (source.storedFields().fetchFields() == false) { if (context.sourceRequested()) { throw new SearchException(shardTarget; "[stored_fields] cannot be disabled if [_source] is requested"); } if (context.fetchFieldsContext() != null) { throw new SearchException(shardTarget; "[stored_fields] cannot be disabled when using the [fields] option"); } } context.storedFieldsContext(source.storedFields()); } ```