diff --git a/server/src/internalClusterTest/java/org/elasticsearch/search/source/MetadataFetchingIT.java b/server/src/internalClusterTest/java/org/elasticsearch/search/source/MetadataFetchingIT.java index 2bb61edeed4..bdddca99328 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/search/source/MetadataFetchingIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/search/source/MetadataFetchingIT.java @@ -132,7 +132,7 @@ public class MetadataFetchingIT extends ESIntegTestCase { assertNotNull(rootCause); assertThat(rootCause.getClass(), equalTo(SearchException.class)); assertThat(rootCause.getMessage(), - equalTo("`stored_fields` cannot be disabled if _source is requested")); + equalTo("[stored_fields] cannot be disabled if [_source] is requested")); } { SearchPhaseExecutionException exc = expectThrows(SearchPhaseExecutionException.class, @@ -141,7 +141,16 @@ public class MetadataFetchingIT extends ESIntegTestCase { assertNotNull(rootCause); assertThat(rootCause.getClass(), equalTo(SearchException.class)); assertThat(rootCause.getMessage(), - equalTo("`stored_fields` cannot be disabled if version is requested")); + equalTo("[stored_fields] cannot be disabled if [version] is requested")); + } + { + SearchPhaseExecutionException exc = expectThrows(SearchPhaseExecutionException.class, + () -> client().prepareSearch("test").storedFields("_none_").addFetchField("field").get()); + Throwable rootCause = ExceptionsHelper.unwrap(exc, SearchException.class); + assertNotNull(rootCause); + assertThat(rootCause.getClass(), equalTo(SearchException.class)); + assertThat(rootCause.getMessage(), + equalTo("[stored_fields] cannot be disabled when using the [fields] option")); } { IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, diff --git a/server/src/main/java/org/elasticsearch/search/SearchService.java b/server/src/main/java/org/elasticsearch/search/SearchService.java index 15ebba36734..378c9662343 100644 --- a/server/src/main/java/org/elasticsearch/search/SearchService.java +++ b/server/src/main/java/org/elasticsearch/search/SearchService.java @@ -986,10 +986,13 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv if (source.storedFields() != null) { if (source.storedFields().fetchFields() == false) { if (context.version()) { - throw new SearchException(shardTarget, "`stored_fields` cannot be disabled if version is requested"); + throw new SearchException(shardTarget, "[stored_fields] cannot be disabled if [version] is requested"); } if (context.sourceRequested()) { - throw new SearchException(shardTarget, "`stored_fields` cannot be disabled if _source is requested"); + 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());