Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Removed

### Fixed
- With creation of FilterFieldType, we need unwrap all the MappedFieldType before using the instanceof check. ([#17951](https://github.com/opensearch-project/OpenSearch/pull/17951))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,14 +496,14 @@ private static DoubleValuesSource getDocValueSource(String variable, SearchLooku

IndexFieldData<?> fieldData = lookup.doc().getForField(fieldType);
final DoubleValuesSource valueSource;
if (fieldType instanceof GeoPointFieldType) {
if (fieldType.unwrap() instanceof GeoPointFieldType) {
// geo
if (methodname == null) {
valueSource = GeoField.getVariable(fieldData, fieldname, variablename);
} else {
valueSource = GeoField.getMethod(fieldData, fieldname, methodname);
}
} else if (fieldType instanceof DateFieldMapper.DateFieldType) {
} else if (fieldType.unwrap() instanceof DateFieldMapper.DateFieldType) {
if (dateAccessor) {
// date object
if (methodname == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,19 +404,19 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
protected Query doToQuery(QueryShardContext context) throws IOException {
final MappedFieldType ft = context.fieldMapper(field);

if (ft instanceof RankFeatureFieldType) {
final RankFeatureFieldType fft = (RankFeatureFieldType) ft;
return scoreFunction.toQuery(RankFeatureMetaFieldMapper.NAME, field, fft.positiveScoreImpact());
} else if (ft == null) {
if (ft == null) {
final int lastDotIndex = field.lastIndexOf('.');
if (lastDotIndex != -1) {
final String parentField = field.substring(0, lastDotIndex);
final MappedFieldType parentFt = context.fieldMapper(parentField);
if (parentFt instanceof RankFeaturesFieldType) {
if (parentFt != null && parentFt.unwrap() instanceof RankFeaturesFieldType) {
return scoreFunction.toQuery(parentField, field.substring(lastDotIndex + 1), true);
}
}
return new MatchNoDocsQuery(); // unmapped field
} else if (ft.unwrap() instanceof RankFeatureFieldType) {
final RankFeatureFieldType fft = (RankFeatureFieldType) ft;
return scoreFunction.toQuery(RankFeatureMetaFieldMapper.NAME, field, fft.positiveScoreImpact());
} else {
throw new IllegalArgumentException(
"[rank_feature] query only works on [rank_feature] fields and "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
throw new QueryShardException(context, "field [" + field + "] does not exist");
}

if (!(fieldType instanceof PercolatorFieldMapper.PercolatorFieldType)) {
if (!(fieldType.unwrap() instanceof PercolatorFieldMapper.PercolatorFieldType)) {
throw new QueryShardException(
context,
"expected field [" + field + "] to be of type [percolator], but is of type [" + fieldType.typeName() + "]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ public void testValidCompositeIndex() {
Set<CompositeMappedFieldType> fts = indexService.mapperService().getCompositeFieldTypes();

for (CompositeMappedFieldType ft : fts) {
assertTrue(ft instanceof StarTreeMapper.StarTreeFieldType);
assertFalse(ft == null);
assertTrue(ft.unwrap() instanceof StarTreeMapper.StarTreeFieldType);
StarTreeMapper.StarTreeFieldType starTreeFieldType = (StarTreeMapper.StarTreeFieldType) ft;
assertEquals("timestamp", starTreeFieldType.getDimensions().get(0).getField());
assertTrue(starTreeFieldType.getDimensions().get(0) instanceof DateDimension);
Expand Down Expand Up @@ -643,7 +644,8 @@ public void testValidCompositeIndexWithDates() {
Set<CompositeMappedFieldType> fts = indexService.mapperService().getCompositeFieldTypes();

for (CompositeMappedFieldType ft : fts) {
assertTrue(ft instanceof StarTreeMapper.StarTreeFieldType);
assertFalse(ft == null);
assertTrue(ft.unwrap() instanceof StarTreeMapper.StarTreeFieldType);
StarTreeMapper.StarTreeFieldType starTreeFieldType = (StarTreeMapper.StarTreeFieldType) ft;
assertEquals("timestamp", starTreeFieldType.getDimensions().get(0).getField());
assertTrue(starTreeFieldType.getDimensions().get(0) instanceof DateDimension);
Expand Down Expand Up @@ -681,7 +683,8 @@ public void testValidCompositeIndexWithNestedFields() {
Set<CompositeMappedFieldType> fts = indexService.mapperService().getCompositeFieldTypes();

for (CompositeMappedFieldType ft : fts) {
assertTrue(ft instanceof StarTreeMapper.StarTreeFieldType);
assertFalse(ft == null);
assertTrue(ft.unwrap() instanceof StarTreeMapper.StarTreeFieldType);
StarTreeMapper.StarTreeFieldType starTreeFieldType = (StarTreeMapper.StarTreeFieldType) ft;
assertEquals("timestamp", starTreeFieldType.getDimensions().get(0).getField());
assertTrue(starTreeFieldType.getDimensions().get(0) instanceof DateDimension);
Expand Down Expand Up @@ -721,7 +724,8 @@ public void testValidCompositeIndexWithDuplicateDates() {
Set<CompositeMappedFieldType> fts = indexService.mapperService().getCompositeFieldTypes();

for (CompositeMappedFieldType ft : fts) {
assertTrue(ft instanceof StarTreeMapper.StarTreeFieldType);
assertFalse(ft == null);
assertTrue(ft.unwrap() instanceof StarTreeMapper.StarTreeFieldType);
StarTreeMapper.StarTreeFieldType starTreeFieldType = (StarTreeMapper.StarTreeFieldType) ft;
assertEquals("timestamp", starTreeFieldType.getDimensions().get(0).getField());
assertTrue(starTreeFieldType.getDimensions().get(0) instanceof DateDimension);
Expand Down Expand Up @@ -1053,7 +1057,8 @@ public void testUpdateIndexWhenMappingIsSame() {
Set<CompositeMappedFieldType> fts = indexService.mapperService().getCompositeFieldTypes();

for (CompositeMappedFieldType ft : fts) {
assertTrue(ft instanceof StarTreeMapper.StarTreeFieldType);
assertFalse(ft == null);
assertTrue(ft.unwrap() instanceof StarTreeMapper.StarTreeFieldType);
StarTreeMapper.StarTreeFieldType starTreeFieldType = (StarTreeMapper.StarTreeFieldType) ft;
assertEquals("timestamp", starTreeFieldType.getDimensions().get(0).getField());
assertTrue(starTreeFieldType.getDimensions().get(0) instanceof DateDimension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private static Analyzer getAnalyzer(AnalyzeAction.Request request, AnalysisRegis
}
MappedFieldType fieldType = indexService.mapperService().fieldType(request.field());
if (fieldType != null) {
if (fieldType instanceof StringFieldType) {
if (fieldType.unwrap() instanceof StringFieldType) {
return fieldType.indexAnalyzer();
} else {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public PostingsFormat getPostingsFormatForField(String field) {
final MappedFieldType fieldType = mapperService.fieldType(field);
if (fieldType == null) {
logger.warn("no index mapper found for field: [{}] returning default postings format", field);
} else if (fieldType instanceof CompletionFieldMapper.CompletionFieldType) {
} else if (fieldType.unwrap() instanceof CompletionFieldMapper.CompletionFieldType) {
return CompletionFieldMapper.CompletionFieldType.postingsFormat();
} else if (IdFieldMapper.NAME.equals(field) && mapperService.getIndexSettings().isEnableFuzzySetForDocId()) {
if (docIdPostingsFormat == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void validate(MapperService mapperService, CompositeIndexSettings
);
}
for (CompositeMappedFieldType compositeFieldType : compositeFieldTypes) {
if (!(compositeFieldType instanceof StarTreeMapper.StarTreeFieldType)) {
if (!(compositeFieldType != null && compositeFieldType.unwrap() instanceof StarTreeMapper.StarTreeFieldType)) {
continue;
}
if (!compositeIndexSettings.isStarTreeIndexCreationEnabled()) {
Expand Down Expand Up @@ -79,7 +79,7 @@ public static void validate(MapperService mapperService, CompositeIndexSettings
String.format(Locale.ROOT, "unknown metric field [%s] as part of star tree field", metric.getField())
);
}
if (ft.isAggregatable() == false && ft instanceof DocCountFieldMapper.DocCountFieldType == false) {
if (ft.isAggregatable() == false && ft.unwrap() instanceof DocCountFieldMapper.DocCountFieldType == false) {
throw new IllegalArgumentException(
String.format(
Locale.ROOT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class StarTreesBuilder implements Closeable {
public StarTreesBuilder(SegmentWriteState segmentWriteState, MapperService mapperService, AtomicInteger fieldNumberAcrossStarTrees) {
List<StarTreeField> starTreeFields = new ArrayList<>();
for (CompositeMappedFieldType compositeMappedFieldType : mapperService.getCompositeFieldTypes()) {
if (compositeMappedFieldType instanceof StarTreeMapper.StarTreeFieldType) {
if (compositeMappedFieldType != null && compositeMappedFieldType.unwrap() instanceof StarTreeMapper.StarTreeFieldType) {
StarTreeMapper.StarTreeFieldType starTreeFieldType = (StarTreeMapper.StarTreeFieldType) compositeMappedFieldType;
starTreeFields.add(
new StarTreeField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Set<String> resolvePattern(String pattern) {
Set<String> derivedFields = new HashSet<>();
if (queryShardContext != null && queryShardContext.getMapperService() != null) {
for (MappedFieldType fieldType : queryShardContext.getMapperService().fieldTypes()) {
if (fieldType instanceof DerivedFieldType && Regex.simpleMatch(pattern, fieldType.name())) {
if (fieldType != null && fieldType.unwrap() instanceof DerivedFieldType && Regex.simpleMatch(pattern, fieldType.name())) {
derivedFields.add(fieldType.name());
}
}
Expand Down Expand Up @@ -223,7 +223,7 @@ private DerivedFieldType getDerivedFieldType(DerivedField derivedField) {
private DerivedFieldType resolveUsingMappings(String name) {
if (queryShardContext != null && queryShardContext.getMapperService() != null) {
MappedFieldType mappedFieldType = queryShardContext.getMapperService().fieldType(name);
if (mappedFieldType instanceof DerivedFieldType) {
if (mappedFieldType != null && mappedFieldType.unwrap() instanceof DerivedFieldType) {
return (DerivedFieldType) mappedFieldType;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ TextFieldMapper.TextFieldType getPrefilterFieldType(QueryShardContext context) {
if (mappedFieldType == null) {
throw new MapperException("prefilter_field[" + derivedField.getPrefilterField() + "] is not defined in the index mappings");
}
if (!(mappedFieldType instanceof TextFieldMapper.TextFieldType)) {
if (!(mappedFieldType.unwrap() instanceof TextFieldMapper.TextFieldType)) {
throw new MapperException(
"prefilter_field["
+ derivedField.getPrefilterField()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ private Set<CompositeMappedFieldType> getCompositeFieldTypesFromMapper() {
return Collections.emptySet();
}
for (MappedFieldType type : this.mapper.fieldTypes()) {
if (type instanceof CompositeMappedFieldType) {
if (type != null && type.unwrap() instanceof CompositeMappedFieldType) {
compositeMappedFieldTypes.add((CompositeMappedFieldType) type);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ public String getNestedScope(String path) {
*/
public boolean containsTimeStampField() {
MappedFieldType timeSeriesFieldType = this.fieldTypeLookup.get(DataStream.TIMESERIES_FIELDNAME);
return timeSeriesFieldType != null && timeSeriesFieldType instanceof DateFieldMapper.DateFieldType; // has to be Date field type
return timeSeriesFieldType != null && timeSeriesFieldType.unwrap() instanceof DateFieldMapper.DateFieldType; // has to be Date field
// type
}

private static String parentObject(String field) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
throw new QueryShardException(context, "failed to find geo_point field [" + fieldName + "]");
}
}
if (!(fieldType instanceof GeoPointFieldType)) {
if (!(fieldType.unwrap() instanceof GeoPointFieldType)) {
throw new QueryShardException(context, "field [" + fieldName + "] is not a geo_point field");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
MappedFieldType fieldType = context.fieldMapper(this.fieldName);
if (fieldType == null) {
return new MatchNoneQueryBuilder();
} else if (fieldType instanceof ConstantFieldType) {
} else if (fieldType.unwrap() instanceof ConstantFieldType) {
// This logic is correct for all field types, but by only applying it to constant
// fields we also have the guarantee that it doesn't perform I/O, which is important
// since rewrites might happen on a network thread.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public void setMapUnmappedFieldAsString(boolean mapUnmappedFieldAsString) {

MappedFieldType failIfFieldMappingNotFound(String name, MappedFieldType fieldMapping) {
if (fieldMapping != null) {
if (fieldMapping instanceof DerivedFieldType) {
if (fieldMapping.unwrap() instanceof DerivedFieldType) {
// resolveDerivedFieldType() will give precedence to search time definitions over index mapping, thus
// calling it instead of directly returning. It also ensures the feature flags are honoured.
return resolveDerivedFieldType(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
MappedFieldType fieldType = context.fieldMapper(this.fieldName);
if (fieldType == null) {
return new MatchNoneQueryBuilder();
} else if (fieldType instanceof ConstantFieldType) {
} else if (fieldType.unwrap() instanceof ConstantFieldType) {
// This logic is correct for all field types, but by only applying it to constant
// fields we also have the guarantee that it doesn't perform I/O, which is important
// since rewrites might happen on a network thread.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
}
if (valueType == ValueType.BITMAP) {
if (values.size() == 1 && values.get(0) instanceof BytesArray) {
if (fieldType instanceof NumberFieldMapper.NumberFieldType) {
if (fieldType.unwrap() instanceof NumberFieldMapper.NumberFieldType) {
return ((NumberFieldMapper.NumberFieldType) fieldType).bitmapQuery((BytesArray) values.get(0));
}
}
Expand Down Expand Up @@ -622,7 +622,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) {
MappedFieldType fieldType = context.fieldMapper(this.fieldName);
if (fieldType == null) {
return new MatchNoneQueryBuilder();
} else if (fieldType instanceof ConstantFieldType) {
} else if (fieldType.unwrap() instanceof ConstantFieldType) {
// This logic is correct for all field types, but by only applying it to constant
// fields we also have the guarantee that it doesn't perform I/O, which is important
// since rewrites might happen on a network thread.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,21 @@
return getVectorQueryFromShape(shape, fieldName, relation, context);
}

private boolean isGeoPointField(MappedFieldType fieldType) {
if (fieldType == null) {
return false;

Check warning on line 82 in server/src/main/java/org/opensearch/index/query/VectorGeoPointShapeQueryProcessor.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/index/query/VectorGeoPointShapeQueryProcessor.java#L82

Added line #L82 was not covered by tests
}
MappedFieldType delegateFieldType = fieldType.unwrap();
if (delegateFieldType instanceof DerivedFieldType) {
MappedFieldType derivedFieldType = ((DerivedFieldType) delegateFieldType).getFieldMapper().fieldType();
delegateFieldType = derivedFieldType == null ? null : derivedFieldType.unwrap();
}
return delegateFieldType instanceof GeoPointFieldMapper.GeoPointFieldType;
}

private void validateIsGeoPointFieldType(String fieldName, QueryShardContext context) {
MappedFieldType fieldType = context.fieldMapper(fieldName);
if (fieldType instanceof GeoPointFieldMapper.GeoPointFieldType == false
&& !(fieldType instanceof DerivedFieldType
&& (((DerivedFieldType) fieldType).getFieldMapper().fieldType() instanceof GeoPointFieldMapper.GeoPointFieldType))) {
if (isGeoPointField(fieldType) == false) {
throw new QueryShardException(
context,
"Expected "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
MappedFieldType fieldType = context.fieldMapper(this.fieldName);
if (fieldType == null) {
return new MatchNoneQueryBuilder();
} else if (fieldType instanceof ConstantFieldType) {
} else if (fieldType.unwrap() instanceof ConstantFieldType) {
// This logic is correct for all field types, but by only applying it to constant
// fields we also have the guarantee that it doesn't perform I/O, which is important
// since rewrites might happen on a network thread.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,11 @@ private AbstractDistanceScoreFunction parseVariable(
// dates and time and geo need special handling
parser.nextToken();
// TODO these ain't gonna work with runtime fields
if (fieldType instanceof DateFieldMapper.DateFieldType) {
if (fieldType.unwrap() instanceof DateFieldMapper.DateFieldType) {
return parseDateVariable(parser, context, fieldType, mode);
} else if (fieldType instanceof GeoPointFieldType) {
} else if (fieldType.unwrap() instanceof GeoPointFieldType) {
return parseGeoVariable(parser, context, fieldType, mode);
} else if (fieldType instanceof NumberFieldMapper.NumberFieldType) {
} else if (fieldType.unwrap() instanceof NumberFieldMapper.NumberFieldType) {
return parseNumberVariable(parser, context, fieldType, mode);
} else {
throw new ParsingException(
Expand Down
Loading
Loading