diff --git a/server/src/main/java/org/elasticsearch/index/mapper/DataStreamTimestampFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/DataStreamTimestampFieldMapper.java index 51b62f6c56533..ece0b082a3ccf 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DataStreamTimestampFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DataStreamTimestampFieldMapper.java @@ -67,7 +67,7 @@ public Query existsQuery(SearchExecutionContext context) { @Override public ValueFetcher valueFetcher(SearchExecutionContext context, String format) { - return new StoredValueFetcher(context.lookup(), NAME); + throw new UnsupportedOperationException(); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/NestedPathFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/NestedPathFieldMapper.java index 3513d7c4066a3..6a0f4a87eb890 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/NestedPathFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/NestedPathFieldMapper.java @@ -66,7 +66,7 @@ public Query existsQuery(SearchExecutionContext context) { @Override public ValueFetcher valueFetcher(SearchExecutionContext context, String format) { - return new StoredValueFetcher(context.lookup(), NAME); + throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SeqNoFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/SeqNoFieldMapper.java index fdc75a8930545..2635c1c11be8e 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SeqNoFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SeqNoFieldMapper.java @@ -167,7 +167,7 @@ public boolean mayExistInIndex(SearchExecutionContext context) { @Override public ValueFetcher valueFetcher(SearchExecutionContext context, String format) { - return new StoredValueFetcher(context.lookup(), NAME); + throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } @Override diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchFieldsPhase.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchFieldsPhase.java index d771d65d847d5..e5b95893a30fd 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchFieldsPhase.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchFieldsPhase.java @@ -10,9 +10,9 @@ import org.apache.lucene.index.LeafReaderContext; import org.elasticsearch.common.document.DocumentField; -import org.elasticsearch.index.mapper.FieldNamesFieldMapper; import org.elasticsearch.index.mapper.IgnoredFieldMapper; import org.elasticsearch.index.mapper.LegacyTypeFieldMapper; +import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.RoutingFieldMapper; import org.elasticsearch.index.mapper.SourceFieldMapper; import org.elasticsearch.index.query.SearchExecutionContext; @@ -73,8 +73,12 @@ public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext) { Collection fieldNames = searchExecutionContext.getMatchingFieldNames(field); for (String fieldName : fieldNames) { if (fieldName.equals(SourceFieldMapper.NAME) == false && searchExecutionContext.isMetadataField(fieldName)) { - // TODO only metadata field that's not stored, it should be excluded in some other way? - if (fieldName.equals(FieldNamesFieldMapper.NAME) == false) { + MappedFieldType fieldType = searchExecutionContext.getFieldType(fieldName); + // TODO this is for bw comp with previous behaviour of _stored_fields: there's a bunch of metadata fields that + // are stored and were never exposed to fetch. + // Some are fetchable via doc_values, but they can't be fetched either from fields, hence it makes sense to keep + // this conditional for the time being? + if (fieldType.isStored()) { fields.add(new FieldAndFormat(fieldName, null)); } } diff --git a/server/src/test/java/org/elasticsearch/search/fetch/subphase/FieldFetcherTests.java b/server/src/test/java/org/elasticsearch/search/fetch/subphase/FieldFetcherTests.java index 569627d6f82b0..e8e811b9ef57b 100644 --- a/server/src/test/java/org/elasticsearch/search/fetch/subphase/FieldFetcherTests.java +++ b/server/src/test/java/org/elasticsearch/search/fetch/subphase/FieldFetcherTests.java @@ -27,9 +27,7 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.MapperServiceTestCase; -import org.elasticsearch.index.mapper.NestedPathFieldMapper; import org.elasticsearch.index.mapper.ParsedDocument; -import org.elasticsearch.index.mapper.SeqNoFieldMapper; import org.elasticsearch.index.mapper.SourceFieldMapper; import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.script.Script; @@ -263,10 +261,7 @@ public void testMetadataFields() throws IOException { }); // several other metadata fields throw exceptions via their value fetchers when trying to get them - for (String fieldname : List.of( - SourceFieldMapper.NAME, - FieldNamesFieldMapper.NAME - )) { + for (String fieldname : List.of(SourceFieldMapper.NAME, FieldNamesFieldMapper.NAME)) { expectThrows(UnsupportedOperationException.class, () -> fetchFields(mapperService, source, fieldname)); } }