Apache Lucene open-source search software
LUCENE-2760: Optimize SpanFirstQuery,SpanPositionRangeQuery
Robert Muir0123357efac5
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 1b72d34dfc3..5d600cb7b70 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -801,6 +801,9 @@ Optimizationsby the improved SorterTemplate class.(Uwe Schindler, Robert Muir, Mike McCandless)+* LUCENE-2760: Optimize SpanFirstQuery and SpanPositionRangeQuery.+ (Robert Muir)+Build* LUCENE-2124: Moved the JDK-based collation support from contrib/collationdiff --git a/lucene/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java b/lucene/src/java/org/apache/lucene/search/spans/SpanFirstQuery.javaindex caee3297447..4c9adafb7cf 100644--- a/lucene/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java+++ b/lucene/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java@@ -37,8 +37,14 @@ public class SpanFirstQuery extends SpanPositionRangeQuery {}@Override- protected boolean acceptPosition(Spans spans) throws IOException {- return spans.end() <= end;+ protected AcceptStatus acceptPosition(Spans spans) throws IOException {+ assert spans.start() != spans.end();+ if (spans.start() >= end)+ return AcceptStatus.NO_AND_ADVANCE;+ else if (spans.end() <= end)+ return AcceptStatus.YES;+ else+ return AcceptStatus.NO;}diff --git a/lucene/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java b/lucene/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.javaindex 960794458c6..3618adc6390 100644--- a/lucene/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java+++ b/lucene/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java@@ -43,7 +43,7 @@ public class SpanNearPayloadCheckQuery extends SpanPositionCheckQuery {}@Override- protected boolean acceptPosition(Spans spans) throws IOException {+ protected AcceptStatus acceptPosition(Spans spans) throws IOException {boolean result = spans.isPayloadAvailable();if (result == true) {Collection<byte[]> candidate = spans.getPayload();@@ -62,15 +62,15 @@ public class SpanNearPayloadCheckQuery extends SpanPositionCheckQuery {}if (matches == payloadToMatch.size()){//we've verified all the bytes- return true;+ return AcceptStatus.YES;} else {- return false;+ return AcceptStatus.NO;}} else {- return false;+ return AcceptStatus.NO;}}- return false;+ return AcceptStatus.NO;}public String toString(String field) {diff --git a/lucene/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java b/lucene/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.javaindex f84f8b96327..69dbc306f19 100644--- a/lucene/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java+++ b/lucene/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java@@ -50,7 +50,7 @@ public class SpanPayloadCheckQuery extends SpanPositionCheckQuery{}@Override- protected boolean acceptPosition(Spans spans) throws IOException {+ protected AcceptStatus acceptPosition(Spans spans) throws IOException {boolean result = spans.isPayloadAvailable();if (result == true){Collection<byte[]> candidate = spans.getPayload();@@ -62,16 +62,16 @@ public class SpanPayloadCheckQuery extends SpanPositionCheckQuery{for (byte[] candBytes : candidate) {//if one is a mismatch, then return falseif (Arrays.equals(candBytes, toMatchIter.next()) == false){- return false;+ return AcceptStatus.NO;}}//we've verified all the bytes- return true;+ return AcceptStatus.YES;} else {- return false;+ return AcceptStatus.NO;}}- return result;+ return AcceptStatus.YES;}public String toString(String field) {diff --git a/lucene/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java b/lucene/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.javaindex 7ff588333f4..a2deeb69731 100644--- a/lucene/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java+++ b/lucene/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java@@ -57,6 +57,12 @@ public abstract class SpanPositionCheckQuery extends SpanQuery implements Cloneamatch.extractTerms(terms);}+ /** Return value if the match should be accepted {@code YES}, rejected {@code NO},+ * or rejected and enumeration should advance to the next document {@code NO_AND_ADVANCE}.+ * @see #acceptPosition(Spans)+ */+ protected static enum AcceptStatus { YES, NO, NO_AND_ADVANCE };+/*** Implementing classes are required to return whether the current position is a match for the passed in* "match" {@link org.apache.lucene.search.spans.SpanQuery}.@@ -66,36 +72,12 @@ public abstract class SpanPositionCheckQuery extends SpanQuery implements Clonea*** @param spans The {@link org.apache.lucene.search.spans.Spans} instance, positioned at the spot to check- * @return true if it is a match, else false.+ * @return whether the match is accepted, rejected, or rejected and should move to the next doc.** @see org.apache.lucene.search.spans.Spans#next()**/- protected abstract boolean acceptPosition(Spans spans) throws IOException;-- /**- * Implementing classes are required to return whether the position at the target is someplace that- * can be skipped to. For instance, the {@link org.apache.lucene.search.spans.SpanFirstQuery} returns- * false if the target position is beyond the maximum position allowed or if {@link Spans#next()} is true.- * <p/>- * Note, this method is only called if the underlying match {@link org.apache.lucene.search.spans.SpanQuery} can- * skip to the target.- * <p/>- * It is safe to assume that the passed in {@link org.apache.lucene.search.spans.Spans} object for the underlying {@link org.apache.lucene.search.spans.SpanQuery} is- * positioned at the target.- * <p/>- * The default implementation is to return true if either {@link #acceptPosition(Spans)} or {@link org.apache.lucene.search.spans.Spans#next()} is true for the- * passed in instance of Spans.- *<p/>- * @param spans The {@link org.apache.lucene.search.spans.Spans} to check- * @return true if the instance can skip to this position- *- * @see Spans#skipTo(int)- * @throws java.io.IOException if there is a low-level IO error- */- protected boolean acceptSkipTo(Spans spans) throws IOException{- return acceptPosition(spans) || spans.next();- }+ protected abstract AcceptStatus acceptPosition(Spans spans) throws IOException;@Overridepublic Spans getSpans(final IndexReader reader) throws IOException {@@ -123,21 +105,16 @@ public abstract class SpanPositionCheckQuery extends SpanQuery implements Cloneaprotected class PositionCheckSpan extends Spans {private Spans spans;- private final IndexReader reader;-public PositionCheckSpan(IndexReader reader) throws IOException {- this.reader = reader;spans = match.getSpans(reader);}@Overridepublic boolean next() throws IOException {- //TODO: optimize to skip ahead to start- while (spans.next()) { // scan to next match- if (acceptPosition(this))- return true;- }- return false;+ if (!spans.next())+ return false;++ return doNext();}@Override@@ -145,8 +122,23 @@ public abstract class SpanPositionCheckQuery extends SpanQuery implements Cloneaif (!spans.skipTo(target))return false;- return acceptSkipTo(this);-+ return doNext();+ }++ protected boolean doNext() throws IOException {+ for (;;) {+ switch(acceptPosition(this)) {+ case YES: return true;+ case NO:+ if (!spans.next())+ return false;+ break;+ case NO_AND_ADVANCE:+ if (!spans.skipTo(spans.doc()+1))+ return false;+ break;+ }+ }}@Overridediff --git a/lucene/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java b/lucene/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.javaindex 57fc22d6f53..3e023ed3592 100644--- a/lucene/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java+++ b/lucene/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java@@ -39,8 +39,14 @@ public class SpanPositionRangeQuery extends SpanPositionCheckQuery {@Override- protected boolean acceptPosition(Spans spans) throws IOException {- return spans.start() >= start && spans.end() <= end;+ protected AcceptStatus acceptPosition(Spans spans) throws IOException {+ assert spans.start() != spans.end();+ if (spans.start() >= end)+ return AcceptStatus.NO_AND_ADVANCE;+ else if (spans.start() >= start && spans.end() <= end)+ return AcceptStatus.YES;+ else+ return AcceptStatus.NO;}