Skip to content

Commit 08a0247

Browse files
authored
Check field data type before casting when applying geo distance sort (elastic#130924) (elastic#131020)
If a user tries to apply geo distance sorting to a field of the wrong type, they'll get a 500 error that causes shard failures, because the field data impl gets casted to the expected type without first checking that the field type is the one expected. This commit addresses that by returning a 400 error instead. Closes elastic#129500
1 parent e2ca3a4 commit 08a0247

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

docs/changelog/130924.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 130924
2+
summary: Check field data type before casting when applying geo distance sort
3+
area: Search
4+
type: bug
5+
issues:
6+
- 129500

server/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,13 @@ private IndexGeoPointFieldData fieldData(SearchExecutionContext context) {
624624
throw new IllegalArgumentException("failed to find mapper for [" + fieldName + "] for geo distance based sort");
625625
}
626626
}
627-
return context.getForField(fieldType, MappedFieldType.FielddataOperation.SEARCH);
627+
IndexFieldData<?> indexFieldData = context.getForField(fieldType, MappedFieldType.FielddataOperation.SEARCH);
628+
if (indexFieldData instanceof IndexGeoPointFieldData) {
629+
return (IndexGeoPointFieldData) indexFieldData;
630+
}
631+
throw new IllegalArgumentException(
632+
"unable to apply geo distance sort to field [" + fieldName + "] of type [" + fieldType.typeName() + "]"
633+
);
628634
}
629635

630636
private Nested nested(SearchExecutionContext context) throws IOException {

server/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.index.mapper.GeoPointFieldMapper;
2424
import org.elasticsearch.index.mapper.MappedFieldType;
2525
import org.elasticsearch.index.mapper.NestedPathFieldMapper;
26+
import org.elasticsearch.index.mapper.NumberFieldMapper;
2627
import org.elasticsearch.index.query.GeoValidationMethod;
2728
import org.elasticsearch.index.query.MatchAllQueryBuilder;
2829
import org.elasticsearch.index.query.MatchNoneQueryBuilder;
@@ -99,6 +100,9 @@ public static GeoDistanceSortBuilder randomGeoDistanceSortBuilder() {
99100

100101
@Override
101102
protected MappedFieldType provideMappedFieldType(String name) {
103+
if (name.equals("double")) {
104+
return new NumberFieldMapper.NumberFieldType(name, NumberFieldMapper.NumberType.DOUBLE);
105+
}
102106
return new GeoPointFieldMapper.GeoPointFieldType(name);
103107
}
104108

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

536546
/**

0 commit comments

Comments
 (0)