Skip to content

Commit 52b8b05

Browse files
authored
Add Unwrapfunction to all the fieldType instaceof check (#17951)
Signed-off-by: Chloe Gao <chloewq@amazon.com>
1 parent a3dba00 commit 52b8b05

File tree

42 files changed

+93
-71
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+93
-71
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1919
### Removed
2020

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

2324
### Security
2425

modules/lang-expression/src/main/java/org/opensearch/script/expression/ExpressionScriptEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,14 +496,14 @@ private static DoubleValuesSource getDocValueSource(String variable, SearchLooku
496496

497497
IndexFieldData<?> fieldData = lookup.doc().getForField(fieldType);
498498
final DoubleValuesSource valueSource;
499-
if (fieldType instanceof GeoPointFieldType) {
499+
if (fieldType.unwrap() instanceof GeoPointFieldType) {
500500
// geo
501501
if (methodname == null) {
502502
valueSource = GeoField.getVariable(fieldData, fieldname, variablename);
503503
} else {
504504
valueSource = GeoField.getMethod(fieldData, fieldname, methodname);
505505
}
506-
} else if (fieldType instanceof DateFieldMapper.DateFieldType) {
506+
} else if (fieldType.unwrap() instanceof DateFieldMapper.DateFieldType) {
507507
if (dateAccessor) {
508508
// date object
509509
if (methodname == null) {

modules/mapper-extras/src/main/java/org/opensearch/index/query/RankFeatureQueryBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,19 +404,19 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
404404
protected Query doToQuery(QueryShardContext context) throws IOException {
405405
final MappedFieldType ft = context.fieldMapper(field);
406406

407-
if (ft instanceof RankFeatureFieldType) {
408-
final RankFeatureFieldType fft = (RankFeatureFieldType) ft;
409-
return scoreFunction.toQuery(RankFeatureMetaFieldMapper.NAME, field, fft.positiveScoreImpact());
410-
} else if (ft == null) {
407+
if (ft == null) {
411408
final int lastDotIndex = field.lastIndexOf('.');
412409
if (lastDotIndex != -1) {
413410
final String parentField = field.substring(0, lastDotIndex);
414411
final MappedFieldType parentFt = context.fieldMapper(parentField);
415-
if (parentFt instanceof RankFeaturesFieldType) {
412+
if (parentFt != null && parentFt.unwrap() instanceof RankFeaturesFieldType) {
416413
return scoreFunction.toQuery(parentField, field.substring(lastDotIndex + 1), true);
417414
}
418415
}
419416
return new MatchNoDocsQuery(); // unmapped field
417+
} else if (ft.unwrap() instanceof RankFeatureFieldType) {
418+
final RankFeatureFieldType fft = (RankFeatureFieldType) ft;
419+
return scoreFunction.toQuery(RankFeatureMetaFieldMapper.NAME, field, fft.positiveScoreImpact());
420420
} else {
421421
throw new IllegalArgumentException(
422422
"[rank_feature] query only works on [rank_feature] fields and "

modules/percolator/src/main/java/org/opensearch/percolator/PercolateQueryBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
508508
throw new QueryShardException(context, "field [" + field + "] does not exist");
509509
}
510510

511-
if (!(fieldType instanceof PercolatorFieldMapper.PercolatorFieldType)) {
511+
if (!(fieldType.unwrap() instanceof PercolatorFieldMapper.PercolatorFieldType)) {
512512
throw new QueryShardException(
513513
context,
514514
"expected field [" + field + "] to be of type [percolator], but is of type [" + fieldType.typeName() + "]"

server/src/internalClusterTest/java/org/opensearch/index/mapper/StarTreeMapperIT.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,8 @@ public void testValidCompositeIndex() {
602602
Set<CompositeMappedFieldType> fts = indexService.mapperService().getCompositeFieldTypes();
603603

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

645646
for (CompositeMappedFieldType ft : fts) {
646-
assertTrue(ft instanceof StarTreeMapper.StarTreeFieldType);
647+
assertFalse(ft == null);
648+
assertTrue(ft.unwrap() instanceof StarTreeMapper.StarTreeFieldType);
647649
StarTreeMapper.StarTreeFieldType starTreeFieldType = (StarTreeMapper.StarTreeFieldType) ft;
648650
assertEquals("timestamp", starTreeFieldType.getDimensions().get(0).getField());
649651
assertTrue(starTreeFieldType.getDimensions().get(0) instanceof DateDimension);
@@ -681,7 +683,8 @@ public void testValidCompositeIndexWithNestedFields() {
681683
Set<CompositeMappedFieldType> fts = indexService.mapperService().getCompositeFieldTypes();
682684

683685
for (CompositeMappedFieldType ft : fts) {
684-
assertTrue(ft instanceof StarTreeMapper.StarTreeFieldType);
686+
assertFalse(ft == null);
687+
assertTrue(ft.unwrap() instanceof StarTreeMapper.StarTreeFieldType);
685688
StarTreeMapper.StarTreeFieldType starTreeFieldType = (StarTreeMapper.StarTreeFieldType) ft;
686689
assertEquals("timestamp", starTreeFieldType.getDimensions().get(0).getField());
687690
assertTrue(starTreeFieldType.getDimensions().get(0) instanceof DateDimension);
@@ -721,7 +724,8 @@ public void testValidCompositeIndexWithDuplicateDates() {
721724
Set<CompositeMappedFieldType> fts = indexService.mapperService().getCompositeFieldTypes();
722725

723726
for (CompositeMappedFieldType ft : fts) {
724-
assertTrue(ft instanceof StarTreeMapper.StarTreeFieldType);
727+
assertFalse(ft == null);
728+
assertTrue(ft.unwrap() instanceof StarTreeMapper.StarTreeFieldType);
725729
StarTreeMapper.StarTreeFieldType starTreeFieldType = (StarTreeMapper.StarTreeFieldType) ft;
726730
assertEquals("timestamp", starTreeFieldType.getDimensions().get(0).getField());
727731
assertTrue(starTreeFieldType.getDimensions().get(0) instanceof DateDimension);
@@ -1053,7 +1057,8 @@ public void testUpdateIndexWhenMappingIsSame() {
10531057
Set<CompositeMappedFieldType> fts = indexService.mapperService().getCompositeFieldTypes();
10541058

10551059
for (CompositeMappedFieldType ft : fts) {
1056-
assertTrue(ft instanceof StarTreeMapper.StarTreeFieldType);
1060+
assertFalse(ft == null);
1061+
assertTrue(ft.unwrap() instanceof StarTreeMapper.StarTreeFieldType);
10571062
StarTreeMapper.StarTreeFieldType starTreeFieldType = (StarTreeMapper.StarTreeFieldType) ft;
10581063
assertEquals("timestamp", starTreeFieldType.getDimensions().get(0).getField());
10591064
assertTrue(starTreeFieldType.getDimensions().get(0) instanceof DateDimension);

server/src/main/java/org/opensearch/action/admin/indices/analyze/TransportAnalyzeAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private static Analyzer getAnalyzer(AnalyzeAction.Request request, AnalysisRegis
213213
}
214214
MappedFieldType fieldType = indexService.mapperService().fieldType(request.field());
215215
if (fieldType != null) {
216-
if (fieldType instanceof StringFieldType) {
216+
if (fieldType.unwrap() instanceof StringFieldType) {
217217
return fieldType.indexAnalyzer();
218218
} else {
219219
throw new IllegalArgumentException(

server/src/main/java/org/opensearch/index/codec/PerFieldMappingPostingFormatCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public PostingsFormat getPostingsFormatForField(String field) {
8888
final MappedFieldType fieldType = mapperService.fieldType(field);
8989
if (fieldType == null) {
9090
logger.warn("no index mapper found for field: [{}] returning default postings format", field);
91-
} else if (fieldType instanceof CompletionFieldMapper.CompletionFieldType) {
91+
} else if (fieldType.unwrap() instanceof CompletionFieldMapper.CompletionFieldType) {
9292
return CompletionFieldMapper.CompletionFieldType.postingsFormat();
9393
} else if (IdFieldMapper.NAME.equals(field) && mapperService.getIndexSettings().isEnableFuzzySetForDocId()) {
9494
if (docIdPostingsFormat == null) {

server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/StarTreeValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static void validate(MapperService mapperService, CompositeIndexSettings
4141
);
4242
}
4343
for (CompositeMappedFieldType compositeFieldType : compositeFieldTypes) {
44-
if (!(compositeFieldType instanceof StarTreeMapper.StarTreeFieldType)) {
44+
if (!(compositeFieldType != null && compositeFieldType.unwrap() instanceof StarTreeMapper.StarTreeFieldType)) {
4545
continue;
4646
}
4747
if (!compositeIndexSettings.isStarTreeIndexCreationEnabled()) {
@@ -79,7 +79,7 @@ public static void validate(MapperService mapperService, CompositeIndexSettings
7979
String.format(Locale.ROOT, "unknown metric field [%s] as part of star tree field", metric.getField())
8080
);
8181
}
82-
if (ft.isAggregatable() == false && ft instanceof DocCountFieldMapper.DocCountFieldType == false) {
82+
if (ft.isAggregatable() == false && ft.unwrap() instanceof DocCountFieldMapper.DocCountFieldType == false) {
8383
throw new IllegalArgumentException(
8484
String.format(
8585
Locale.ROOT,

server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/builder/StarTreesBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class StarTreesBuilder implements Closeable {
4747
public StarTreesBuilder(SegmentWriteState segmentWriteState, MapperService mapperService, AtomicInteger fieldNumberAcrossStarTrees) {
4848
List<StarTreeField> starTreeFields = new ArrayList<>();
4949
for (CompositeMappedFieldType compositeMappedFieldType : mapperService.getCompositeFieldTypes()) {
50-
if (compositeMappedFieldType instanceof StarTreeMapper.StarTreeFieldType) {
50+
if (compositeMappedFieldType != null && compositeMappedFieldType.unwrap() instanceof StarTreeMapper.StarTreeFieldType) {
5151
StarTreeMapper.StarTreeFieldType starTreeFieldType = (StarTreeMapper.StarTreeFieldType) compositeMappedFieldType;
5252
starTreeFields.add(
5353
new StarTreeField(

server/src/main/java/org/opensearch/index/mapper/DefaultDerivedFieldResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public Set<String> resolvePattern(String pattern) {
7272
Set<String> derivedFields = new HashSet<>();
7373
if (queryShardContext != null && queryShardContext.getMapperService() != null) {
7474
for (MappedFieldType fieldType : queryShardContext.getMapperService().fieldTypes()) {
75-
if (fieldType instanceof DerivedFieldType && Regex.simpleMatch(pattern, fieldType.name())) {
75+
if (fieldType != null && fieldType.unwrap() instanceof DerivedFieldType && Regex.simpleMatch(pattern, fieldType.name())) {
7676
derivedFields.add(fieldType.name());
7777
}
7878
}
@@ -223,7 +223,7 @@ private DerivedFieldType getDerivedFieldType(DerivedField derivedField) {
223223
private DerivedFieldType resolveUsingMappings(String name) {
224224
if (queryShardContext != null && queryShardContext.getMapperService() != null) {
225225
MappedFieldType mappedFieldType = queryShardContext.getMapperService().fieldType(name);
226-
if (mappedFieldType instanceof DerivedFieldType) {
226+
if (mappedFieldType != null && mappedFieldType.unwrap() instanceof DerivedFieldType) {
227227
return (DerivedFieldType) mappedFieldType;
228228
}
229229
}

0 commit comments

Comments
 (0)