From 1bb82ed5a2f50a60a987694ee5cc87a11fe737f9 Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Thu, 4 Apr 2024 10:43:10 +0200 Subject: [PATCH] expose method to indicate whether a field should be fetched via wildcard pattern --- .../index/mapper/DataStreamTimestampFieldMapper.java | 5 +++++ .../elasticsearch/index/mapper/DocCountFieldMapper.java | 5 +++++ .../elasticsearch/index/mapper/FieldNamesFieldMapper.java | 5 +++++ .../org/elasticsearch/index/mapper/IndexFieldMapper.java | 4 ++++ .../org/elasticsearch/index/mapper/MappedFieldType.java | 7 +++++++ .../elasticsearch/index/mapper/NestedPathFieldMapper.java | 5 +++++ .../org/elasticsearch/index/mapper/SeqNoFieldMapper.java | 5 +++++ .../org/elasticsearch/index/mapper/VersionFieldMapper.java | 5 +++++ .../search/fetch/subphase/StoredFieldsPhase.java | 4 +++- 9 files changed, 44 insertions(+), 1 deletion(-) 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 ece0b082a3ccf..09c7a11e63fab 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DataStreamTimestampFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DataStreamTimestampFieldMapper.java @@ -69,6 +69,11 @@ public Query existsQuery(SearchExecutionContext context) { public ValueFetcher valueFetcher(SearchExecutionContext context, String format) { throw new UnsupportedOperationException(); } + + @Override + public boolean isFetchedViaWildcardExpression() { + return false; + } } private static DataStreamTimestampFieldMapper toType(FieldMapper in) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/DocCountFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/DocCountFieldMapper.java index af341e64661d1..633adcc27ee51 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DocCountFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DocCountFieldMapper.java @@ -85,6 +85,11 @@ protected Object parseSourceValue(Object value) { } }; } + + @Override + public boolean isFetchedViaWildcardExpression() { + return false; + } } private DocCountFieldMapper() { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java index 57a4885d44d5a..e3a9aef5f26a7 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java @@ -154,6 +154,11 @@ public Query termQuery(Object value, SearchExecutionContext context) { ); return super.termQuery(value, context); } + + @Override + public boolean isFetchedViaWildcardExpression() { + return false; + } } private final Explicit enabled; diff --git a/server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java index 1b2667fe9d2ea..04c9f8902b2c6 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java @@ -101,6 +101,10 @@ public StoredFieldsSpec storedFieldsSpec() { }; } + @Override + public boolean isFetchedViaWildcardExpression() { + return false; + } } public IndexFieldMapper() { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java index a554e6e44a8e8..8ab30e47cac2b 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java @@ -665,6 +665,13 @@ public boolean fieldHasValue(FieldInfos fieldInfos) { return fieldInfos.fieldInfo(name()) != null; } + /** + * Whether the field needs to be fetched when a wildcard expression is provided via `stored_fields` option + */ + public boolean isFetchedViaWildcardExpression() { + return true; + } + /** * Returns a loader for ESQL or {@code null} if the field doesn't support * ESQL. 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 6a0f4a87eb890..bdf61c6b5f148 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/NestedPathFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/NestedPathFieldMapper.java @@ -69,6 +69,11 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format) throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } + @Override + public boolean isFetchedViaWildcardExpression() { + return false; + } + @Override public boolean mayExistInIndex(SearchExecutionContext context) { return false; 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 2635c1c11be8e..fadaad933580c 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SeqNoFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SeqNoFieldMapper.java @@ -170,6 +170,11 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format) throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } + @Override + public boolean isFetchedViaWildcardExpression() { + return false; + } + @Override public Query termQuery(Object value, @Nullable SearchExecutionContext context) { long v = parse(value); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/VersionFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/VersionFieldMapper.java index 0d4f5562d3046..430b187e441d8 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/VersionFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/VersionFieldMapper.java @@ -58,6 +58,11 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format) return new DocValueFetcher(docValueFormat(format, null), context.getForField(this, FielddataOperation.SEARCH)); } + @Override + public boolean isFetchedViaWildcardExpression() { + return false; + } + @Override public BlockLoader blockLoader(BlockLoaderContext blContext) { return new BlockDocValuesReader.LongsBlockLoader(name()); diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/StoredFieldsPhase.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/StoredFieldsPhase.java index d6950df962433..618be15eb941d 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/StoredFieldsPhase.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/StoredFieldsPhase.java @@ -77,10 +77,12 @@ public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext) { Collection fieldNames = sec.getMatchingFieldNames(field); for (String fieldName : fieldNames) { MappedFieldType ft = sec.getFieldType(fieldName); + boolean isMetadataField = sec.isMetadataField(fieldName); + assert isMetadataField == false || (ft.isStored() == ft.isFetchedViaWildcardExpression()) : fieldName; if (ft.isStored() == false) { continue; } - storedFields.add(new StoredField(fieldName, ft, sec.isMetadataField(ft.name()))); + storedFields.add(new StoredField(fieldName, ft, isMetadataField)); fieldsToLoad.add(ft.name()); } }