Apache Lucene open-source search software
LUCENE-4157 Improve TermQueryPrefixTreeStrategy and move makeQuery impl to SpatialStrategy
David Wayne Smiley42abb41cddc2
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java b/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.javaindex 26816116aac..5931fc5ac06 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java@@ -21,8 +21,10 @@ import com.spatial4j.core.context.SpatialContext;import com.spatial4j.core.query.SpatialArgs;import com.spatial4j.core.shape.Shape;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;+import org.apache.lucene.search.FilteredQuery;import org.apache.lucene.search.Query;/**@@ -60,13 +62,25 @@ public abstract class SpatialStrategy<T extends SpatialFieldInfo> {return new IndexableField[] { createField(fieldInfo, shape, index, store) };}+ /**+ * The value source yields a number that is proportional to the distance between the query shape and indexed data.+ * @param args+ * @param fieldInfo+ * @return+ */public abstract ValueSource makeValueSource(SpatialArgs args, T fieldInfo);/**- * Make a query+ * Make a query which has a score based on the distance from the data to the query shape.+ * The default implementation constructs a {@link FilteredQuery} based on+ * {@link #makeFilter(com.spatial4j.core.query.SpatialArgs, SpatialFieldInfo)} and+ * {@link #makeValueSource(com.spatial4j.core.query.SpatialArgs, SpatialFieldInfo)}.*/- public abstract Query makeQuery(SpatialArgs args, T fieldInfo);-+ public Query makeQuery(SpatialArgs args, T fieldInfo) {+ Filter filter = makeFilter(args, fieldInfo);+ ValueSource vs = makeValueSource(args, fieldInfo);+ return new FilteredQuery(new FunctionQuery(vs), filter);+ }/*** Make a Filter*/diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/RecursivePrefixTreeStrategy.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/RecursivePrefixTreeStrategy.javaindex 76b684e244b..22e5f087cea 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/RecursivePrefixTreeStrategy.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/RecursivePrefixTreeStrategy.java@@ -21,15 +21,13 @@ import com.spatial4j.core.exception.UnsupportedSpatialOperation;import com.spatial4j.core.query.SpatialArgs;import com.spatial4j.core.query.SpatialOperation;import com.spatial4j.core.shape.Shape;-import org.apache.lucene.queries.function.FunctionQuery;-import org.apache.lucene.queries.function.ValueSource;import org.apache.lucene.search.Filter;-import org.apache.lucene.search.FilteredQuery;-import org.apache.lucene.search.Query;import org.apache.lucene.spatial.SimpleSpatialFieldInfo;import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;-+/**+ * Based on {@link RecursivePrefixTreeFilter}.+ */public class RecursivePrefixTreeStrategy extends PrefixTreeStrategy {private int prefixGridScanLevel;//TODO how is this customized?@@ -48,26 +46,18 @@ public class RecursivePrefixTreeStrategy extends PrefixTreeStrategy {return getClass().getSimpleName()+"(prefixGridScanLevel:"+prefixGridScanLevel+",SPG:("+ grid +"))";}- @Override- public Query makeQuery(SpatialArgs args, SimpleSpatialFieldInfo fieldInfo) {- Filter f = makeFilter(args, fieldInfo);-- ValueSource vs = makeValueSource(args, fieldInfo);- return new FilteredQuery( new FunctionQuery(vs), f );- }-@Overridepublic Filter makeFilter(SpatialArgs args, SimpleSpatialFieldInfo fieldInfo) {final SpatialOperation op = args.getOperation();- if (! SpatialOperation.is(op, SpatialOperation.IsWithin, SpatialOperation.Intersects, SpatialOperation.BBoxWithin))+ if (! SpatialOperation.is(op, SpatialOperation.IsWithin, SpatialOperation.Intersects, SpatialOperation.BBoxWithin, SpatialOperation.BBoxIntersects))throw new UnsupportedSpatialOperation(op);- Shape qshape = args.getShape();+ Shape shape = args.getShape();- int detailLevel = grid.getMaxLevelForPrecision(qshape,args.getDistPrecision());+ int detailLevel = grid.getMaxLevelForPrecision(shape,args.getDistPrecision());return new RecursivePrefixTreeFilter(- fieldInfo.getFieldName(), grid,qshape, prefixGridScanLevel, detailLevel);+ fieldInfo.getFieldName(), grid,shape, prefixGridScanLevel, detailLevel);}}diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/TermQueryPrefixTreeStrategy.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/TermQueryPrefixTreeStrategy.javaindex fbe0325a172..cfebe58c00c 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/TermQueryPrefixTreeStrategy.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/TermQueryPrefixTreeStrategy.java@@ -22,13 +22,18 @@ import com.spatial4j.core.query.SpatialArgs;import com.spatial4j.core.query.SpatialOperation;import com.spatial4j.core.shape.Shape;import org.apache.lucene.index.Term;-import org.apache.lucene.search.*;+import org.apache.lucene.queries.TermsFilter;+import org.apache.lucene.search.Filter;import org.apache.lucene.spatial.SimpleSpatialFieldInfo;import org.apache.lucene.spatial.prefix.tree.Node;import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;import java.util.List;+/**+ * A basic implementation using a large {@link TermsFilter} of all the nodes from+ * {@link SpatialPrefixTree#getNodes(com.spatial4j.core.shape.Shape, int, boolean)}.+ */public class TermQueryPrefixTreeStrategy extends PrefixTreeStrategy {public TermQueryPrefixTreeStrategy(SpatialPrefixTree grid) {@@ -37,26 +42,18 @@ public class TermQueryPrefixTreeStrategy extends PrefixTreeStrategy {@Overridepublic Filter makeFilter(SpatialArgs args, SimpleSpatialFieldInfo fieldInfo) {- return new QueryWrapperFilter( makeQuery(args, fieldInfo) );- }-- @Override- public Query makeQuery(SpatialArgs args, SimpleSpatialFieldInfo fieldInfo) {- if (args.getOperation() != SpatialOperation.Intersects &&- args.getOperation() != SpatialOperation.IsWithin &&- args.getOperation() != SpatialOperation.Overlaps ){- // TODO -- can translate these other query types- throw new UnsupportedSpatialOperation(args.getOperation());- }- Shape qshape = args.getShape();- int detailLevel = grid.getMaxLevelForPrecision(qshape, args.getDistPrecision());- List<Node> cells = grid.getNodes(qshape, detailLevel, false);-- BooleanQuery booleanQuery = new BooleanQuery();+ final SpatialOperation op = args.getOperation();+ if (! SpatialOperation.is(op, SpatialOperation.IsWithin, SpatialOperation.Intersects, SpatialOperation.BBoxWithin, SpatialOperation.BBoxIntersects))+ throw new UnsupportedSpatialOperation(op);++ Shape shape = args.getShape();+ int detailLevel = grid.getMaxLevelForPrecision(shape, args.getDistPrecision());+ List<Node> cells = grid.getNodes(shape, detailLevel, false);+ TermsFilter filter = new TermsFilter();for (Node cell : cells) {- booleanQuery.add(new TermQuery(new Term(fieldInfo.getFieldName(), cell.getTokenString())), BooleanClause.Occur.SHOULD);+ filter.addTerm(new Term(fieldInfo.getFieldName(), cell.getTokenString()));}- return booleanQuery;+ return filter;}}