LUCENE-7808: Fix PayloadScoreQuery and SpanPayloadCheckQuery .equals and .hashCode methods.

This commit is contained in:
Erik Hatcher 2017-04-26 20:05:32 -04:00
parent 90b3ef18de
commit 28aeeadb5f
5 changed files with 66 additions and 9 deletions

View File

@ -100,6 +100,9 @@ Bug Fixes
* LUCENE-7797: The static FSDirectory.listAll(Path) method was always * LUCENE-7797: The static FSDirectory.listAll(Path) method was always
returning an empty array. (Atkins Chang via Mike McCandless) returning an empty array. (Atkins Chang via Mike McCandless)
* LUCENE-7808: Fix PayloadScoreQuery and SpanPayloadCheckQuery
.equals and .hashCode methods. (Erik Hatcher)
Improvements Improvements
* LUCENE-7782: OfflineSorter now passes the total number of items it * LUCENE-7782: OfflineSorter now passes the total number of items it

View File

@ -82,7 +82,7 @@ public class PayloadScoreQuery extends SpanQuery {
@Override @Override
public String toString(String field) { public String toString(String field) {
return "PayloadSpanQuery[" + wrappedQuery.toString(field) + "; " + function.toString() + "]"; return "PayloadScoreQuery[" + wrappedQuery.toString(field) + "; " + function.getClass().getSimpleName() + "; " + includeSpanScore + "]";
} }
@Override @Override
@ -101,7 +101,7 @@ public class PayloadScoreQuery extends SpanQuery {
private boolean equalsTo(PayloadScoreQuery other) { private boolean equalsTo(PayloadScoreQuery other) {
return wrappedQuery.equals(other.wrappedQuery) && return wrappedQuery.equals(other.wrappedQuery) &&
function.equals(other.function); function.equals(other.function) && (includeSpanScore == other.includeSpanScore);
} }
@Override @Override
@ -109,6 +109,7 @@ public class PayloadScoreQuery extends SpanQuery {
int result = classHash(); int result = classHash();
result = 31 * result + Objects.hashCode(wrappedQuery); result = 31 * result + Objects.hashCode(wrappedQuery);
result = 31 * result + Objects.hashCode(function); result = 31 * result + Objects.hashCode(function);
result = 31 * result + Objects.hashCode(includeSpanScore);
return result; return result;
} }
@ -132,7 +133,7 @@ public class PayloadScoreQuery extends SpanQuery {
} }
@Override @Override
public PayloadSpanScorer scorer(LeafReaderContext context) throws IOException { public SpanScorer scorer(LeafReaderContext context) throws IOException {
Spans spans = getSpans(context, Postings.PAYLOADS); Spans spans = getSpans(context, Postings.PAYLOADS);
if (spans == null) if (spans == null)
return null; return null;
@ -148,7 +149,7 @@ public class PayloadScoreQuery extends SpanQuery {
@Override @Override
public Explanation explain(LeafReaderContext context, int doc) throws IOException { 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) if (scorer == null || scorer.iterator().advance(doc) != doc)
return Explanation.noMatch("No match"); return Explanation.noMatch("No match");

View File

@ -18,14 +18,17 @@ package org.apache.lucene.queries.payloads;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermContext; import org.apache.lucene.index.TermContext;
import org.apache.lucene.index.Terms; import org.apache.lucene.index.Terms;
import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.similarities.Similarity; import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.search.spans.FilterSpans; import org.apache.lucene.search.spans.FilterSpans;
import org.apache.lucene.search.spans.FilterSpans.AcceptStatus; 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); 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 * Weight that pulls its Spans using a PayloadSpanCollector
*/ */
@ -175,11 +183,15 @@ public class SpanPayloadCheckQuery extends SpanQuery {
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
return sameClassAs(other) && return sameClassAs(other) &&
payloadToMatch.equals(((SpanPayloadCheckQuery) other).payloadToMatch); payloadToMatch.equals(((SpanPayloadCheckQuery) other).payloadToMatch) &&
match.equals(((SpanPayloadCheckQuery) other).match);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return classHash() ^ payloadToMatch.hashCode(); int result = classHash();
result = 31 * result + Objects.hashCode(match);
result = 31 * result + Objects.hashCode(payloadToMatch);
return result;
} }
} }

View File

@ -188,4 +188,24 @@ public class TestPayloadCheckQuery extends LuceneTestCase {
checkHits(query, new int[]{1103, 1203,1303,1403,1503,1603,1703,1803,1903}); 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));
}
}

View File

@ -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 IndexSearcher searcher;
private static IndexReader reader; private static IndexReader reader;
private static Directory directory; 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[] payload2 = new byte[]{2};
private static byte[] payload4 = new byte[]{4}; 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 //Make everything else 1 so we see the effect of the payload