From 28aeeadb5f5d3ba74823da00250398b0a80a6ec1 Mon Sep 17 00:00:00 2001 From: Erik Hatcher Date: Wed, 26 Apr 2017 20:05:32 -0400 Subject: [PATCH] LUCENE-7808: Fix PayloadScoreQuery and SpanPayloadCheckQuery .equals and .hashCode methods. --- lucene/CHANGES.txt | 3 +++ .../queries/payloads/PayloadScoreQuery.java | 9 ++++--- .../payloads/SpanPayloadCheckQuery.java | 16 ++++++++++-- .../payloads/TestPayloadCheckQuery.java | 22 +++++++++++++++- .../payloads/TestPayloadScoreQuery.java | 25 +++++++++++++++++-- 5 files changed, 66 insertions(+), 9 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index f9cca9fdfad..83ce74761b8 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -100,6 +100,9 @@ Bug Fixes * LUCENE-7797: The static FSDirectory.listAll(Path) method was always returning an empty array. (Atkins Chang via Mike McCandless) +* LUCENE-7808: Fix PayloadScoreQuery and SpanPayloadCheckQuery + .equals and .hashCode methods. (Erik Hatcher) + Improvements * LUCENE-7782: OfflineSorter now passes the total number of items it diff --git a/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java index 5669406a41e..0cddd00a24e 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java @@ -82,7 +82,7 @@ public class PayloadScoreQuery extends SpanQuery { @Override public String toString(String field) { - return "PayloadSpanQuery[" + wrappedQuery.toString(field) + "; " + function.toString() + "]"; + return "PayloadScoreQuery[" + wrappedQuery.toString(field) + "; " + function.getClass().getSimpleName() + "; " + includeSpanScore + "]"; } @Override @@ -101,7 +101,7 @@ public class PayloadScoreQuery extends SpanQuery { private boolean equalsTo(PayloadScoreQuery other) { return wrappedQuery.equals(other.wrappedQuery) && - function.equals(other.function); + function.equals(other.function) && (includeSpanScore == other.includeSpanScore); } @Override @@ -109,6 +109,7 @@ public class PayloadScoreQuery extends SpanQuery { int result = classHash(); result = 31 * result + Objects.hashCode(wrappedQuery); result = 31 * result + Objects.hashCode(function); + result = 31 * result + Objects.hashCode(includeSpanScore); return result; } @@ -132,7 +133,7 @@ public class PayloadScoreQuery extends SpanQuery { } @Override - public PayloadSpanScorer scorer(LeafReaderContext context) throws IOException { + public SpanScorer scorer(LeafReaderContext context) throws IOException { Spans spans = getSpans(context, Postings.PAYLOADS); if (spans == null) return null; @@ -148,7 +149,7 @@ public class PayloadScoreQuery extends SpanQuery { @Override public Explanation explain(LeafReaderContext context, int doc) throws IOException { - PayloadSpanScorer scorer = scorer(context); + PayloadSpanScorer scorer = (PayloadSpanScorer)scorer(context); if (scorer == null || scorer.iterator().advance(doc) != doc) return Explanation.noMatch("No match"); diff --git a/lucene/queries/src/java/org/apache/lucene/queries/payloads/SpanPayloadCheckQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/payloads/SpanPayloadCheckQuery.java index 57ba678e34d..e33eb184ce9 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/payloads/SpanPayloadCheckQuery.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/payloads/SpanPayloadCheckQuery.java @@ -18,14 +18,17 @@ package org.apache.lucene.queries.payloads; import java.io.IOException; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermContext; import org.apache.lucene.index.Terms; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; import org.apache.lucene.search.similarities.Similarity; import org.apache.lucene.search.spans.FilterSpans; import org.apache.lucene.search.spans.FilterSpans.AcceptStatus; @@ -64,6 +67,11 @@ public class SpanPayloadCheckQuery extends SpanQuery { return new SpanPayloadCheckWeight(searcher, needsScores ? getTermContexts(matchWeight) : null, matchWeight, boost); } + @Override + public Query rewrite(IndexReader reader) throws IOException { + return super.rewrite(reader); + } + /** * Weight that pulls its Spans using a PayloadSpanCollector */ @@ -175,11 +183,15 @@ public class SpanPayloadCheckQuery extends SpanQuery { @Override public boolean equals(Object other) { return sameClassAs(other) && - payloadToMatch.equals(((SpanPayloadCheckQuery) other).payloadToMatch); + payloadToMatch.equals(((SpanPayloadCheckQuery) other).payloadToMatch) && + match.equals(((SpanPayloadCheckQuery) other).match); } @Override public int hashCode() { - return classHash() ^ payloadToMatch.hashCode(); + int result = classHash(); + result = 31 * result + Objects.hashCode(match); + result = 31 * result + Objects.hashCode(payloadToMatch); + return result; } } \ No newline at end of file diff --git a/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadCheckQuery.java b/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadCheckQuery.java index 7067ef201f1..ffe2cb8361a 100644 --- a/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadCheckQuery.java +++ b/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadCheckQuery.java @@ -188,4 +188,24 @@ public class TestPayloadCheckQuery extends LuceneTestCase { checkHits(query, new int[]{1103, 1203,1303,1403,1503,1603,1703,1803,1903}); } -} + + public void testEquality() { + SpanQuery sq1 = new SpanTermQuery(new Term("field", "one")); + SpanQuery sq2 = new SpanTermQuery(new Term("field", "two")); + BytesRef payload1 = new BytesRef("pay1"); + BytesRef payload2 = new BytesRef("pay2"); + SpanQuery query1 = new SpanPayloadCheckQuery(sq1, Collections.singletonList(payload1)); + SpanQuery query2 = new SpanPayloadCheckQuery(sq2, Collections.singletonList(payload1)); + SpanQuery query3 = new SpanPayloadCheckQuery(sq1, Collections.singletonList(payload2)); + SpanQuery query4 = new SpanPayloadCheckQuery(sq2, Collections.singletonList(payload2)); + SpanQuery query5 = new SpanPayloadCheckQuery(sq1, Collections.singletonList(payload1)); + + assertEquals(query1, query5); + assertFalse(query1.equals(query2)); + assertFalse(query1.equals(query3)); + assertFalse(query1.equals(query4)); + assertFalse(query2.equals(query3)); + assertFalse(query2.equals(query4)); + assertFalse(query3.equals(query4)); + } +} \ No newline at end of file diff --git a/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.java b/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.java index 2afec44b2a1..fa387762b6b 100644 --- a/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.java +++ b/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.java @@ -172,10 +172,31 @@ public class TestPayloadScoreQuery extends LuceneTestCase { } + @Test + public void testEquality() { + SpanQuery sq1 = new SpanTermQuery(new Term("field", "one")); + SpanQuery sq2 = new SpanTermQuery(new Term("field", "two")); + PayloadFunction minFunc = new MinPayloadFunction(); + PayloadFunction maxFunc = new MaxPayloadFunction(); + PayloadScoreQuery query1 = new PayloadScoreQuery(sq1, minFunc, true); + PayloadScoreQuery query2 = new PayloadScoreQuery(sq2, minFunc, true); + PayloadScoreQuery query3 = new PayloadScoreQuery(sq2, maxFunc, true); + PayloadScoreQuery query4 = new PayloadScoreQuery(sq2, maxFunc, false); + PayloadScoreQuery query5 = new PayloadScoreQuery(sq1, minFunc); + + assertEquals(query1, query5); + assertFalse(query1.equals(query2)); + assertFalse(query1.equals(query3)); + assertFalse(query1.equals(query4)); + assertFalse(query2.equals(query3)); + assertFalse(query2.equals(query4)); + assertFalse(query3.equals(query4)); + } + private static IndexSearcher searcher; private static IndexReader reader; private static Directory directory; - private static BoostingSimilarity similarity = new BoostingSimilarity(); + private static JustScorePayloadSimilarity similarity = new JustScorePayloadSimilarity(); private static byte[] payload2 = new byte[]{2}; private static byte[] payload4 = new byte[]{4}; @@ -260,7 +281,7 @@ public class TestPayloadScoreQuery extends LuceneTestCase { } - static class BoostingSimilarity extends MultiplyingSimilarity { + static class JustScorePayloadSimilarity extends MultiplyingSimilarity { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //Make everything else 1 so we see the effect of the payload