Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions docs/changelog/130924.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 130924
summary: Check field data type before casting when applying geo distance sort
area: Search
type: bug
issues:
- 129500
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,13 @@ private IndexGeoPointFieldData fieldData(SearchExecutionContext context) {
throw new IllegalArgumentException("failed to find mapper for [" + fieldName + "] for geo distance based sort");
}
}
return context.getForField(fieldType, MappedFieldType.FielddataOperation.SEARCH);
IndexFieldData<?> indexFieldData = context.getForField(fieldType, MappedFieldType.FielddataOperation.SEARCH);
if (indexFieldData instanceof IndexGeoPointFieldData) {
return (IndexGeoPointFieldData) indexFieldData;
}
throw new IllegalArgumentException(
"unable to apply geo distance sort to field [" + fieldName + "] of type [" + fieldType.typeName() + "]"
);
}

private Nested nested(SearchExecutionContext context) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.elasticsearch.index.mapper.GeoPointFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.NestedPathFieldMapper;
import org.elasticsearch.index.mapper.NumberFieldMapper;
import org.elasticsearch.index.query.GeoValidationMethod;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchNoneQueryBuilder;
Expand Down Expand Up @@ -99,6 +100,9 @@ public static GeoDistanceSortBuilder randomGeoDistanceSortBuilder() {

@Override
protected MappedFieldType provideMappedFieldType(String name) {
if (name.equals("double")) {
return new NumberFieldMapper.NumberFieldType(name, NumberFieldMapper.NumberType.DOUBLE);
}
return new GeoPointFieldMapper.GeoPointFieldType(name);
}

Expand Down Expand Up @@ -531,6 +535,12 @@ public void testBuildInvalidPoints() throws IOException {
);
assertEquals("illegal longitude value [-360.0] for [GeoDistanceSort] for field [fieldName].", ex.getMessage());
}
{
GeoDistanceSortBuilder sortBuilder = new GeoDistanceSortBuilder("double", 0.0, 180.0);
sortBuilder.validation(GeoValidationMethod.STRICT);
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> sortBuilder.build(searchExecutionContext));
assertEquals("unable to apply geo distance sort to field [double] of type [double]", ex.getMessage());
}
}

/**
Expand Down
Loading