diff --git a/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/RankFeatureFieldMapper.java b/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/RankFeatureFieldMapper.java index 6f64be86f93..31fef730147 100644 --- a/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/RankFeatureFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/RankFeatureFieldMapper.java @@ -135,11 +135,11 @@ public class RankFeatureFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { if (format != null) { throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); } - return new SourceValueFetcher(name(), mapperService) { + return new SourceValueFetcher(name(), context) { @Override protected Float parseSourceValue(Object value) { return objectToFloat(value); diff --git a/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/RankFeatureMetaFieldMapper.java b/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/RankFeatureMetaFieldMapper.java index dd29826af3a..b9effebeb9e 100644 --- a/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/RankFeatureMetaFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/RankFeatureMetaFieldMapper.java @@ -65,7 +65,7 @@ public class RankFeatureMetaFieldMapper extends MetadataFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for internal field [" + typeName() + "]."); } diff --git a/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/RankFeaturesFieldMapper.java b/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/RankFeaturesFieldMapper.java index b35f790901f..43853eb40f4 100644 --- a/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/RankFeaturesFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/RankFeaturesFieldMapper.java @@ -105,8 +105,8 @@ public class RankFeaturesFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - return SourceValueFetcher.identity(name(), mapperService, format); + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { + return SourceValueFetcher.identity(name(), context, format); } @Override diff --git a/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/ScaledFloatFieldMapper.java b/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/ScaledFloatFieldMapper.java index 93f3adf4768..78a9e389eb6 100644 --- a/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/ScaledFloatFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/ScaledFloatFieldMapper.java @@ -260,11 +260,11 @@ public class ScaledFloatFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { if (format != null) { throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); } - return new SourceValueFetcher(name(), mapperService) { + return new SourceValueFetcher(name(), context) { @Override protected Double parseSourceValue(Object value) { double doubleValue; diff --git a/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/SearchAsYouTypeFieldMapper.java b/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/SearchAsYouTypeFieldMapper.java index 6c55c2ecd0f..7bf102584a3 100644 --- a/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/SearchAsYouTypeFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/SearchAsYouTypeFieldMapper.java @@ -329,8 +329,8 @@ public class SearchAsYouTypeFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - return SourceValueFetcher.toString(name(), mapperService, format); + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { + return SourceValueFetcher.toString(name(), context, format); } @Override @@ -439,10 +439,10 @@ public class SearchAsYouTypeFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { // Because this internal field is modelled as a multi-field, SourceValueFetcher will look up its // parent field in _source. So we don't need to use the parent field name here. - return SourceValueFetcher.toString(name(), mapperService, format); + return SourceValueFetcher.toString(name(), context, format); } @Override @@ -545,10 +545,10 @@ public class SearchAsYouTypeFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { // Because this internal field is modelled as a multi-field, SourceValueFetcher will look up its // parent field in _source. So we don't need to use the parent field name here. - return SourceValueFetcher.toString(name(), mapperService, format); + return SourceValueFetcher.toString(name(), context, format); } @Override diff --git a/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/TokenCountFieldMapper.java b/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/TokenCountFieldMapper.java index ed71ffa5158..fd029503e9a 100644 --- a/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/TokenCountFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/opensearch/index/mapper/TokenCountFieldMapper.java @@ -36,6 +36,7 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; import org.opensearch.index.analysis.NamedAnalyzer; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.lookup.SearchLookup; import java.io.IOException; @@ -119,7 +120,7 @@ public class TokenCountFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { if (hasDocValues() == false) { return lookup -> org.opensearch.common.collect.List.of(); } diff --git a/modules/parent-join/src/main/java/org/opensearch/join/mapper/MetaJoinFieldMapper.java b/modules/parent-join/src/main/java/org/opensearch/join/mapper/MetaJoinFieldMapper.java index 4570a67d67e..2b0d3e4764b 100644 --- a/modules/parent-join/src/main/java/org/opensearch/join/mapper/MetaJoinFieldMapper.java +++ b/modules/parent-join/src/main/java/org/opensearch/join/mapper/MetaJoinFieldMapper.java @@ -39,7 +39,6 @@ import org.apache.lucene.util.BytesRef; import org.opensearch.index.fielddata.IndexFieldData; import org.opensearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData; import org.opensearch.index.mapper.FieldMapper; -import org.opensearch.index.mapper.MapperService; import org.opensearch.index.mapper.ParseContext; import org.opensearch.index.mapper.StringFieldType; import org.opensearch.index.mapper.TextSearchInfo; @@ -111,7 +110,7 @@ public class MetaJoinFieldMapper extends FieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for metadata field [" + typeName() + "]."); } diff --git a/modules/parent-join/src/main/java/org/opensearch/join/mapper/ParentIdFieldMapper.java b/modules/parent-join/src/main/java/org/opensearch/join/mapper/ParentIdFieldMapper.java index 4e3d26d73c3..29a02a5bc80 100644 --- a/modules/parent-join/src/main/java/org/opensearch/join/mapper/ParentIdFieldMapper.java +++ b/modules/parent-join/src/main/java/org/opensearch/join/mapper/ParentIdFieldMapper.java @@ -48,11 +48,11 @@ import org.opensearch.index.fielddata.IndexFieldData; import org.opensearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData; import org.opensearch.index.mapper.FieldMapper; import org.opensearch.index.mapper.MappedFieldType; -import org.opensearch.index.mapper.MapperService; import org.opensearch.index.mapper.ParseContext; import org.opensearch.index.mapper.StringFieldType; import org.opensearch.index.mapper.TextSearchInfo; import org.opensearch.index.mapper.ValueFetcher; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.aggregations.support.CoreValuesSourceType; import org.opensearch.search.lookup.SearchLookup; @@ -132,7 +132,7 @@ public final class ParentIdFieldMapper extends FieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for internal field [" + typeName() + "]."); } diff --git a/modules/parent-join/src/main/java/org/opensearch/join/mapper/ParentJoinFieldMapper.java b/modules/parent-join/src/main/java/org/opensearch/join/mapper/ParentJoinFieldMapper.java index 7d34e6d40a7..662bacc0fb0 100644 --- a/modules/parent-join/src/main/java/org/opensearch/join/mapper/ParentJoinFieldMapper.java +++ b/modules/parent-join/src/main/java/org/opensearch/join/mapper/ParentJoinFieldMapper.java @@ -57,6 +57,7 @@ import org.opensearch.index.mapper.SourceValueFetcher; import org.opensearch.index.mapper.StringFieldType; import org.opensearch.index.mapper.TextSearchInfo; import org.opensearch.index.mapper.ValueFetcher; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.aggregations.support.CoreValuesSourceType; import org.opensearch.search.lookup.SearchLookup; @@ -243,8 +244,8 @@ public final class ParentJoinFieldMapper extends FieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - return SourceValueFetcher.identity(name(), mapperService, format); + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { + return SourceValueFetcher.identity(name(), context, format); } @Override diff --git a/modules/percolator/src/main/java/org/opensearch/percolator/PercolatorFieldMapper.java b/modules/percolator/src/main/java/org/opensearch/percolator/PercolatorFieldMapper.java index 72adc5539d6..aaf81ad576f 100644 --- a/modules/percolator/src/main/java/org/opensearch/percolator/PercolatorFieldMapper.java +++ b/modules/percolator/src/main/java/org/opensearch/percolator/PercolatorFieldMapper.java @@ -72,7 +72,6 @@ import org.opensearch.index.mapper.KeywordFieldMapper; import org.opensearch.index.mapper.MappedFieldType; import org.opensearch.index.mapper.Mapper; import org.opensearch.index.mapper.MapperParsingException; -import org.opensearch.index.mapper.MapperService; import org.opensearch.index.mapper.NumberFieldMapper; import org.opensearch.index.mapper.ParametrizedFieldMapper; import org.opensearch.index.mapper.ParseContext; @@ -245,8 +244,8 @@ public class PercolatorFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - return SourceValueFetcher.identity(name(), mapperService, format); + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { + return SourceValueFetcher.identity(name(), context, format); } Query percolateQuery( diff --git a/plugins/analysis-icu/src/main/java/org/opensearch/index/mapper/ICUCollationKeywordFieldMapper.java b/plugins/analysis-icu/src/main/java/org/opensearch/index/mapper/ICUCollationKeywordFieldMapper.java index e6a33c75458..59b01b0ddb4 100644 --- a/plugins/analysis-icu/src/main/java/org/opensearch/index/mapper/ICUCollationKeywordFieldMapper.java +++ b/plugins/analysis-icu/src/main/java/org/opensearch/index/mapper/ICUCollationKeywordFieldMapper.java @@ -120,12 +120,12 @@ public class ICUCollationKeywordFieldMapper extends FieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { if (format != null) { throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); } - return new SourceValueFetcher(name(), mapperService, nullValue) { + return new SourceValueFetcher(name(), context, nullValue) { @Override protected String parseSourceValue(Object value) { String keywordValue = value.toString(); diff --git a/plugins/mapper-annotated-text/src/main/java/org/opensearch/search/fetch/subphase/highlight/AnnotatedTextHighlighter.java b/plugins/mapper-annotated-text/src/main/java/org/opensearch/search/fetch/subphase/highlight/AnnotatedTextHighlighter.java index fe99746a5ce..90d123e86e2 100644 --- a/plugins/mapper-annotated-text/src/main/java/org/opensearch/search/fetch/subphase/highlight/AnnotatedTextHighlighter.java +++ b/plugins/mapper-annotated-text/src/main/java/org/opensearch/search/fetch/subphase/highlight/AnnotatedTextHighlighter.java @@ -40,8 +40,8 @@ import org.opensearch.index.mapper.DocumentMapper; import org.opensearch.index.mapper.MappedFieldType; import org.opensearch.index.mapper.annotatedtext.AnnotatedTextFieldMapper.AnnotatedHighlighterAnalyzer; import org.opensearch.index.mapper.annotatedtext.AnnotatedTextFieldMapper.AnnotatedText; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.fetch.FetchSubPhase.HitContext; -import org.opensearch.search.fetch.subphase.highlight.SearchHighlightContext.Field; import java.io.IOException; import java.util.ArrayList; @@ -55,12 +55,12 @@ public class AnnotatedTextHighlighter extends UnifiedHighlighter { @Override protected List loadFieldValues( CustomUnifiedHighlighter highlighter, + QueryShardContext context, MappedFieldType fieldType, - Field field, HitContext hitContext, boolean forceSource ) throws IOException { - List fieldValues = super.loadFieldValues(highlighter, fieldType, field, hitContext, forceSource); + List fieldValues = super.loadFieldValues(highlighter, context, fieldType, hitContext, forceSource); List strings = new ArrayList<>(fieldValues.size()); AnnotatedText[] annotations = new AnnotatedText[fieldValues.size()]; diff --git a/plugins/mapper-murmur3/src/main/java/org/opensearch/index/mapper/murmur3/Murmur3FieldMapper.java b/plugins/mapper-murmur3/src/main/java/org/opensearch/index/mapper/murmur3/Murmur3FieldMapper.java index ffbb1905bcd..4e87b031320 100644 --- a/plugins/mapper-murmur3/src/main/java/org/opensearch/index/mapper/murmur3/Murmur3FieldMapper.java +++ b/plugins/mapper-murmur3/src/main/java/org/opensearch/index/mapper/murmur3/Murmur3FieldMapper.java @@ -44,7 +44,6 @@ import org.opensearch.index.fielddata.IndexNumericFieldData.NumericType; import org.opensearch.index.fielddata.plain.SortedNumericIndexFieldData; import org.opensearch.index.mapper.FieldMapper; import org.opensearch.index.mapper.MappedFieldType; -import org.opensearch.index.mapper.MapperService; import org.opensearch.index.mapper.ParametrizedFieldMapper; import org.opensearch.index.mapper.ParseContext; import org.opensearch.index.mapper.SourceValueFetcher; @@ -121,8 +120,8 @@ public class Murmur3FieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - return SourceValueFetcher.toString(name(), mapperService, format); + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { + return SourceValueFetcher.toString(name(), context, format); } @Override diff --git a/server/src/internalClusterTest/java/org/opensearch/search/fetch/subphase/highlight/HighlighterSearchIT.java b/server/src/internalClusterTest/java/org/opensearch/search/fetch/subphase/highlight/HighlighterSearchIT.java index dc60f503ab4..1918b43fb78 100644 --- a/server/src/internalClusterTest/java/org/opensearch/search/fetch/subphase/highlight/HighlighterSearchIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/search/fetch/subphase/highlight/HighlighterSearchIT.java @@ -3523,7 +3523,7 @@ public class HighlighterSearchIT extends OpenSearchIntegTestCase { assertHitCount(searchResponse, 1); HighlightField field = searchResponse.getHits().getAt(0).getHighlightFields().get("keyword"); assertThat(field.getFragments().length, equalTo(1)); - assertThat(field.getFragments()[0].string(), equalTo("Hello World")); + assertThat(field.getFragments()[0].string(), equalTo("hello world")); } } diff --git a/server/src/main/java/org/opensearch/index/mapper/AbstractGeometryFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/AbstractGeometryFieldMapper.java index df81984794c..0d90e694ef4 100644 --- a/server/src/main/java/org/opensearch/index/mapper/AbstractGeometryFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/AbstractGeometryFieldMapper.java @@ -284,19 +284,19 @@ public abstract class AbstractGeometryFieldMapper extends Fie } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { String geoFormat = format != null ? format : GeoJsonGeometryFormat.NAME; Function valueParser = value -> geometryParser.parseAndFormatObject(value, geoFormat); if (parsesArrayValue) { - return new ArraySourceValueFetcher(name(), mapperService) { + return new ArraySourceValueFetcher(name(), context) { @Override protected Object parseSourceValue(Object value) { return valueParser.apply(value); } }; } else { - return new SourceValueFetcher(name(), mapperService) { + return new SourceValueFetcher(name(), context) { @Override protected Object parseSourceValue(Object value) { return valueParser.apply(value); diff --git a/server/src/main/java/org/opensearch/index/mapper/AllFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/AllFieldMapper.java index 6059a65c2b3..634424d6f45 100644 --- a/server/src/main/java/org/opensearch/index/mapper/AllFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/AllFieldMapper.java @@ -100,7 +100,7 @@ public class AllFieldMapper extends MetadataFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { throw new UnsupportedOperationException(); } diff --git a/server/src/main/java/org/opensearch/index/mapper/ArraySourceValueFetcher.java b/server/src/main/java/org/opensearch/index/mapper/ArraySourceValueFetcher.java index e74e019e3d6..f28e4a64eec 100644 --- a/server/src/main/java/org/opensearch/index/mapper/ArraySourceValueFetcher.java +++ b/server/src/main/java/org/opensearch/index/mapper/ArraySourceValueFetcher.java @@ -33,6 +33,7 @@ package org.opensearch.index.mapper; import org.opensearch.common.Nullable; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.lookup.SourceLookup; import java.util.ArrayList; @@ -51,17 +52,17 @@ public abstract class ArraySourceValueFetcher implements ValueFetcher { private final Set sourcePaths; private final @Nullable Object nullValue; - public ArraySourceValueFetcher(String fieldName, MapperService mapperService) { - this(fieldName, mapperService, null); + public ArraySourceValueFetcher(String fieldName, QueryShardContext context) { + this(fieldName, context, null); } /** * @param fieldName The name of the field. - * @param mapperService A mapper service. + * @param context A query shard context. * @param nullValue A optional substitute value if the _source value is 'null'. */ - public ArraySourceValueFetcher(String fieldName, MapperService mapperService, Object nullValue) { - this.sourcePaths = mapperService.sourcePath(fieldName); + public ArraySourceValueFetcher(String fieldName, QueryShardContext context, Object nullValue) { + this.sourcePaths = context.sourcePath(fieldName); this.nullValue = nullValue; } diff --git a/server/src/main/java/org/opensearch/index/mapper/BinaryFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/BinaryFieldMapper.java index e55e339c3fd..68b0df63f2b 100644 --- a/server/src/main/java/org/opensearch/index/mapper/BinaryFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/BinaryFieldMapper.java @@ -117,8 +117,8 @@ public class BinaryFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - return SourceValueFetcher.identity(name(), mapperService, format); + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { + return SourceValueFetcher.identity(name(), context, format); } @Override diff --git a/server/src/main/java/org/opensearch/index/mapper/BooleanFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/BooleanFieldMapper.java index 6933175d4ef..a14364640d6 100644 --- a/server/src/main/java/org/opensearch/index/mapper/BooleanFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/BooleanFieldMapper.java @@ -159,12 +159,12 @@ public class BooleanFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { if (format != null) { throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); } - return new SourceValueFetcher(name(), mapperService, nullValue) { + return new SourceValueFetcher(name(), context, nullValue) { @Override protected Boolean parseSourceValue(Object value) { if (value instanceof Boolean) { diff --git a/server/src/main/java/org/opensearch/index/mapper/CompletionFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/CompletionFieldMapper.java index cd93a7a18b0..5d0e2349aca 100644 --- a/server/src/main/java/org/opensearch/index/mapper/CompletionFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/CompletionFieldMapper.java @@ -54,6 +54,7 @@ import org.opensearch.common.xcontent.XContentParser.NumberType; import org.opensearch.common.xcontent.XContentParser.Token; import org.opensearch.index.analysis.AnalyzerScope; import org.opensearch.index.analysis.NamedAnalyzer; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.lookup.SearchLookup; import org.opensearch.search.suggest.completion.CompletionSuggester; import org.opensearch.search.suggest.completion.context.ContextMapping; @@ -363,12 +364,12 @@ public class CompletionFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { if (format != null) { throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); } - return new ArraySourceValueFetcher(name(), mapperService) { + return new ArraySourceValueFetcher(name(), context) { @Override protected List parseSourceValue(Object value) { if (value instanceof List) { diff --git a/server/src/main/java/org/opensearch/index/mapper/DataStreamFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/DataStreamFieldMapper.java index 8961987bca8..03e792c4893 100644 --- a/server/src/main/java/org/opensearch/index/mapper/DataStreamFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/DataStreamFieldMapper.java @@ -66,7 +66,7 @@ public class DataStreamFieldMapper extends MetadataFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for internal field [" + typeName() + "]"); } diff --git a/server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java index ffd7f908217..9aa7b019a8e 100644 --- a/server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java @@ -370,13 +370,13 @@ public final class DateFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { DateFormatter defaultFormatter = dateTimeFormatter(); DateFormatter formatter = format != null ? DateFormatter.forPattern(format).withLocale(defaultFormatter.locale()) : defaultFormatter; - return new SourceValueFetcher(name(), mapperService, nullValue) { + return new SourceValueFetcher(name(), context, nullValue) { @Override public String parseSourceValue(Object value) { String date = value.toString(); diff --git a/server/src/main/java/org/opensearch/index/mapper/FieldNamesFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/FieldNamesFieldMapper.java index b038de69b2e..500df197f4f 100644 --- a/server/src/main/java/org/opensearch/index/mapper/FieldNamesFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/FieldNamesFieldMapper.java @@ -139,7 +139,7 @@ public class FieldNamesFieldMapper extends MetadataFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup lookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup lookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } diff --git a/server/src/main/java/org/opensearch/index/mapper/IdFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/IdFieldMapper.java index 6057c418016..7ca7b9e81be 100644 --- a/server/src/main/java/org/opensearch/index/mapper/IdFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/IdFieldMapper.java @@ -132,7 +132,7 @@ public class IdFieldMapper extends MetadataFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup lookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup lookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } diff --git a/server/src/main/java/org/opensearch/index/mapper/IgnoredFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/IgnoredFieldMapper.java index ff26f18b1f6..0d82b20cfae 100644 --- a/server/src/main/java/org/opensearch/index/mapper/IgnoredFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/IgnoredFieldMapper.java @@ -90,7 +90,7 @@ public final class IgnoredFieldMapper extends MetadataFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup lookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup lookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } } diff --git a/server/src/main/java/org/opensearch/index/mapper/IndexFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/IndexFieldMapper.java index d7ebdc4c1c2..ae32aa52162 100644 --- a/server/src/main/java/org/opensearch/index/mapper/IndexFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/IndexFieldMapper.java @@ -86,7 +86,7 @@ public class IndexFieldMapper extends MetadataFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } } diff --git a/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java index dcd68d77813..3ba5e1e0dfe 100644 --- a/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java @@ -188,11 +188,11 @@ public class IpFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { if (format != null) { throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); } - return new SourceValueFetcher(name(), mapperService, nullValue) { + return new SourceValueFetcher(name(), context, nullValue) { @Override protected Object parseSourceValue(Object value) { InetAddress address; diff --git a/server/src/main/java/org/opensearch/index/mapper/KeywordFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/KeywordFieldMapper.java index 3192a8d709f..4206b29e234 100644 --- a/server/src/main/java/org/opensearch/index/mapper/KeywordFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/KeywordFieldMapper.java @@ -45,6 +45,7 @@ import org.opensearch.index.analysis.IndexAnalyzers; import org.opensearch.index.analysis.NamedAnalyzer; import org.opensearch.index.fielddata.IndexFieldData; import org.opensearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.index.similarity.SimilarityProvider; import org.opensearch.search.aggregations.support.CoreValuesSourceType; import org.opensearch.search.lookup.SearchLookup; @@ -289,12 +290,12 @@ public final class KeywordFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { if (format != null) { throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); } - return new SourceValueFetcher(name(), mapperService, nullValue) { + return new SourceValueFetcher(name(), context, nullValue) { @Override protected String parseSourceValue(Object value) { String keywordValue = value.toString(); diff --git a/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java b/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java index 426e74551d3..1bf8686f07c 100644 --- a/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java +++ b/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java @@ -126,7 +126,7 @@ public abstract class MappedFieldType { * for metadata fields, field types should not throw {@link UnsupportedOperationException} since this * could cause a search retrieving multiple fields (like "fields": ["*"]) to fail. */ - public abstract ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, @Nullable String format); + public abstract ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, @Nullable String format); /** Returns the name of this type, as would be specified in mapping properties */ public abstract String typeName(); diff --git a/server/src/main/java/org/opensearch/index/mapper/NumberFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/NumberFieldMapper.java index 5ea56412399..368f4ae4ade 100644 --- a/server/src/main/java/org/opensearch/index/mapper/NumberFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/NumberFieldMapper.java @@ -1062,12 +1062,12 @@ public class NumberFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { if (format != null) { throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); } - return new SourceValueFetcher(name(), mapperService, nullValue) { + return new SourceValueFetcher(name(), context, nullValue) { @Override protected Object parseSourceValue(Object value) { if (value.equals("")) { diff --git a/server/src/main/java/org/opensearch/index/mapper/RangeFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/RangeFieldMapper.java index 40a9fb89550..2464f482e33 100644 --- a/server/src/main/java/org/opensearch/index/mapper/RangeFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/RangeFieldMapper.java @@ -263,13 +263,13 @@ public class RangeFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { DateFormatter defaultFormatter = dateTimeFormatter(); DateFormatter formatter = format != null ? DateFormatter.forPattern(format).withLocale(defaultFormatter.locale()) : defaultFormatter; - return new SourceValueFetcher(name(), mapperService) { + return new SourceValueFetcher(name(), context) { @Override @SuppressWarnings("unchecked") diff --git a/server/src/main/java/org/opensearch/index/mapper/RoutingFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/RoutingFieldMapper.java index c31f02f1917..e487673b5c9 100644 --- a/server/src/main/java/org/opensearch/index/mapper/RoutingFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/RoutingFieldMapper.java @@ -36,6 +36,7 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.opensearch.common.lucene.Lucene; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.lookup.SearchLookup; import java.util.Collections; @@ -105,7 +106,7 @@ public class RoutingFieldMapper extends MetadataFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup lookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup lookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } } diff --git a/server/src/main/java/org/opensearch/index/mapper/SeqNoFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/SeqNoFieldMapper.java index 9c3f56cfc2b..490e1a31ae7 100644 --- a/server/src/main/java/org/opensearch/index/mapper/SeqNoFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/SeqNoFieldMapper.java @@ -138,7 +138,7 @@ public class SeqNoFieldMapper extends MetadataFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup lookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup lookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } diff --git a/server/src/main/java/org/opensearch/index/mapper/SourceFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/SourceFieldMapper.java index 54888bf2dfd..8791c23e490 100644 --- a/server/src/main/java/org/opensearch/index/mapper/SourceFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/SourceFieldMapper.java @@ -135,7 +135,7 @@ public class SourceFieldMapper extends MetadataFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup lookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup lookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } diff --git a/server/src/main/java/org/opensearch/index/mapper/SourceValueFetcher.java b/server/src/main/java/org/opensearch/index/mapper/SourceValueFetcher.java index edcd48a5e12..9467f22afa8 100644 --- a/server/src/main/java/org/opensearch/index/mapper/SourceValueFetcher.java +++ b/server/src/main/java/org/opensearch/index/mapper/SourceValueFetcher.java @@ -33,6 +33,7 @@ package org.opensearch.index.mapper; import org.opensearch.common.Nullable; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.lookup.SourceLookup; import java.util.ArrayDeque; @@ -52,17 +53,17 @@ public abstract class SourceValueFetcher implements ValueFetcher { private final Set sourcePaths; private final @Nullable Object nullValue; - public SourceValueFetcher(String fieldName, MapperService mapperService) { - this(fieldName, mapperService, null); + public SourceValueFetcher(String fieldName, QueryShardContext context) { + this(fieldName, context, null); } /** * @param fieldName The name of the field. - * @param mapperService A mapper service. + * @param context A query shard context. * @param nullValue A optional substitute value if the _source value is 'null'. */ - public SourceValueFetcher(String fieldName, MapperService mapperService, Object nullValue) { - this.sourcePaths = mapperService.sourcePath(fieldName); + public SourceValueFetcher(String fieldName, QueryShardContext context, Object nullValue) { + this.sourcePaths = context.sourcePath(fieldName); this.nullValue = nullValue; } @@ -104,11 +105,11 @@ public abstract class SourceValueFetcher implements ValueFetcher { /** * Creates a {@link SourceValueFetcher} that passes through source values unmodified. */ - public static SourceValueFetcher identity(String fieldName, MapperService mapperService, String format) { + public static SourceValueFetcher identity(String fieldName, QueryShardContext context, String format) { if (format != null) { throw new IllegalArgumentException("Field [" + fieldName + "] doesn't support formats."); } - return new SourceValueFetcher(fieldName, mapperService) { + return new SourceValueFetcher(fieldName, context) { @Override protected Object parseSourceValue(Object value) { return value; @@ -119,11 +120,11 @@ public abstract class SourceValueFetcher implements ValueFetcher { /** * Creates a {@link SourceValueFetcher} that converts source values to strings. */ - public static SourceValueFetcher toString(String fieldName, MapperService mapperService, String format) { + public static SourceValueFetcher toString(String fieldName, QueryShardContext context, String format) { if (format != null) { throw new IllegalArgumentException("Field [" + fieldName + "] doesn't support formats."); } - return new SourceValueFetcher(fieldName, mapperService) { + return new SourceValueFetcher(fieldName, context) { @Override protected Object parseSourceValue(Object value) { return value.toString(); diff --git a/server/src/main/java/org/opensearch/index/mapper/TextFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/TextFieldMapper.java index 7de2ec16eb6..064ef178bc9 100644 --- a/server/src/main/java/org/opensearch/index/mapper/TextFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/TextFieldMapper.java @@ -516,10 +516,10 @@ public class TextFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { // Because this internal field is modelled as a multi-field, SourceValueFetcher will look up its // parent field in _source. So we don't need to use the parent field name here. - return SourceValueFetcher.toString(name(), mapperService, format); + return SourceValueFetcher.toString(name(), context, format); } @Override @@ -546,10 +546,10 @@ public class TextFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { // Because this internal field is modelled as a multi-field, SourceValueFetcher will look up its // parent field in _source. So we don't need to use the parent field name here. - return SourceValueFetcher.toString(name(), mapperService, format); + return SourceValueFetcher.toString(name(), context, format); } void setAnalyzer(NamedAnalyzer delegate) { @@ -752,8 +752,8 @@ public class TextFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - return SourceValueFetcher.toString(name(), mapperService, format); + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { + return SourceValueFetcher.toString(name(), context, format); } @Override diff --git a/server/src/main/java/org/opensearch/index/mapper/TypeFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/TypeFieldMapper.java index eb84c472ab7..ce7bdd3682d 100644 --- a/server/src/main/java/org/opensearch/index/mapper/TypeFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/TypeFieldMapper.java @@ -106,7 +106,7 @@ public class TypeFieldMapper extends MetadataFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup lookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup lookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } diff --git a/server/src/main/java/org/opensearch/index/mapper/VersionFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/VersionFieldMapper.java index 966abadbbcb..e601894105b 100644 --- a/server/src/main/java/org/opensearch/index/mapper/VersionFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/VersionFieldMapper.java @@ -69,7 +69,7 @@ public class VersionFieldMapper extends MetadataFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup lookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup lookup, String format) { throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); } } diff --git a/server/src/main/java/org/opensearch/index/query/QueryShardContext.java b/server/src/main/java/org/opensearch/index/query/QueryShardContext.java index 47bade8b6aa..ab0ae487a39 100644 --- a/server/src/main/java/org/opensearch/index/query/QueryShardContext.java +++ b/server/src/main/java/org/opensearch/index/query/QueryShardContext.java @@ -295,6 +295,19 @@ public class QueryShardContext extends QueryRewriteContext { return mapperService.simpleMatchToFullName(pattern); } + /** + * Returns the {@link MappedFieldType} for the provided field name. + * If the field is not mapped, the behaviour depends on the index.query.parse.allow_unmapped_fields setting, which defaults to true. + * In case unmapped fields are allowed, null is returned when the field is not mapped. + * In case unmapped fields are not allowed, either an exception is thrown or the field is automatically mapped as a text field. + * @throws QueryShardException if unmapped fields are not allowed and automatically mapping unmapped fields as text is disabled. + * @see QueryShardContext#setAllowUnmappedFields(boolean) + * @see QueryShardContext#setMapUnmappedFieldAsString(boolean) + */ + public MappedFieldType getFieldType(String name) { + return failIfFieldMappingNotFound(name, mapperService.fieldType(name)); + } + public MappedFieldType fieldMapper(String name) { return failIfFieldMappingNotFound(name, mapperService.fieldType(name)); } @@ -303,6 +316,14 @@ public class QueryShardContext extends QueryRewriteContext { return mapperService.getObjectMapper(name); } + public boolean isMetadataField(String field) { + return mapperService.isMetadataField(field); + } + + public Set sourcePath(String fullName) { + return mapperService.sourcePath(fullName); + } + /** * Returns s {@link DocumentMapper} instance for the given type. * Delegates to {@link MapperService#documentMapper(String)} diff --git a/server/src/main/java/org/opensearch/search/fetch/FetchContext.java b/server/src/main/java/org/opensearch/search/fetch/FetchContext.java index 24bb8f67157..d26dbac33af 100644 --- a/server/src/main/java/org/opensearch/search/fetch/FetchContext.java +++ b/server/src/main/java/org/opensearch/search/fetch/FetchContext.java @@ -36,6 +36,7 @@ import org.apache.lucene.search.Query; import org.opensearch.index.IndexSettings; import org.opensearch.index.mapper.MapperService; import org.opensearch.index.query.ParsedQuery; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.SearchExtBuilder; import org.opensearch.search.fetch.subphase.FetchDocValuesContext; import org.opensearch.search.fetch.subphase.FetchFieldsContext; @@ -220,6 +221,10 @@ public class FetchContext { return searchContext.getSearchExt(name); } + public QueryShardContext getQueryShardContext() { + return searchContext.getQueryShardContext(); + } + /** * For a hit document that's being processed, return the source lookup representing the * root document. This method is used to pass down the root source when processing this diff --git a/server/src/main/java/org/opensearch/search/fetch/subphase/FetchFieldsPhase.java b/server/src/main/java/org/opensearch/search/fetch/subphase/FetchFieldsPhase.java index 0f7af12d908..353ec3f8080 100644 --- a/server/src/main/java/org/opensearch/search/fetch/subphase/FetchFieldsPhase.java +++ b/server/src/main/java/org/opensearch/search/fetch/subphase/FetchFieldsPhase.java @@ -35,7 +35,6 @@ package org.opensearch.search.fetch.subphase; import org.apache.lucene.index.LeafReaderContext; import org.opensearch.common.document.DocumentField; import org.opensearch.index.mapper.IgnoredFieldMapper; -import org.opensearch.index.mapper.MapperService; import org.opensearch.search.SearchHit; import org.opensearch.search.fetch.FetchContext; import org.opensearch.search.fetch.FetchSubPhase; @@ -61,7 +60,6 @@ public final class FetchFieldsPhase implements FetchSubPhase { return null; } - MapperService mapperService = fetchContext.mapperService(); SearchLookup searchLookup = fetchContext.searchLookup(); if (fetchContext.mapperService().documentMapper().sourceMapper().enabled() == false) { throw new IllegalArgumentException( @@ -72,7 +70,7 @@ public final class FetchFieldsPhase implements FetchSubPhase { ); } - FieldFetcher fieldFetcher = FieldFetcher.create(mapperService, searchLookup, fetchFieldsContext.fields()); + FieldFetcher fieldFetcher = FieldFetcher.create(fetchContext.getQueryShardContext(), searchLookup, fetchFieldsContext.fields()); return new FetchSubPhaseProcessor() { @Override public void setNextReader(LeafReaderContext readerContext) { diff --git a/server/src/main/java/org/opensearch/search/fetch/subphase/FieldFetcher.java b/server/src/main/java/org/opensearch/search/fetch/subphase/FieldFetcher.java index 718c7971caf..6790a1a79d6 100644 --- a/server/src/main/java/org/opensearch/search/fetch/subphase/FieldFetcher.java +++ b/server/src/main/java/org/opensearch/search/fetch/subphase/FieldFetcher.java @@ -35,8 +35,8 @@ package org.opensearch.search.fetch.subphase; import org.apache.lucene.index.LeafReaderContext; import org.opensearch.common.document.DocumentField; import org.opensearch.index.mapper.MappedFieldType; -import org.opensearch.index.mapper.MapperService; import org.opensearch.index.mapper.ValueFetcher; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.lookup.SearchLookup; import org.opensearch.search.lookup.SourceLookup; @@ -53,7 +53,7 @@ import java.util.Set; * Then given a specific document, it can retrieve the corresponding fields from the document's source. */ public class FieldFetcher { - public static FieldFetcher create(MapperService mapperService, SearchLookup searchLookup, Collection fieldAndFormats) { + public static FieldFetcher create(QueryShardContext context, SearchLookup searchLookup, Collection fieldAndFormats) { List fieldContexts = new ArrayList<>(); @@ -61,13 +61,13 @@ public class FieldFetcher { String fieldPattern = fieldAndFormat.field; String format = fieldAndFormat.format; - Collection concreteFields = mapperService.simpleMatchToFullName(fieldPattern); + Collection concreteFields = context.simpleMatchToIndexNames(fieldPattern); for (String field : concreteFields) { - MappedFieldType ft = mapperService.fieldType(field); - if (ft == null || mapperService.isMetadataField(field)) { + MappedFieldType ft = context.getFieldType(field); + if (ft == null || context.isMetadataField(field)) { continue; } - ValueFetcher valueFetcher = ft.valueFetcher(mapperService, searchLookup, format); + ValueFetcher valueFetcher = ft.valueFetcher(context, searchLookup, format); fieldContexts.add(new FieldContext(field, valueFetcher)); } } diff --git a/server/src/main/java/org/opensearch/search/fetch/subphase/highlight/HighlightUtils.java b/server/src/main/java/org/opensearch/search/fetch/subphase/highlight/HighlightUtils.java index 080a869bb48..3265a339595 100644 --- a/server/src/main/java/org/opensearch/search/fetch/subphase/highlight/HighlightUtils.java +++ b/server/src/main/java/org/opensearch/search/fetch/subphase/highlight/HighlightUtils.java @@ -36,8 +36,9 @@ import org.apache.lucene.search.highlight.Encoder; import org.apache.lucene.search.highlight.SimpleHTMLEncoder; import org.opensearch.index.fieldvisitor.CustomFieldsVisitor; import org.opensearch.index.mapper.MappedFieldType; +import org.opensearch.index.mapper.ValueFetcher; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.fetch.FetchSubPhase; -import org.opensearch.search.lookup.SourceLookup; import java.io.IOException; import java.util.Collections; @@ -58,24 +59,20 @@ public final class HighlightUtils { /** * Load field values for highlighting. */ - public static List loadFieldValues(MappedFieldType fieldType, FetchSubPhase.HitContext hitContext, boolean forceSource) - throws IOException { - // percolator needs to always load from source, thus it sets the global force source to true - List textsToHighlight; + public static List loadFieldValues( + MappedFieldType fieldType, + QueryShardContext context, + FetchSubPhase.HitContext hitContext, + boolean forceSource + ) throws IOException { if (forceSource == false && fieldType.isStored()) { CustomFieldsVisitor fieldVisitor = new CustomFieldsVisitor(singleton(fieldType.name()), false); hitContext.reader().document(hitContext.docId(), fieldVisitor); - textsToHighlight = fieldVisitor.fields().get(fieldType.name()); - if (textsToHighlight == null) { - // Can happen if the document doesn't have the field to highlight - textsToHighlight = Collections.emptyList(); - } - } else { - SourceLookup sourceLookup = hitContext.sourceLookup(); - textsToHighlight = sourceLookup.extractRawValues(fieldType.name()); + List textsToHighlight = fieldVisitor.fields().get(fieldType.name()); + return textsToHighlight != null ? textsToHighlight : Collections.emptyList(); } - assert textsToHighlight != null; - return textsToHighlight; + ValueFetcher fetcher = fieldType.valueFetcher(context, null, null); + return fetcher.fetchValues(hitContext.sourceLookup()); } public static class Encoders { diff --git a/server/src/main/java/org/opensearch/search/fetch/subphase/highlight/PlainHighlighter.java b/server/src/main/java/org/opensearch/search/fetch/subphase/highlight/PlainHighlighter.java index 94ca1745bf2..8171e02eca4 100644 --- a/server/src/main/java/org/opensearch/search/fetch/subphase/highlight/PlainHighlighter.java +++ b/server/src/main/java/org/opensearch/search/fetch/subphase/highlight/PlainHighlighter.java @@ -49,7 +49,6 @@ import org.apache.lucene.util.BytesRefHash; import org.apache.lucene.util.CollectionUtil; import org.opensearch.common.text.Text; import org.opensearch.index.IndexSettings; -import org.opensearch.index.mapper.KeywordFieldMapper; import org.opensearch.index.mapper.MappedFieldType; import org.opensearch.search.fetch.FetchContext; import org.opensearch.search.fetch.FetchSubPhase; @@ -118,25 +117,13 @@ public class PlainHighlighter implements Highlighter { ArrayList fragsList = new ArrayList<>(); List textsToHighlight; Analyzer analyzer = context.mapperService().documentMapper(hitContext.hit().getType()).mappers().indexAnalyzer(); - Integer keywordIgnoreAbove = null; - if (fieldType instanceof KeywordFieldMapper.KeywordFieldType) { - KeywordFieldMapper mapper = (KeywordFieldMapper) context.mapperService() - .documentMapper() - .mappers() - .getMapper(fieldContext.fieldName); - keywordIgnoreAbove = mapper.ignoreAbove(); - } - ; final int maxAnalyzedOffset = context.getIndexSettings().getHighlightMaxAnalyzedOffset(); - textsToHighlight = HighlightUtils.loadFieldValues(fieldType, hitContext, fieldContext.forceSource); + textsToHighlight = HighlightUtils.loadFieldValues(fieldType, context.getQueryShardContext(), hitContext, fieldContext.forceSource); for (Object textToHighlight : textsToHighlight) { String text = convertFieldValue(fieldType, textToHighlight); int textLength = text.length(); - if (keywordIgnoreAbove != null && textLength > keywordIgnoreAbove) { - continue; // skip highlighting keyword terms that were ignored during indexing - } if (textLength > maxAnalyzedOffset) { throw new IllegalArgumentException( "The length of [" diff --git a/server/src/main/java/org/opensearch/search/fetch/subphase/highlight/UnifiedHighlighter.java b/server/src/main/java/org/opensearch/search/fetch/subphase/highlight/UnifiedHighlighter.java index 9fe500868cd..8f0c434674f 100644 --- a/server/src/main/java/org/opensearch/search/fetch/subphase/highlight/UnifiedHighlighter.java +++ b/server/src/main/java/org/opensearch/search/fetch/subphase/highlight/UnifiedHighlighter.java @@ -51,6 +51,7 @@ import org.opensearch.index.mapper.IdFieldMapper; import org.opensearch.index.mapper.KeywordFieldMapper; import org.opensearch.index.mapper.MappedFieldType; import org.opensearch.index.mapper.TextSearchInfo; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.fetch.FetchSubPhase; import org.opensearch.search.fetch.FetchSubPhase.HitContext; @@ -88,7 +89,13 @@ public class UnifiedHighlighter implements Highlighter { FetchSubPhase.HitContext hitContext = fieldContext.hitContext; CheckedSupplier loadFieldValues = () -> { - List fieldValues = loadFieldValues(highlighter, fieldType, field, hitContext, fieldContext.forceSource); + List fieldValues = loadFieldValues( + highlighter, + fieldContext.context.getQueryShardContext(), + fieldType, + hitContext, + fieldContext.forceSource + ); if (fieldValues.size() == 0) { return null; } @@ -186,12 +193,12 @@ public class UnifiedHighlighter implements Highlighter { protected List loadFieldValues( CustomUnifiedHighlighter highlighter, + QueryShardContext context, MappedFieldType fieldType, - SearchHighlightContext.Field field, FetchSubPhase.HitContext hitContext, boolean forceSource ) throws IOException { - List fieldValues = HighlightUtils.loadFieldValues(fieldType, hitContext, forceSource); + List fieldValues = HighlightUtils.loadFieldValues(fieldType, context, hitContext, forceSource); fieldValues = fieldValues.stream().map((s) -> convertFieldValue(fieldType, s)).collect(Collectors.toList()); return fieldValues; } diff --git a/server/src/test/java/org/opensearch/index/fielddata/AbstractStringFieldDataTestCase.java b/server/src/test/java/org/opensearch/index/fielddata/AbstractStringFieldDataTestCase.java index 5fb55366556..33813580468 100644 --- a/server/src/test/java/org/opensearch/index/fielddata/AbstractStringFieldDataTestCase.java +++ b/server/src/test/java/org/opensearch/index/fielddata/AbstractStringFieldDataTestCase.java @@ -225,7 +225,7 @@ public abstract class AbstractStringFieldDataTestCase extends AbstractFieldDataI writer.addDocument(d); d = new Document(); - d.add(new StringField("_id", "6", Field.Store.NO)); + addField(d, "_id", "6"); writer.addDocument(d); d = new Document(); diff --git a/server/src/test/java/org/opensearch/index/mapper/DocumentFieldMapperTests.java b/server/src/test/java/org/opensearch/index/mapper/DocumentFieldMapperTests.java index c088fb4b081..3d2b8c4457c 100644 --- a/server/src/test/java/org/opensearch/index/mapper/DocumentFieldMapperTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/DocumentFieldMapperTests.java @@ -39,6 +39,7 @@ import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.apache.lucene.util.LuceneTestCase; import org.opensearch.index.analysis.AnalyzerScope; import org.opensearch.index.analysis.NamedAnalyzer; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.lookup.SearchLookup; import java.io.IOException; @@ -84,7 +85,7 @@ public class DocumentFieldMapperTests extends LuceneTestCase { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { throw new UnsupportedOperationException(); } diff --git a/server/src/test/java/org/opensearch/index/mapper/ExternalMapper.java b/server/src/test/java/org/opensearch/index/mapper/ExternalMapper.java index 9a4af308142..49b38f828f8 100644 --- a/server/src/test/java/org/opensearch/index/mapper/ExternalMapper.java +++ b/server/src/test/java/org/opensearch/index/mapper/ExternalMapper.java @@ -40,6 +40,7 @@ import org.opensearch.geometry.Point; import org.opensearch.index.analysis.AnalyzerScope; import org.opensearch.index.analysis.IndexAnalyzers; import org.opensearch.index.analysis.NamedAnalyzer; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.lookup.SearchLookup; import java.io.IOException; @@ -137,8 +138,8 @@ public class ExternalMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - return SourceValueFetcher.identity(name(), mapperService, format); + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { + return SourceValueFetcher.identity(name(), context, format); } } diff --git a/server/src/test/java/org/opensearch/index/mapper/FakeStringFieldMapper.java b/server/src/test/java/org/opensearch/index/mapper/FakeStringFieldMapper.java index b0f17f41c4c..9da53b36e11 100644 --- a/server/src/test/java/org/opensearch/index/mapper/FakeStringFieldMapper.java +++ b/server/src/test/java/org/opensearch/index/mapper/FakeStringFieldMapper.java @@ -36,6 +36,7 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.opensearch.common.lucene.Lucene; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.lookup.SearchLookup; import java.io.IOException; @@ -95,8 +96,8 @@ public class FakeStringFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - return SourceValueFetcher.toString(name(), mapperService, format); + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { + return SourceValueFetcher.toString(name(), context, format); } } diff --git a/server/src/test/java/org/opensearch/search/collapse/CollapseBuilderTests.java b/server/src/test/java/org/opensearch/search/collapse/CollapseBuilderTests.java index 8913854df7e..8a05f4041d0 100644 --- a/server/src/test/java/org/opensearch/search/collapse/CollapseBuilderTests.java +++ b/server/src/test/java/org/opensearch/search/collapse/CollapseBuilderTests.java @@ -45,7 +45,6 @@ import org.opensearch.common.xcontent.NamedXContentRegistry; import org.opensearch.common.xcontent.XContentParser; import org.opensearch.index.mapper.KeywordFieldMapper; import org.opensearch.index.mapper.MappedFieldType; -import org.opensearch.index.mapper.MapperService; import org.opensearch.index.mapper.NumberFieldMapper; import org.opensearch.index.mapper.TextSearchInfo; import org.opensearch.index.mapper.ValueFetcher; @@ -239,7 +238,7 @@ public class CollapseBuilderTests extends AbstractSerializingTestCase fetchFields(MapperService mapperService, XContentBuilder source, String fieldPattern) + private static Map fetchFields(MapperService mapperService, XContentBuilder source, String fieldPattern) throws IOException { List fields = org.opensearch.common.collect.List.of(new FieldAndFormat(fieldPattern, null)); return fetchFields(mapperService, source, fields); } - private Map fetchFields(MapperService mapperService, XContentBuilder source, List fields) + private static Map fetchFields(MapperService mapperService, XContentBuilder source, List fields) throws IOException { SourceLookup sourceLookup = new SourceLookup(); sourceLookup.setSource(BytesReference.bytes(source)); - FieldFetcher fieldFetcher = FieldFetcher.create(mapperService, null, fields); + FieldFetcher fieldFetcher = FieldFetcher.create(createQueryShardContext(mapperService), null, fields); return fieldFetcher.fetch(sourceLookup, org.opensearch.common.collect.Set.of()); } @@ -480,4 +484,34 @@ public class FieldFetcherTests extends OpenSearchSingleNodeTestCase { IndexService indexService = createIndex("index", Settings.EMPTY, MapperService.SINGLE_MAPPING_NAME, mapping); return indexService.mapperService(); } + + private static QueryShardContext createQueryShardContext(MapperService mapperService) { + Settings settings = Settings.builder() + .put("index.version.created", Version.CURRENT) + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put(IndexMetadata.SETTING_INDEX_UUID, "uuid") + .build(); + IndexMetadata indexMetadata = new IndexMetadata.Builder("index").settings(settings).build(); + IndexSettings indexSettings = new IndexSettings(indexMetadata, settings); + return new QueryShardContext( + 0, + indexSettings, + null, + null, + null, + mapperService, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null + ); + } } diff --git a/server/src/test/java/org/opensearch/search/slice/SliceBuilderTests.java b/server/src/test/java/org/opensearch/search/slice/SliceBuilderTests.java index 897dfe54cd1..6deae9d8dae 100644 --- a/server/src/test/java/org/opensearch/search/slice/SliceBuilderTests.java +++ b/server/src/test/java/org/opensearch/search/slice/SliceBuilderTests.java @@ -64,7 +64,6 @@ import org.opensearch.common.xcontent.XContentType; import org.opensearch.index.IndexSettings; import org.opensearch.index.fielddata.IndexNumericFieldData; import org.opensearch.index.mapper.MappedFieldType; -import org.opensearch.index.mapper.MapperService; import org.opensearch.index.mapper.TextSearchInfo; import org.opensearch.index.mapper.ValueFetcher; import org.opensearch.index.query.QueryShardContext; @@ -163,7 +162,7 @@ public class SliceBuilderTests extends OpenSearchTestCase { ) { @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { throw new UnsupportedOperationException(); } diff --git a/test/framework/src/main/java/org/opensearch/index/mapper/FieldTypeTestCase.java b/test/framework/src/main/java/org/opensearch/index/mapper/FieldTypeTestCase.java index ebc17df9fa5..571f1b21dd7 100644 --- a/test/framework/src/main/java/org/opensearch/index/mapper/FieldTypeTestCase.java +++ b/test/framework/src/main/java/org/opensearch/index/mapper/FieldTypeTestCase.java @@ -64,10 +64,10 @@ public abstract class FieldTypeTestCase extends OpenSearchTestCase { public static List fetchSourceValue(MappedFieldType fieldType, Object sourceValue, String format) throws IOException { String field = fieldType.name(); - MapperService mapperService = mock(MapperService.class); - when(mapperService.sourcePath(field)).thenReturn(org.opensearch.common.collect.Set.of(field)); + QueryShardContext context = mock(QueryShardContext.class); + when(context.sourcePath(field)).thenReturn(org.opensearch.common.collect.Set.of(field)); - ValueFetcher fetcher = fieldType.valueFetcher(mapperService, null, format); + ValueFetcher fetcher = fieldType.valueFetcher(context, null, format); SourceLookup lookup = new SourceLookup(); lookup.setSource(Collections.singletonMap(field, sourceValue)); return fetcher.fetchValues(lookup); diff --git a/test/framework/src/main/java/org/opensearch/index/mapper/MockFieldMapper.java b/test/framework/src/main/java/org/opensearch/index/mapper/MockFieldMapper.java index 071329616fa..6e504e0f8d2 100644 --- a/test/framework/src/main/java/org/opensearch/index/mapper/MockFieldMapper.java +++ b/test/framework/src/main/java/org/opensearch/index/mapper/MockFieldMapper.java @@ -35,6 +35,7 @@ package org.opensearch.index.mapper; import org.opensearch.Version; import org.opensearch.cluster.metadata.IndexMetadata; import org.opensearch.common.settings.Settings; +import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.lookup.SearchLookup; import java.util.Collections; @@ -77,7 +78,7 @@ public class MockFieldMapper extends ParametrizedFieldMapper { } @Override - public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { + public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup searchLookup, String format) { throw new UnsupportedOperationException(); } }