GeoPointField now validates incoming lat/lon

This commit is contained in:
Mike McCandless 2016-03-23 04:42:15 -04:00
parent 574da7667f
commit 6a5e935aa6
2 changed files with 26 additions and 2 deletions

View File

@ -16,13 +16,14 @@
*/
package org.apache.lucene.spatial.geopoint.document;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.spatial.util.GeoEncodingUtils;
import org.apache.lucene.spatial.util.GeoUtils;
/**
* <p>
@ -162,6 +163,14 @@ public final class GeoPointField extends Field {
public GeoPointField(String name, double lat, double lon, FieldType type) {
super(name, type);
if (GeoUtils.isValidLat(lat) == false) {
throw new IllegalArgumentException("invalid lat=" + lat + " for field \"" + name + "\"");
}
if (GeoUtils.isValidLon(lon) == false) {
throw new IllegalArgumentException("invalid lon=" + lon + " for field \"" + name + "\"");
}
// field must be indexed
// todo does it make sense here to provide the ability to store a GeoPointField but not index?
if (type.indexOptions() == IndexOptions.NONE && type.stored() == false) {

View File

@ -237,4 +237,19 @@ public class TestGeoPointField extends LuceneTestCase {
TopDocs td = geoDistanceRangeQuery(0.0, 0.0, 10, 20000000, 20);
assertEquals("GeoDistanceRangeQuery failed", 24, td.totalHits);
}
public void testInvalidLatLon() throws Exception {
IllegalArgumentException e;
e= expectThrows(IllegalArgumentException.class,
() -> {
new GeoPointField("field", 180.0, 0.0, Field.Store.NO);
});
assertEquals("invalid lat=180.0 for field \"field\"", e.getMessage());
e = expectThrows(IllegalArgumentException.class,
() -> {
new GeoPointField("field", 0.0, 190.0, Field.Store.NO);
});
assertEquals("invalid lon=190.0 for field \"field\"", e.getMessage());
}
}