Apache Lucene open-source search software
LUCENE-6567: Simplify payload checks in SpanPayloadCheckQuery
Alan Woodward8d8818fea11c
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 36a35142263..93623c345b3 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -167,6 +167,9 @@ Build* LUCENE-6518: Don't report false thread leaks from IBM J9ClassCache Reaper in test framework. (Dawid Weiss)+* LUCENE-6567: Simplify payload checking in SpanPayloadCheckQuery (Alan+ Woodward)+======================= Lucene 5.2.1 =======================Bug Fixesdiff --git a/lucene/core/src/java/org/apache/lucene/search/payloads/SpanPayloadCheckQuery.java b/lucene/core/src/java/org/apache/lucene/search/payloads/SpanPayloadCheckQuery.javaindex 898f66c145a..56bc406cba1 100644--- a/lucene/core/src/java/org/apache/lucene/search/payloads/SpanPayloadCheckQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/payloads/SpanPayloadCheckQuery.java@@ -16,6 +16,13 @@ package org.apache.lucene.search.payloads;* limitations under the License.*/+import java.io.IOException;+import java.util.Arrays;+import java.util.Collection;+import java.util.Iterator;+import java.util.Map;+import java.util.Set;+import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.index.Term;import org.apache.lucene.index.TermContext;@@ -25,7 +32,6 @@ import org.apache.lucene.search.Scorer;import org.apache.lucene.search.similarities.Similarity;import org.apache.lucene.search.spans.FilterSpans;import org.apache.lucene.search.spans.FilterSpans.AcceptStatus;-import org.apache.lucene.search.spans.SpanNearQuery;import org.apache.lucene.search.spans.SpanQuery;import org.apache.lucene.search.spans.SpanScorer;import org.apache.lucene.search.spans.SpanWeight;@@ -33,14 +39,6 @@ import org.apache.lucene.search.spans.Spans;import org.apache.lucene.util.Bits;import org.apache.lucene.util.ToStringUtils;-import java.io.IOException;-import java.util.Arrays;-import java.util.Collection;-import java.util.Iterator;-import java.util.Map;-import java.util.Set;--/*** Only return those matches that have a specific payload at the given position.*/@@ -98,17 +96,9 @@ public class SpanPayloadCheckQuery extends SpanQuery {return (matchSpans == null) ? null : new FilterSpans(matchSpans) {@Overrideprotected AcceptStatus accept(Spans candidate) throws IOException {-collector.reset();candidate.collect(collector);- Collection<byte[]> collected = collector.getPayloads();-- if (match instanceof SpanNearQuery) {- return checkCompositePayloads(collected);- }- else {- return checkOrderedPayloads(collected);- }+ return checkPayloads(collector.getPayloads());}};}@@ -132,19 +122,14 @@ public class SpanPayloadCheckQuery extends SpanQuery {/*** Check to see if the collected payloads match the required set.*- * This is called for Near span queries which collect their sub spans- * out-of-order, meaning that we can't rely on the order of payloads- * in the collection- ** @param candidate a collection of payloads from the current Spans* @return whether or not the payloads match*/- protected AcceptStatus checkOrderedPayloads(Collection<byte[]> candidate) {+ protected AcceptStatus checkPayloads(Collection<byte[]> candidate) {if (candidate.size() == payloadToMatch.size()){//TODO: check the byte arrays are the sameIterator<byte[]> toMatchIter = payloadToMatch.iterator();//check each of the byte arrays, in order- //hmm, can't rely on order herefor (byte[] candBytes : candidate) {//if one is a mismatch, then return falseif (Arrays.equals(candBytes, toMatchIter.next()) == false){@@ -158,36 +143,6 @@ public class SpanPayloadCheckQuery extends SpanQuery {}}- /**- * Check to see if the collected payloads match the required set.- * @param candidate a collection of payloads from the current Spans- * @return whether or not the payloads match- */- protected AcceptStatus checkCompositePayloads(Collection<byte[]> candidate) {- if (candidate.size() == payloadToMatch.size()) {- //TODO: check the byte arrays are the same- //hmm, can't rely on order here- int matches = 0;- for (byte[] candBytes : candidate) {- //Unfortunately, we can't rely on order, so we need to compare all- for (byte[] payBytes : payloadToMatch) {- if (Arrays.equals(candBytes, payBytes) == true) {- matches++;- break;- }- }- }- if (matches == payloadToMatch.size()){- //we've verified all the bytes- return AcceptStatus.YES;- } else {- return AcceptStatus.NO;- }- } else {- return AcceptStatus.NO;- }- }-@Overridepublic String toString(String field) {StringBuilder buffer = new StringBuilder();diff --git a/lucene/core/src/test/org/apache/lucene/search/payloads/TestPayloadBasics.java b/lucene/core/src/test/org/apache/lucene/search/payloads/TestPayloadBasics.javaindex a2613ac4025..7c27d47148f 100644--- a/lucene/core/src/test/org/apache/lucene/search/payloads/TestPayloadBasics.java+++ b/lucene/core/src/test/org/apache/lucene/search/payloads/TestPayloadBasics.java@@ -134,6 +134,23 @@ public class TestPayloadBasics extends LuceneTestCase {{505});}+ public void testUnorderedPayloadChecks() throws Exception {++ SpanTermQuery term5 = new SpanTermQuery(new Term("field", "five"));+ SpanTermQuery term100 = new SpanTermQuery(new Term("field", "hundred"));+ SpanTermQuery term4 = new SpanTermQuery(new Term("field", "four"));+ SpanNearQuery nearQuery = new SpanNearQuery(new SpanQuery[]{term5, term100, term4}, 0, false);++ List<byte[]> payloads = new ArrayList<>();+ payloads.add(("pos: " + 2).getBytes(StandardCharsets.UTF_8));+ payloads.add(("pos: " + 1).getBytes(StandardCharsets.UTF_8));+ payloads.add(("pos: " + 0).getBytes(StandardCharsets.UTF_8));++ SpanPayloadCheckQuery payloadQuery = new SpanPayloadCheckQuery(nearQuery, payloads);+ checkHits(payloadQuery, new int[]{ 405 });++ }+public void testComplexSpanChecks() throws Exception {SpanTermQuery one = new SpanTermQuery(new Term("field", "one"));SpanTermQuery thous = new SpanTermQuery(new Term("field", "thousand"));