Apache Lucene open-source search software
LUCENE-4173 remove ignoreIncompatibleGeometry, fail unless given the exact shape needed
David Wayne Smileydf5e4e7eb666
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java b/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.javaindex 624cd4ce345..7c9922e2fe1 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java@@ -19,9 +19,7 @@ package org.apache.lucene.spatial;import com.spatial4j.core.context.SpatialContext;import com.spatial4j.core.shape.Shape;-import org.apache.lucene.document.Field;-import org.apache.lucene.index.IndexableField;import org.apache.lucene.queries.function.FunctionQuery;import org.apache.lucene.queries.function.ValueSource;import org.apache.lucene.search.Filter;@@ -57,7 +55,6 @@ import org.apache.lucene.spatial.query.SpatialArgs;*/public abstract class SpatialStrategy {- protected boolean ignoreIncompatibleGeometry = false;protected final SpatialContext ctx;private final String fieldName;@@ -122,14 +119,6 @@ public abstract class SpatialStrategy {*/public abstract Filter makeFilter(SpatialArgs args);- public boolean isIgnoreIncompatibleGeometry() {- return ignoreIncompatibleGeometry;- }-- public void setIgnoreIncompatibleGeometry(boolean ignoreIncompatibleGeometry) {- this.ignoreIncompatibleGeometry = ignoreIncompatibleGeometry;- }-@Overridepublic String toString() {return getClass().getSimpleName()+" field:"+fieldName+" ctx="+ctx;diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/bbox/BBoxStrategy.java b/lucene/spatial/src/java/org/apache/lucene/spatial/bbox/BBoxStrategy.javaindex 91af948c1b3..0a90345f965 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/bbox/BBoxStrategy.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/bbox/BBoxStrategy.java@@ -24,7 +24,6 @@ import org.apache.lucene.document.DoubleField;import org.apache.lucene.document.Field;import org.apache.lucene.document.FieldType;import org.apache.lucene.document.StringField;-import org.apache.lucene.index.IndexableField;import org.apache.lucene.index.Term;import org.apache.lucene.queries.function.FunctionQuery;import org.apache.lucene.queries.function.ValueSource;@@ -93,7 +92,12 @@ public class BBoxStrategy extends SpatialStrategy {@Overridepublic Field[] createIndexableFields(Shape shape) {- Rectangle bbox = shape.getBoundingBox();+ if (shape instanceof Rectangle)+ return createIndexableFields((Rectangle)shape);+ throw new IllegalArgumentException("Can only index Rectangle, not " + shape);+ }++ public Field[] createIndexableFields(Rectangle bbox) {FieldType doubleFieldType = new FieldType(DoubleField.TYPE_NOT_STORED);doubleFieldType.setNumericPrecisionStep(precisionStep);Field[] fields = new Field[5];@@ -111,8 +115,11 @@ public class BBoxStrategy extends SpatialStrategy {@Overridepublic ValueSource makeValueSource(SpatialArgs args) {+ Shape shape = args.getShape();+ if (!(shape instanceof Rectangle))+ throw new IllegalArgumentException("Can only get valueSource by Rectangle, not " + shape);return new BBoxSimilarityValueSource(- this, new AreaSimilarity(args.getShape().getBoundingBox(), queryPower, targetPower));+ this, new AreaSimilarity((Rectangle)shape, queryPower, targetPower));}@@ -136,7 +143,11 @@ public class BBoxStrategy extends SpatialStrategy {private Query makeSpatialQuery(SpatialArgs args) {- Rectangle bbox = args.getShape().getBoundingBox();+ Shape shape = args.getShape();+ if (!(shape instanceof Rectangle))+ throw new IllegalArgumentException("Can only query by Rectangle, not " + shape);++ Rectangle bbox = (Rectangle) shape;Query spatial = null;// Useful for understanding Relations:diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.java b/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.javaindex d16e8cee72e..cf405b0cd31 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.java@@ -26,7 +26,6 @@ import com.spatial4j.core.shape.Shape;import org.apache.lucene.document.DoubleField;import org.apache.lucene.document.Field;import org.apache.lucene.document.FieldType;-import org.apache.lucene.index.IndexableField;import org.apache.lucene.queries.function.FunctionQuery;import org.apache.lucene.queries.function.ValueSource;import org.apache.lucene.search.BooleanClause;@@ -79,19 +78,19 @@ public class TwoDoublesStrategy extends SpatialStrategy {@Overridepublic Field[] createIndexableFields(Shape shape) {- if( shape instanceof Point ) {- Point point = (Point)shape;- FieldType doubleFieldType = new FieldType(DoubleField.TYPE_NOT_STORED);- doubleFieldType.setNumericPrecisionStep(precisionStep);- Field[] f = new Field[2];- f[0] = new DoubleField(fieldNameX, point.getX(), doubleFieldType);- f[1] = new DoubleField(fieldNameY, point.getY(), doubleFieldType);- return f;- }- if( !ignoreIncompatibleGeometry ) {- throw new IllegalArgumentException( "TwoDoublesStrategy can not index: "+shape );- }- return new Field[0]; // nothing (solr does not support null)+ if (shape instanceof Point)+ return createIndexableFields((Point) shape);+ throw new IllegalArgumentException("Can only index Point, not " + shape);+ }++ /** @see #createIndexableFields(com.spatial4j.core.shape.Shape) */+ public Field[] createIndexableFields(Point point) {+ FieldType doubleFieldType = new FieldType(DoubleField.TYPE_NOT_STORED);+ doubleFieldType.setNumericPrecisionStep(precisionStep);+ Field[] f = new Field[2];+ f[0] = new DoubleField(fieldNameX, point.getX(), doubleFieldType);+ f[1] = new DoubleField(fieldNameY, point.getY(), doubleFieldType);+ return f;}@Overridediff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.java b/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.javaindex ab056f919c3..b40358859dc 100644--- a/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.java+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.java@@ -85,17 +85,25 @@ public abstract class StrategyTestCase extends SpatialTestCase {document.add(new StringField("id", data.id, Field.Store.YES));document.add(new StringField("name", data.name, Field.Store.YES));Shape shape = new ShapeReadWriter(ctx).readShape(data.shape);- for (Field f : strategy.createIndexableFields(shape)) {- document.add(f);+ shape = convertShapeFromGetDocuments(shape);+ if (shape != null) {+ for (Field f : strategy.createIndexableFields(shape)) {+ document.add(f);+ }+ if (storeShape)+ document.add(new StoredField(strategy.getFieldName(), ctx.toString(shape)));}- if (storeShape)- document.add(new StoredField(strategy.getFieldName(), ctx.toString(shape)));documents.add(document);}return documents;}+ /** Subclasses may override to transform or remove a shape for indexing */+ protected Shape convertShapeFromGetDocuments(Shape shape) {+ return shape;+ }+protected Iterator<SampleData> getSampleData(String testDataFile) throws IOException {return new SampleDataReader(getClass().getClassLoader().getResourceAsStream("data/"+testDataFile) );diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/bbox/TestBBoxStrategy.java b/lucene/spatial/src/test/org/apache/lucene/spatial/bbox/TestBBoxStrategy.javaindex f15f36fc16e..6e2ef3478ba 100644--- a/lucene/spatial/src/test/org/apache/lucene/spatial/bbox/TestBBoxStrategy.java+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/bbox/TestBBoxStrategy.java@@ -18,6 +18,7 @@ package org.apache.lucene.spatial.bbox;*/import com.spatial4j.core.context.SpatialContext;+import com.spatial4j.core.shape.Shape;import org.apache.lucene.spatial.SpatialMatchConcern;import org.apache.lucene.spatial.StrategyTestCase;import org.junit.Before;@@ -35,6 +36,12 @@ public class TestBBoxStrategy extends StrategyTestCase {this.strategy = new BBoxStrategy(ctx, "bbox");}+ /* Convert DATA_WORLD_CITIES_POINTS to bbox */+ @Override+ protected Shape convertShapeFromGetDocuments(Shape shape) {+ return shape.getBoundingBox();+ }+@Testpublic void testBasicOperaions() throws IOException {getAddAndVerifyIndexedDocuments(DATA_SIMPLE_BBOX);